14

I am trying to build a query that will find all user documents (docType = user) and then filter them based on many filters. Such as location, gender, age, etc. The filters are added / removed based on user input on the search function I'm building.

Below returns no results:

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
                },
                "filter": {
                    "and": {
                        "filters": 
                        [
                            {
                                "term": {
                                    "doc.docType": "user"
                                }
                            },
                            {
                                "term": {
                                    "doc.data.profile.location" : "CA"
                                }
                            }
                        ]
                    }
                }
        }
    }
}

Below return results:

{
    "query": {
        "filtered": {
            "query": {
                "field": {
                    "doc.data.profile.location" : "CA"
                }
                },
                "filter": {
                    "and": {
                        "filters": 
                        [
                            {
                                "term": {
                                    "doc.docType": "user"
                                }
                            }
                        ]
                    }
                }
        }
    }
}

The latter, although returning results, isn't going to work in the long run as I may want to include an extra filter for age, gender, etc and I can't seem to add multiple fields. The first query works if I remove the filter for location.

2
  • How did you get on with this? Commented May 1, 2014 at 15:20
  • 1
    exactly what I needed. Worked a charm! Commented May 2, 2014 at 16:20

3 Answers 3

14

Here is how you can write multiple filter query

{
  "query": {
    "bool": {
      "filter": [
        {
          "term" : {
            "id":254
          }
        },
        {
          "term" : {
            "cityId":35
          }
        }
      ],
      "must": [
        {
          "match_all": {}
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

13

The bool filter allows you to chain multiple MUST, SHOULD and SHOULD_NOT requests together. Allowing for you to construct this into one query.

2 Comments

Thank you. I will do more research and mark your answer as accepted soon if it turns out to be what I need :)
The bool filter is now replaced by the Bool Query : elastic.co/guide/en/elasticsearch/reference/current/…
5

I think what you want is a bool query

That way you can chain multiple musts together to get the desired result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.