Filter items which array contains any of given values

前端 未结 5 1098
萌比男神i
萌比男神i 2020-12-12 15:45

I have a set of documents like

{
    tags:[\'a\',\'b\',\'c\']
    // ... a bunch properties
}

As stated in the title: Is there a way to fil

5条回答
  •  萌比男神i
    2020-12-12 16:02

    Whilst this an old question, I ran into this problem myself recently and some of the answers here are now deprecated (as the comments point out). So for the benefit of others who may have stumbled here:

    A term query can be used to find the exact term specified in the reverse index:

    {
      "query": {
       "term" : { "tags" : "a" }
    } 
    

    From the documenation https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

    Alternatively you can use a terms query, which will match all documents with any of the items specified in the given array:

    {
      "query": {
       "terms" : { "tags" : ["a", "c"]}
    } 
    

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

    One gotcha to be aware of (which caught me out) - how you define the document also makes a difference. If the field you're searching in has been indexed as a text type then Elasticsearch will perform a full text search (i.e using an analyzed string).

    If you've indexed the field as a keyword then a keyword search using a 'non-analyzed' string is performed. This can have a massive practical impact as Analyzed strings are pre-processed (lowercased, punctuation dropped etc.) See (https://www.elastic.co/guide/en/elasticsearch/guide/master/term-vs-full-text.html)

    To avoid these issues, the string field has split into two new types: text, which should be used for full-text search, and keyword, which should be used for keyword search. (https://www.elastic.co/blog/strings-are-dead-long-live-strings)

提交回复
热议问题