4

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
  }
}
2
  • 1
    Why don't you use { "query": { "match": { "Commodity": 50 } } }? Commented Jun 10, 2015 at 11:29
  • 1
    I have a long list of filters like that. + I want to be able to filter with more than one value, example: Commodity to contain 50 and 45 Commented Jun 10, 2015 at 11:32

3 Answers 3

6

Try this instead of that script:

{
  "query": {
    "filtered": {
      "filter": {
        "terms": {
          "Commodity": [
            55,
            150
          ],
          "execution": "and"
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

can you share the alternate approaches here (instead of just a link)?... that'll make the answer clearer and less dependent on external sources
1

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]"
           }
         }
        ]
    }
  }
}

1 Comment

Yo can check the hits.total to see it returned any results. Will be > 0 if it has any results

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.