Using filter beside query_string in Elastic Search
Asked Answered
B

3

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?

Bivalent answered 29/9, 2014 at 8:7 Comment(0)
N
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
            }
         }
      }
   }
}
Nakamura answered 29/9, 2014 at 8:18 Comment(2)
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 lookSordid
Ah that was a typo, fixedNakamura
D
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
            }
         }
      }
   }
}
Deforce answered 14/3, 2017 at 1:53 Comment(1)
Validated on ES 6.7.2Diamine
N
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
            }
         }
      }
   }
}
Nakamura answered 29/9, 2014 at 8:18 Comment(2)
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 lookSordid
Ah that was a typo, fixedNakamura
C
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
            }
         }
      }
   }
}
Coulometer answered 18/1, 2016 at 2:32 Comment(1)
it is this which works and not the one mentioned by @Nakamura .. Was a big help thanks!Sordid

© 2022 - 2024 — McMap. All rights reserved.