Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch: How to get an exact match in a nested field

The mapping contains nested fields which shouldn't be analyzed (not sure if the 'not_analyzed' value is accurate). Is it possible to do an exact match on a nested field? In the query below the "metadata.value": "2014.NWJSD.47" still gets analyzed. Elasticsearch breaks up the string into several terms ("2014", "NWJSD", "47"). I tried to use "term" instead of "match" but this didn't return any result.

"mappings" : {
  "metadata" : {
    "type" : "nested",
    "properties" : {
      "name" : {
          "type" : "text",
          "index" : "not_analyzed"
      },
      "value" : {
          "type" : "text",
          "index" : "not_analyzed"
      }
    }
  }
}

The Query:

  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "metadata",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "metadata.name": "number"
                    }
                  },
                  {
                    "match": {
                      "metadata.value": "2014.NWJSD.47"
                      }
                    }
                  ]
                }
              }
            }
         }
      ]
    }
}
like image 458
paddel10 Avatar asked Nov 07 '17 14:11

paddel10


People also ask

How do I search in nested fields?

You can search nested fields using dot notation that includes the complete path, such as obj1.name . Multi-level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level, rather than root, if it exists within another nested query.

How does match query work in Elasticsearch?

The match query analyzes any provided text before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term. (Optional, string) Analyzer used to convert the text in the query value into tokens. Defaults to the index-time analyzer mapped for the <field> .

What is nested mapping in Elasticsearch?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What nested fields?

When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .


1 Answers

Try to use keyword instead of text in your mapping like:

{
   "mappings": {
      "your_type_name": {
         "properties": {
            "metadata" : {
               "type" : "nested",
               "properties" : {
                  "name" : {
                     "type" : "keyword"
                   },
                  "value" : {
                     "type" :"keyword"
                  }
               }
             }
           }
        }
     }
  }

These fields won't be analyzed. Then you should reindex your data and to query your data you should replace match (which is analyzed query) with term.

 {
    "query": {
       "bool": {
          "must": [
             {
                "nested": {
                   "path": "metadata",
                   "query": {
                      "bool": {
                        "must": [
                           {
                              "term": {
                                 "metadata.name": "number"
                               }
                            },
                            {
                               "term": {
                                  "metadata.value": "2014.NWJSD.47"
                               }
                            }
                         ]
                      }
                   }
                }
             }
          ]
       }
    }
 }
like image 158
mickl Avatar answered Sep 17 '22 05:09

mickl