1

I'm trying to do a query in Elasticsearch using an array as argument for the query. For example, I want to search for [apple, orange, pineapple] in the field "fruits". But in my case I want to search for an array of companyIds. I came up with this query:

{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "range":{  
                  "eventconnected":{  
                     "from":"2018-11-21T15:00:00.023Z",
                     "to":"2018-11-22T15:00:00.023Z",
                     "include_lower":true,
                     "include_upper":true,
                     "boost":1.0
                  }
               }
            },
            {  
               "match":{  
                  "idunity":{  
                     "query":[  
                        "157",
                        "160"
                     ],
                     "operator":"OR",
                     "prefix_length":0,
                     "max_expansions":50,
                     "fuzzy_transpositions":true,
                     "lenient":false,
                     "zero_terms_query":"NONE",
                     "auto_generate_synonyms_phrase_query":true,
                     "boost":1.0
                  }
               }
            }
         ],
         "adjust_pure_negative":true,
         "boost":1.0
      }
   }
}

When I run this query, I get the following error:

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[match] unknown token [START_ARRAY] after [query]","line":20,"col":30}],"type":"parsing_exception","reason":"[match] unknown token [START_ARRAY] after [query]","line":20,"col":30},"status":400}

It's as if I can't pass an array as argument for the query. So my question is: what is the correct way to pass an array as argument of a query in ElasticSearch?

To give more context: I'm doing this in a java project, the searchSourceBuilder for this query is the following: locaisTemp is the array I'm trying to pass as an argument.

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders
                    .boolQuery()
                        .must(QueryBuilders
                            .rangeQuery("eventconnected")
                                .from(tempDate0.getTime())
                                .to(tempDate.getTime()))
                        .must(QueryBuilders
                                .matchQuery("idunity", locaisTemp)) 
                    ); 

1 Answer 1

3

use termsQuery instead and also you should use filter() instead of must() since you're only filtering:

                    ...
                    .filter(QueryBuilders
                       ^    .termsQuery("idunity", locaisTemp))
                       |         ^ 
                       |         |
                       change this 
                    ...
Sign up to request clarification or add additional context in comments.

3 Comments

I just had time to test this now, it works like a charm, once again you saved my life Val :D It seems like I need to study more about ElasticSearch because it isn't as straight forward as I thought... so, to search for an array I need to use termsQuery instead of matchQuery? Is there any reason for why the matchQuery doesn't work with arrays?
I just checked the documentations and it makes much more sense now, thank you Val! I'll definetly go back to studying more about ES before I go any further into my project.
The match query works with words in a string, you can use a match query with the following input string "157 160", but for an array it makes more send to use a terms query.

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.