1

I would like to develop multiple if else condition like this :

if(condition 1)
{
process 1

}
else 
{
   if(condition 2.1)
      { 
            process 2
          }
    else (condition 2.2)
         { process 3
             }

}

is bool with must and should the optimized way to do it or can script be used? As my query is already huge, since it has fuzziness and wildcard already.

Thanks

2 Answers 2

2

I think you can use painless script query for your use case. Bool must query will not work in this case I think.

You can refer this page for how to use if else in the script query .https://www.elastic.co/guide/en/elasticsearch/painless/6.0/painless-examples.html

Sign up to request clarification or add additional context in comments.

Comments

0
GET /books/_search
{
  "_source": [
    "id",
    "name",
    "user",
    "privacy"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "status": {
              "value": 1
            }
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              { //if
                "bool": {
                  "must": [
                    {
                      "term": {
                        "user.privacy.mode": {
                          "value": 0
                        }
                      }
                    },
                    {
                      "term": {
                        "privacy.mode": {
                          "value": 0
                        }
                      }
                    }
                  ]
                }
              },
              {//else if
                "bool": {
                  "must": [
                    {
                      "term": {
                        "user.privacy.mode": {
                          "value": 2
                        }
                      }
                    },
                    {
                      "bool": {
                        "minimum_should_match": 1,
                        "should": [
                          {// if
                            "nested": {
                              "path": "readers",
                              "query": {
                                "match": {
                                  "readers.id": "621120dc86b8920019295363"
                                }
                              }
                            }
                          },
                          { // else
                            "nested": {
                              "path": "buyers",
                              "query": {
                                "match": {
                                  "buyers.purchase.id": "621120dc86b8920019290f50"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {// else if
                "bool": {
                  "must": [
                    {
                      "term": {
                        "privacy.mode": {
                          "value": 2
                        }
                      }
                    },
                    {
                      "bool": {
                        "minimum_should_match": 1,
                        "should": [
                          {
                            "nested": {
                              "path": "readers",
                              "query": {
                                "match": {
                                  "readers.id": "621120dc86b89200195373"
                                }
                              }
                            }
                          },
                          {
                            "nested": {
                              "path": "buyers",
                              "query": {
                                "match": {
                                  "buyers.purchase.id": "621120dc86b892001929036350"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
      "filter": {
        "bool": {
          "must_not": [
            {
              "term": {
                "user.privacy.mode": 1
              }
            },
            {
              "term": {
                "privacy.mode": 1
              }
            }
          ]
        }
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.