Scope Elasticsearch Results to Specific Ids Scope Elasticsearch Results to Specific Ids elasticsearch elasticsearch

Scope Elasticsearch Results to Specific Ids


Here is an example query which might work for you. This assumes that the _all field is enabled on your index (which is the default). It will do a full text search across all the fields in your index. Additionally, with the added ids filter, the query will exclude any document whose id is not in the given array.

{  "bool": {    "must": {      "match": {        "_all": "your search text"      }    },    "filter": {      "ids": {        "values": ["1","2","3","4"]      }    }  }}

Hope this helps!


As discussed by Ali Beyad, ids field in the query can do that for you. Just to complement his answer, I am giving an working example. In case anyone in the future needs it.

GET index_name/_search{  "query": {    "bool": {      "must": [        {          "match": {            "field": "your query"          }        },        {          "ids" : {            "values" : ["0aRM6ngBFlDmSSLpu_J4", "0qRM6ngBFlDmSSLpu_J4"]          }        }      ]    }  }}


You can create a bool query that contains an Ids query in a MUST clause:https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-ids-query.html

By using a MUST clause in a bool query, your search will be further limited by the Ids you specify. I'm assuming here by Ids you mean the _id value for your documents.