Multiple Must queries not working inside a should query as expected

I am trying to write a query of the form A or B or C or D where condition A is of the form A11 and A12, similarly B = B11 and B12 and so on. I am using elastic version 6.8 and executing the queries in Kibana. I have added "_name" to track which documents matched what conditions.

{
  "size": "1000",
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "taxRegistrationNumber_processed.lowercase_keyword": {
                    "query": "111222333",
                    "operator": "OR",
                    "fuzziness": "2",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "A11"
                  }
                }
              },
              {
                "match": {
                  "doc_mapping_type": {
                    "query": "merchant",
                    "operator": "OR",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "A12"
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "taxRegistrationNumber_processed": {
                    "query": "111222333",
                    "operator": "OR",
                    "fuzziness": "2",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "B11"
                  }
                }
              },
              {
                "match": {
                  "doc_mapping_type": {
                    "query": "merchant",
                    "operator": "OR",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "B12"
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "owner_fullName_processed": {
                    "query": "test user",
                    "operator": "OR",
                    "fuzziness": "AUTO",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "C11"
                  }
                }
              },
              {
                "match": {
                  "doc_mapping_type": {
                    "query": "owners",
                    "operator": "OR",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "C12"
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "owner_fullName_processed.lowercase_keyword": {
                    "query": "test user",
                    "operator": "OR",
                    "fuzziness": "2",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "D11"
                  }
                }
              },
              {
                "match": {
                  "doc_mapping_type": {
                    "query": "owners",
                    "operator": "OR",
                    "prefix_length": 0,
                    "max_expansions": 50,
                    "fuzzy_transpositions": true,
                    "lenient": false,
                    "zero_terms_query": "NONE",
                    "boost": 1,
                    "_name": "D12"
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ]
    }
  }
}

``
I am getting documents  with the following 

"matched_queries": [

      "A11",

      "C12",

      "B11",

      "C11",

      "D11",

      "D12"

    ]
My requirement is if a document satisfied condition A11 it must also satisfy A12. 
Same goes for others.
Thanks for the help :slight_smile: .

It looks like your document matched your logic correctly - C had both of the required 11s and 12s. The other reported half-matches for A and B are just added info you requested. If you are only interested in “x11 AND x12” match reporting shift the _name param up one level to the Bool that contains the 11 and 12 clauses and rename it to something appropriate eg b11&b12

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.