Filter _id range in elastics search

I am trying to filter the _id field (index not included) in elasticsearch by range. Is it possible? If so, how can this be done? I read in the elasticsearch documentation that we can use “identifiers” to query on _id and type, but I don’t see how this can be done with a range filter. (I do not want to include the index on _id).

{ "from": 0, "size": 20, "query": { "match_all": {} }, "filter": { "range": { "_id": { "gt": "51f7b6b7710c42b136027581" } } }, "sort": { "pubdate": { "order": "desc" } } } 
+7
filter range elasticsearch
source share
1 answer

It may be a little late, but I'm trying to answer, and maybe the answer is still useful to you.
Having seen the comments made for colleagues, I think that two main ideas can be extracted:

  • The generated Elasticsearch identifier cannot be used to filter or perform any operation other than GET o search.
  • The value of the custom index uid is indicated (and how I will try to solve the problem)

So, I coded an example to check if solution 2 is possible. The key parts are the following:

 #cluster node to query es = Elasticsearch(['localhost:9200',]) records = [ #some custom data ] for idx,r in enumerate(records): _index_config = dict(index_config) #set Elasticsearch uid _index_config['_id'] = idx #replicate in a document field to be able to filter for r['id'] = idx kwargs['body'].append({'index' : _index_config}) kwargs['body'].append(r) _ = es.bulk(**kwargs) 

Once you have indexed the id field, you can filter it as you wish. The range filter is one of them.

 elasticsearch_query = { "query": { "filtered": { "filter": { "range": { "id": { "gte" : 3, "lt" : 5 } } } } } } 

You can see a working example in this notebook.

+1
source share

All Articles