2
votes

I have a nested type field in my mapping. When I use Term search query on my nested field no result is returned from Elasticsearch whereas when I change Term to Match query, it works fine and Elasticsearch returns expected result

here is my mapping, imagine I have only one nested field in my type mapping

{
"homing.estatefiles": {
    "mappings": {
        "estatefile": {
            "properties": {
                "DynamicFields": {
                    "type": "nested",
                    "properties": {
                        "Name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "ValueBool": {
                            "type": "boolean"
                        },
                        "ValueDateTime": {
                            "type": "date"
                        },
                        "ValueInt": {
                            "type": "long"
                        }
                    }
                }
            }
        }
    }
}
}

And here is my term query (which returns no result)

{
 "from": 50,
  "size": 50,
  "query": {
   "bool": {
   "filter": [
    {
      "nested": {
        "query": {
          "bool": {
            "must": [
              {
                "term": {
                  "DynamicFields.Name":{"value":"HasParking"}
                }
              },
              {
                "term": {
                  "DynamicFields.ValueBool": {
                    "value": true
                  }
                }
              }
            ]
          }
        },
        "path": "DynamicFields"
      }
    }
  ]
  }
 }
}

And here is my query which returns expected result (by changing Term query to Match query)

{
 "from": 50,
 "size": 50,
 "query": {
  "bool": {
   "filter": [
    {
      "nested": {
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "DynamicFields.Name":"HasParking"
                }
              },
              {
                "term": {
                  "DynamicFields.ValueBool": {
                    "value": true
                  }
                }
              }
            ]
          }
        },
        "path": "DynamicFields"
      }
     }
    ]
   }
 }
}
1

1 Answers

2
votes

This is happening because the capital letters with the analyzer of elastic.

When you are using term the elastic is looking for the exact value you gave. up until now it sounds good, but before it tries to match the term, the value you gave go through an analyzer of elastic which manipulate your value. For example in your case it also turn the HasParking to hasparking.

And than it will try to match it and of course will fail. They have a great explanation in the documentation in the "Why doesn’t the term query match my document" section. This analyzer not being activated on the value when you query using match and this why you get your result.