es的range过滤_在Elasticsearch中过滤_id范围

I am trying to filter _id field (index not enabled) in elasticsearch by range. Is it possible? If so, how it can be done?

I've read in elasticsearch documentation that we can use 'ids' to query by _id and type, but I can't see how it can be done with range filter. (I don't want to enable index on _id).

{

"from": 0,

"size": 20,

"query": {

"match_all": {}

},

"filter": {

"range": {

"_id": {

"gt": "51f7b6b7710c42b136027581"

}

}

},

"sort": {

"pubdate": {

"order": "desc"

}

}

}

解决方案

Maybe it's a little late, but I try to answer and maybe the answer is still usefull for you.

Seen the comments done for collegues, I think that two main ideas can be extracted:

Generated id by Elasticsearch can't be used to filter or perform any operation other than GET o id search.

Index custom uid value is guessed (and that's the way I'd try to solve the problem)

So, I've coded an example to check if solution 2 is possible. Key parts are these:

#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 by it as you wish. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值