Home > Enterprise >  Automate the writing of complex elastic requests
Automate the writing of complex elastic requests

Time:07-04

I get a string from my user that represents an elastic request. There can be the following types of requests:

Single term: TERM:MATCH_TERM ;

Several terms separated by AND: TERM:MATCH_TERM AND TERM:MATCH_TERM AND TERM:MATCH_TERM ;

Several terms separated by OR: TERM:MATCH_TERM OR TERM:MATCH_TERM OR TERM:MATCH_TERM ;

Several terms separated by AND and OR: TERM:MATCH_TERM AND TERM:MATCH_TERM OR TERM:MATCH_TERM AND TERM:MATCH_TERM OR TERM:MATCH_TERM AND TERM:MATCH_TERM ;

The OR operator has priority over the AND operator

I parse this string next and get a list of my different terms.

I want to use this list now to build my elastic query. I know I will have to use the syntax for complex queries: bool. (I use the javascript client of elastic to make my request)

From what I understand, the must filter will allow me to do an AND and the should filter will allow me to do an OR.

In the case where we have: TERM1:MATCH_TERM1 AND TERM2:MATCH_TERM2 OR TERM3:MATCH_TERM3 AND TERM4:MATCH_TERM4 OR TERM5:MATCH_TERM5 AND TERM6:MATCH_TERM6 ;

Would the correct syntax be something like this:

"query":{
    "bool":{ 
      "should":[
         "must":[
             {"terms":{"TERM1":"MATCH_TERM1"}}
         ] , 
         "must":[
             {"terms":{"TERM2":"MATCH_TERM2"}}
         ] , 
      ],
      "should":[
         "must":[
             {"terms":{"TERM3":"MATCH_TERM3"}}
         ] , 
         "must":[
             {"terms":{"TERM4":"MATCH_TERM4"}}
         ] , 
      ],
      "should":[
         "must":[
             {"terms":{"TERM5":"MATCH_TERM5"}}
         ] , 
         "must":[
             {"terms":{"TERM6":"MATCH_TERM6"}}
         ] , 
      ],
    }
  }

If there is any information missing to answer my question let me know and I will try to add it as soon as possible.

Thank you in advance if you take the time to help me.

CodePudding user response:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": [
              {
                "terms": {
                  "TERM1": [
                    "MATCH_TERM1"
                  ]
                }
              },
              {
                "terms": {
                  "TERM2": [
                    "MATCH_TERM2"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "terms": {
                  "TERM3": [
                    "MATCH_TERM3"
                  ]
                }
              },
              {
                "terms": {
                  "TERM4": [
                    "MATCH_TERM4"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "terms": {
                  "TERM5": [
                    "MATCH_TERM5"
                  ]
                }
              },
              {
                "terms": {
                  "TERM6": [
                    "MATCH_TERM6"
                  ]
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": "1"
    }
  }
}
  • Related