Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass list of values for a particular field in Elastic Search Query

I have a query to search for a provider_id from the Elastic Search Cluster. I am using the below query to get results for a single provider_id but need help in figuring out how can I pass a list of providers.

{
"query": {
    "bool": {
        "must": [{
            "match": {
                "message.provider_id": {
                    "query": 943523,
                    "type": "phrase"
                }
            }
        }]
    }
}
}

Suppose I want to search for provider_ids = [913523, 923523, 923523, 933523, 953523] then how should I modify the query?

like image 413
python Avatar asked Sep 14 '16 16:09

python


People also ask

How do I select a specific field in Elasticsearch?

There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.

How do you query Elasticsearch data?

You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .

How do I get all Elasticsearch index data?

You can use cURL in a UNIX terminal or Windows command prompt, the Kibana Console UI, or any one of the various low-level clients available to make an API call to get all of the documents in an Elasticsearch index. All of these methods use a variation of the GET request to search the index.


1 Answers

You could index the provider_id as not_analyzed and then use a terms query:

POST /test_index/_search
{
    "query": {
        "terms": {
           "message.provider_id": [
              "913523", "923523", "923523", "933523", "953523"
           ]
        }
    }
}

or as a bool query with a filter if you are not going to need the score:

POST /test_index/_search
{
   "query": {
      "bool": {
         "filter": [
            {
               "terms": {
                  "message.provider_id": [
                      "913523", "923523", "923523", "933523", "953523"
                  ]
               }
            }
         ]
      }
   }
}
like image 200
Henrik Avatar answered Nov 15 '22 07:11

Henrik