5

I have a simplified object that looks something like this:

"name" : "Partner Name",
"features" : [ 
    {
        "val" : "Family",
        "key" : "Type"
    },
    {
        "val" : "Paris",
        "key" : "City"
    }
],
"variants" : [ 
    {
        "name" : "Activity 1 Name",
        "description" : "Quick description",
        "price" : 20
    }
]

I want to filter by the City and Type keys. My current query filters by price but I can't get it working for City or Type. Adding more terms to the filter array didn't do the trick.

'query':{
    'filtered':{
        'query':{
            'query_string':{
                'query':query
            }
        },
    'filter': {
        'bool':{
            'filter': [{ 
                    'range': {
                        'variants.price': {
                            'gte': 0
                        }
                    }
                },
                { 
                    'range': {
                        'variants.price': {
                            'lte': 50
                        }
                    }
                },
                {
                    'term': {
                        'active': true
                    }
                }
            ]
        }
     }
  }
}

Any help would be appreciated. Thanks!

1
  • 3
    I wish there was lengthy documentation on writing queries like this. Spending hours swimming through a variety of suggestions on stack overflow sucks. Especially when over half the accepted answers are invalid due to version breaking changes Commented Nov 1, 2019 at 1:57

1 Answer 1

6
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "range": {
                "variants.price": {
                  "gte": 0
                }
              }
            },
            {
              "range": {
                "variants.price": {
                  "lte": 50
                }
              }
            },
            {
              "nested": {
                "path": "features",
                "query": {
                  "bool": {
                    "should": [
                      {"term":{"features.key":"type"}},
                      {"term":{"features.key":"city"}}
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

15 Comments

Thanks for the reply. I tried formatting the JSON like you suggested but I'm still getting a 400 bad response error. Here's the pastebin of my object pastebin.com/nsex7PfK
What ES version are you using?
Elasticsearch 2.3.4
I think the issue is that I'm not inserting my documents correctly. I'm using the bulk API and included type:'nested' but when using the Sense chrome plugin to search I'm getting the error "[nested] nested object under path [features] is not of nested type"
You need to create the index upfront. And to give it your own mapping definition. At least for the fields that are nested. Otherwise ES will not know that you want nested fields.
|

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.