paddel10
paddel10

Reputation: 180

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"
                      }
                    }
                  ]
                }
              }
            }
         }
      ]
    }
}

Upvotes: 5

Views: 2975

Answers (2)

pbuzulan
pbuzulan

Reputation: 67

I think you are looking for a query string query. You can freely disable "analyze" option for that field in mapping option and reindex everything again but you could also check this query out:

as written here:

GET /_search
{
    "query": {
        "query_string" : {
            "query" : "your string"
        }
    }
}

Upvotes: 0

mickl
mickl

Reputation: 49985

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"
                               }
                            }
                         ]
                      }
                   }
                }
             }
          ]
       }
    }
 }

Upvotes: 6

Related Questions