How to query and apply filter on a field that has multi_field type


#1

I am going through the documentation and understood that multi_field is used to apply different analyzers on one field.

{
"tweet" : {
    "properties" : {
        "name" : {
            "type" : "multi_field",
            "fields" : {
                "name" : {"type" : "string", "index" : "analyzed"},
                "untouched" : {"type" : "string", "index" : "not_analyzed"}
            }
        }
    }
}

}

I have indexed the data as follows

/tweet/1   { 
  "name" : "anil"
}

And I am searching for the same as follows.

/<indexname>/_search

I am getting the data as follows.

hits : [
{
           "_index" : ...
           ....
           "_source" : {
                  "name" : "anil" 
          }
 }
]

So far this is good.

Now I am looking for match.

GET <indexname>/_search
   {
          "query": {
              "match": {
                 "name": "anil"
              }
           }
    }

The above one works fine since I have the analyzer applied on it.

My question is, If I want to apply filter with exact string, I am getting 0 hits.

GET <indexname>/_search
        {
           "query": {
                   "filtered": {
                         "query": {
                              "match_all": {}
                          },
                          "filter": {
                                  "term": {
                                        "name" : "anil"
                                    }
                          }
                   }
               }
         }

Please suggest what to do.


(Jason Wee) #2

name is analyzed field right? if you want to query the field on not analyzed, you should maybe use dot notation, like name.untouched , for example

name.untouched : "anil"

hth


#3

I am doing that in my API, if client pass a query search on name, I am using name, if they pass filter I am using "name.untouched".
is there any way that I can use name only and make use of both analyzed and not analyzed fields ?
because, my client will never no that I have one field which is analyzed and which one is not analyzed. Instead of doing it in my code, I expected some field to be set in elastic so that will for both.


(Jason Wee) #4

try this one? https://www.elastic.co/blog/multi-field-search-just-got-better#_most_matching_fields


(system) #5