14

How to full text search and have filter? I want to search for a text among documents with language_id=10. I've tried it this way:

{
  "query": {
    "query_string": {
      "query": "Declared"
    },
    {
      "filtered": {
        "filter": {
          "term": {
            "language_id": 10
          }
        }
      }
    }
  }
}

but seems like it's not correct. How to correct it?

3 Answers 3

15

In version 5.2, filtered query is replaced by the bool query, and returns error on my Elastic 5.2 instance. See here.

The new syntax is:

{
   "query":{
      "bool":{
         "must":{
            "query_string":{
               "query":"Declared"
            }
         },
         "filter":{
            "term":{
               "language_id":10
            }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Validated on ES 6.7.2
14

Yep, the syntax of the filtered query is a bit cumbersome. AFAIK it should look like that:

{
   "query":{
      "filtered":{
         "query":{
            "query_string":{
               "query":"Declared"
            }
         },
         "filter":{
            "term":{
               "language_id":10
            }
         }
      }
   }
}

2 Comments

I tried your format but it doesn't work, the filters are working better in the format of @Kévin 's answer. Please have a look
Ah that was a typo, fixed
4

Sorry Ashalynd but the filter is not placed a the right place in your answer.

This is working better:

{
   "query":{
      "filtered":{
         "query":{
            "query_string":{
               "query":"Declared"
            }
         },
         "filter":{
            "term":{
               "language_id":10
            }
         }
      }
   }
}

1 Comment

it is this which works and not the one mentioned by @Ashalynd .. Was a big help thanks!

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.