简体   繁体   中英

Elasticsearch - passing an array as argument in a query

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)) 
                    ); 

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

                    ...
                    .filter(QueryBuilders
                       ^    .termsQuery("idunity", locaisTemp))
                       |         ^ 
                       |         |
                       change this 
                    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM