0

I have an index with the following mapping:

curl -XPUT "http://localhost:9200/index1" -d'
{
    "mappings": {
        "movie": {
            "properties": {
                "people": {
                    "properties": {
                        "cast": {
                            "type": "nested",
                            "properties": {
                                "firstName": {
                                    "type": "string"
                                },
                                "lastName": {
                                    "type": "string"
                                }
                            }
                        },
                        "producers": {
                           "type": "nested",
                           "properties": {
                              "lastName": {
                                 "type": "string"
                              }
                           }
                        }
                    }
                }
            }
        }
    }
}'

Then I put a simple doc in there:

curl -XPOST "http://localhost:9200/index1/movie/" -d'
{
   "title": "The Matrix",
   "people": {
        "cast": [
      {
         "firstName": "Keanu",
         "lastName": "Reeves"
      },
      {
         "firstName": "Laurence",
         "lastName": "Fishburne"
      }
    ]
    }
}'

Now, this is the interesting part. If I make a nested filter for the firstName field, it worsk just fine. However, if I do it for lastName it does not work at all. It seems that because this fields appears in two nested properties I cannot filter with them:

WORKS:

curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "people.cast",
               "filter": {
                  "term": {
                     "firstName": "keanu"
                  }
               }
            }
         }
      }
   }
}'

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"index1","_type":"movie","_id":"AUzX6wLHdI5sfxoODE5E","_score":1.0,"_source":
{
   "title": "The Matrix",
   "people": {
    "cast": [
      {
         "firstName": "Keanu",
         "lastName": "Reeves"
      },
      {
         "firstName": "Laurence",
         "lastName": "Fishburne"
      }
    ]
  }
}}]}}

DOESN'T WORK:

curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "people.cast",
               "filter": {
                  "term": {
                     "lastName": "reeves"
                  }
               }
            }
         }
      }
   }
}'

{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%  

Is this a bug or I am doing something wrong?

1 Answer 1

1

I don't really understand why your first query works, but here's how to get the second one to work:

curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "people.cast",
               "filter": {
                  "term": {
                     "people.cast.lastName": "reeves"
                  }
               }
            }
         }
      }
   }
}'

Here is the code I used to test it:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.