Search code examples
elasticsearchelastic-stackelasticsearch-5

How do I query nested with normal match query together?


I want to fire nestedQuery on addresses and multiMatchQuery on name in single query. I tried few ways but I am getting "[bool] query does not support [nested]". I don't know whether this is possible or not (ES version: 7.x).

When I separately querying (i.e.nestedQuery() & multiMatchQuery()) that time it is working fine. Please help me with that. This is the mapping I am using:

{
  "employee" : {
    "mappings" : {
      "properties" : {
        "addresses" : {
          "type" : "nested",
          "properties" : {
            "permanentAddress" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "TemporaryAddress" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}



1.    {
      "query": {
        "nested": {
          "path": "addresses",
          "query": {
            "bool": {
              "must": [
                { "match": { "addresses.permanentAddress": "xxx" } }
          
              ]
            }
          },
          "score_mode": "avg"
        }
      }
    }
    
2.    {
     "query": {
        "bool": {
            "must" : [
              {
                "multi_match" : {
                  "query" : "xxx",
                  "fields" : [
                    "name^1.0"
                  ],
                  "type" : "best_fields",
                  "boost" : 1.0
                }
              }
            ]
        }
      }
    }
  1. nestedQuery() = looking for xxx value in addresses.permanentAddress
  2. multi_match() = looking for xxx value in name

If value of name or addresses.permanentAddress matches with xxx then returns the result.


Solution

  •   "bool" : {
    "should" : [
      {
        "bool" : {
          "must" : [
            {
              "match" : {
                "name" : {
                  "query" : "xxx",
                  "operator" : "AND",
                  "prefix_length" : 0,
                  "max_expansions" : 50,
                  "fuzzy_transpositions" : true,
                  "lenient" : false,
                  "zero_terms_query" : "NONE",
                  "auto_generate_synonyms_phrase_query" : true,
                  "boost" : 1.0
                }
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "nested" : {
          "query" : {
            "bool" : {
              "must" : [
                {
                  "match" : {
                    "employee.permanentAddress" : {
                      "query" : "xxx",
                      "operator" : "AND",
                      "prefix_length" : 0,
                      "max_expansions" : 50,
                      "fuzzy_transpositions" : true,
                      "lenient" : false,
                      "zero_terms_query" : "NONE",
                      "auto_generate_synonyms_phrase_query" : true,
                      "boost" : 1.0
                    }
                  }
                }
              ],
              "adjust_pure_negative" : true,
              "boost" : 1.0
            }
          },
          "path" : "employee",
          "ignore_unmapped" : false,
          "score_mode" : "none",
          "boost" : 1.0,
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
    

    }

    This Bool query with nested worked for me and with that I can able to check parent's as well as nested property.