在Elasticsearch中过滤_id范围 [英] Filter _id range in elasticsearch

查看:687
本文介绍了在Elasticsearch中过滤_id范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按范围过滤Elasticsearch中的_id字段(未启用索引)。可能吗?如果是这样,怎么办?
我已经阅读了Elasticsearch文档,可以使用 ids通过_id和类型进行查询,但是我看不到如何使用范围过滤器来完成。 (我不想在_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:


  1. Elasticsearch生成的ID不能用于过滤或执行

  2. 可以猜测索引自定义uid值(这就是我试图解决问题的方式)

因此,我编写了一个示例来检查解决方案2是否可行。关键部分包括:

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)

一旦索引了 id 字段,您可以根据需要过滤它。 范围过滤器就是其中之一

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
                    }
                }
            }
        }
    }
}

您可以在此笔记本

这篇关于在Elasticsearch中过滤_id范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆