user3658423
user3658423

Reputation: 1944

elasticsearch filter on object property

I am trying to filter a term on object (person) property (name) but dont seem to get what's going wrong. If I filter on field "t" it works but I cant seem to filter on object! Can anyone please help!

My query is below:

"query": {
    "filtered" : {
        "filter" : {
            "term" : { "person.fname" : "JOHN"}
        }      
    }

}

My mapping:

    {
        "_ttl": {"enabled": true, "default": '90d'},

        "properties" : {
            "t": { "type" : "date" },

            "person": {   "type" : "object",
                    "properties" : {
                      "fname" : { "type" : "string"},
                      "lname" : { "type" : "string"}
                    }
            }

        }

    }

This document exist in the index...

{
  "_index" : "personindex",
  "_type" : "S",
  "_id" : "AUxnFFrAGK3sw8tqMMY0",
  "_score" : 1.0,
  "_source":{"t":1427658527178,"person":{"fname":"JOHN","lname":"DOE"}}
}

Upvotes: 0

Views: 5736

Answers (1)

Sloan Ahrens
Sloan Ahrens

Reputation: 8718

The problem has to do with analysis. Since your mapping doesn't specify an analyzer, the standard analyzer is used. The standard analyzer converts tokens to lower-case. So the term "john" ends up in the inverted index, but the term "JOHN" does not. When you run a term query, the query text is not analyzed, and since no document matches the exact term "JOHN" you get no results.

This query will give you what you want:

{
   "query": {
      "filtered": {
         "filter": {
            "term": {
               "person.fname": "john"
            }
         }
      }
   }
}

As will this one (since the match query performs analysis on the query text):

{
    "query": {
        "match": {
           "person.fname": "JOHN"
        }
    }
}

Alternatively, you could set up a different analyzer (or none) on that field in order to make your term filter work.

Here is some code I used to play around with it:

http://sense.qbox.io/gist/a87bcca08496218b07287d358939c1caed42f047

Upvotes: 5

Related Questions