zunman
zunman

Reputation: 118

need help writing elasticsearch query with series of AND and ORs

I'm writing an elasticsearch query where I have an arbitrary number of AND clauses, and each AND clause can have an OR clause. e.g. A AND B AND C where A can have values from A1 OR A2, B can have values from B1 OR B2, C can have values from C1 or C2. OR is always between 2 elements, or can be missing altogether. AND is among arbitrary elements.

this is where I'm at, with a syntax error.

{
  "size": 50,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": 
              [
                {
                  "query_string": {
                    "default_field": "text",
                    "query": "\"abc\" \"cde\" ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                },
                {
                  "query_string": {
                    "default_field": "standard_text",
                    "query": "\"rst\" \"xyz\" ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                }
              ],"should": 
              [
                {
                  "query_string": {
                    "default_field": "text",
                    "query": "\"dan\" \"po\" \"in\" ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                },
                {
                  "query_string": {
                    "default_field": "standard_text",
                    "query": "\"hes\" \"wo\" \"aid\"  ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                }
              ],{
                "query_string": {
                  "default_field": "standard_text",
                  "query": "\"i\" \"me\" \"my\" \"we\" \"i am\" \"i'm\" ",
                  "minimum_should_match": "1",
                  "analyze_wildcard": true
                }
              }
            
          },

        }
      ],
      "must_not": [ ],
      "filter": []
    }
  }
} 

Upvotes: 1

Views: 143

Answers (1)

Tushar Shahi
Tushar Shahi

Reputation: 20701

You are using multiple should at the same level. Every should is a group of ors (in your example A1,A2 etc. or B1,B2 etc.).

Say you have A(A1 or A2) AND B(B1 or B2) AND C(C1), then A,B,C are supposed to be at the same level. Each being a top level query in the must array:

{
  "size": 50,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": 
              [
                {
                  "query_string": {
                    "default_field": "text",
                    "query": "\"abc\" \"cde\" ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                },
                {
                  "query_string": {
                    "default_field": "standard_text",
                    "query": "\"rst\" \"xyz\" ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                }
              ]  }   },
       {
          "bool": {
          "should": 
              [
                {
                  "query_string": {
                    "default_field": "text",
                    "query": "\"dan\" \"po\" \"in\" ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                },
                {
                  "query_string": {
                    "default_field": "standard_text",
                    "query": "\"hes\" \"wo\" \"aid\"  ",
                    "minimum_should_match": "1",
                    "analyze_wildcard": true
                  }
                }
              ]  }   } ,
    {
          "bool": { "should": 
              [
      {
                "query_string": {
                  "default_field": "standard_text",
                  "query": "\"i\" \"me\" \"my\" \"we\" \"i am\" \"i'm\" ",
                  "minimum_should_match": "1",
                  "analyze_wildcard": true
                }
              }]

        }  }
      ],
      "must_not": [ ],
      "filter": []
    }
  }
} 

Upvotes: 2

Related Questions