Including a filter with a query string


(Vincent Stevenson) #1

What does the JSON query look like that includes a filter with the query string? If I am querying an index and want to filter results that come back with my query string, I tried the following:

GET animals/_search
{
"query": {
"query_string": {
"query": "dog"
},
"filter": {
"should" : [
{"match" : {"color" : "brown"}}
]
}
}
}

But I get a message saying:
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 6,
"col": 5
}
],
"type": "parsing_exception",
"reason": "[query_string]

Is is possible to include filters with query strings?

Thanks!


(Gordon Brown) #2

You absolutely can do this! You want a bool query, like so:

GET animals/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "dog"
          }
        }
      ],
      "filter": [
        {
          "match": {
            "color": "brown"
          }
        }
      ]
    }
  }
}

(Vincent Stevenson) #3

Thanks!

Also, if I had different fields, is there a way I can add a section to the query to boost specific fields? If I wanted to place an emphasis on color over body type, what would it look like?

Thanks again!


(system) closed #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.