griffon vulture
griffon vulture

Reputation: 6764

elasticsearch -check if array contains a value

I want to check on an field of an array long type that includes some values. the only way I found is using script: ElasticSearch Scripting: check if array contains a value

but it still not working fore me: Query:

  {
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "script": {
                    "script": "doc['Commodity'].values.contains(param1)",
                    "params": {
                        "param1": 50
                    }
                }
            }
        }
    }
}

but I get 0 hits. while I have the records:

{
"_index" : "aaa",
"_type" : "logs",
"_id" : "2zzlXEOgRtujWiCGtX6s9Q",
"_score" : 1,
"_source" : {

   "Commodity" : [
     50
    ],
   "Type" : 1,
    "SourceId" : "fsd",
      "Id" : 123
  }
}

Upvotes: 4

Views: 31547

Answers (3)

dotnetdev_2009
dotnetdev_2009

Reputation: 791

For those of you who are using es 6.x, this might help. Here I am checking whether the user([email protected]) has any orders by passing in an array of orders

GET user-orders/_search
{
  "query": {
    "bool": {
      "filter": [
         {
           "terms":{
             "orders":["123456","45678910"]
           }
         },
         {
           "term":{
             "user":"[email protected]"
           }
         }
        ]
    }
  }
}

Upvotes: 1

ShashankAC
ShashankAC

Reputation: 1078

For those of you using the latest version of Elasticsearch (7.1.1), please note that "filtered" and "execution" are deprecated so @Andrei Stefan's answer may not help anymore.

You can go through the below discussion for alternative approaches.

https://discuss.elastic.co/t/is-there-an-alternative-solution-to-terms-execution-and-on-es-2-x/41089

In the answer written by nik9000 in the above discussion, I just replaced "term" with "terms" (in PHP) and it started working with array inputs and AND was applied with respect to each of the "terms" keys that I used.

EDIT: Upon request I will post a sample query written in PHP.

'body' => [ 
      'query' => [ 
           'bool' => [
               'filter' => [
                     ['terms' => ['key1' => array1]],
                     ['terms' => ['key2' => array2]],   
                     ['terms' => ['key3' => array3]],
                     ['terms' => ['key4' => array4]],                            
                           ]
                     ]
                 ]
             ] 

key1,key2 and key3 are keys present in my elasticsearch data and they will be searched for in their respective arrays. AND function is applied between the ["terms" => ['key' => array ] lines.

Upvotes: 6

Andrei Stefan
Andrei Stefan

Reputation: 52368

Try this instead of that script:

{
  "query": {
    "filtered": {
      "filter": {
        "terms": {
          "Commodity": [
            55,
            150
          ],
          "execution": "and"
        }
      }
    }
  }
}

Upvotes: 6

Related Questions