1

I'm working on integration elasticsearch into an application we are building. I run into trouble with nested queries and filters. Here's an example of our mapping:

"index": {
    "mappings": {
        "users": {
            "properties": {
                "membership": {
                    "type": "nested",
                    "properties": {
                        "membership": {
                            "type": "string"
                        },
                        "start date": {
                            "type": "date"
                        },
                        "end date date": {
                            "type": "date"
                        }
                    }
                }
            }
        }
    }
}

Now, i want to find all users with "membership" "foo bar", without an enddate. The problem is that when i use a standard analyzer, "foo bar" is split into two words in the index, so i can't use the term filter to look for "foo bar". We can solve this by using a different analyzer, but this has other disadvantages, like problems with upper/lowercases, etc.

Besides: it's quite easy to find everyone with a "foo bar" membership by using a query instead of a filter. The problem is that there is no query to find nested documents with "End date" = missing. There is a filter for this, but i can't seem to be able to combine the query "membership" = "foo bar" with the filter "end date" = missing, within the same nested document.

So, i can find everyone with "end date" = missing and i can find everyone with membership "foo bar", but not everyone with no end date specificaly for membership "foo bar".

Are there any suggestions on how to solve this problem?

1 Answer 1

1

This seems to do it:

POST /test_index/_search
{
   "query": {
      "nested": {
         "path": "membership",
         "query": {
            "bool": {
               "must": [
                  {
                     "match": {
                        "membership.membership": {
                           "query": "foo bar",
                           "operator": "and"
                        }
                     }
                  },
                  {
                     "filtered": {
                        "filter": {
                           "not": {
                              "filter": {
                                 "exists": {
                                    "field": "membership.end_date"
                                 }
                              }
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

Here is the code I used to set it up (ES 1.5.1):

http://sense.qbox.io/gist/20a12a71178f0aac4369f52ab0c4c811ba803122

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.