【问题标题】:Finding multiple Elasticsearch documents with same ids, different types查找具有相同 ID、不同类型的多个 Elasticsearch 文档
【发布时间】:2015-11-09 15:11:52
【问题描述】:

我需要确定是否有任何具有特定 id 的文档已在我的 ES 数据库中建立索引,以便我可以在索引新文档之前删除它们。

问题是我事先不知道它被索引为的 type。 我发现 _mget 查询听起来可能是我需要的,但是文档中的这句话说我在搜索时只得到 1(随机)命中

如果您没有设置类型并且有许多共享相同的文档 _id,你最终只会得到第一个匹配的文档。

我怎样才能得到这种行为;在没有昂贵的_search查询的情况下,在同一索引中查找共享_id,可能> 1且具有不同_type的所有文档?

谢谢!

【问题讨论】:

    标签: elasticsearch duplicates


    【解决方案1】:

    "_id" 上的简单 term query 对我有用。

    所以我创建了一个简单的索引并分别添加了两个文档,用于两种不同的类型:

    PUT /test_index
    
    POST /test_index/_bulk
    {"index":{"_type":"type1","_id":1}}
    {"name":"type1 doc1"}
    {"index":{"_type":"type1","_id":2}}
    {"name":"type1 doc2"}
    {"index":{"_type":"type2","_id":1}}
    {"name":"type2 doc1"}
    {"index":{"_type":"type2","_id":2}}
    {"name":"type2 doc2"}
    

    这个查询将返回两个 id 为 1 的文档:

    POST /test_index/_search
    {
       "query": {
          "constant_score": {
             "filter": {
                "term": {
                   "_id": "1"
                }
             }
          }
       }
    }
    ...
    {
       "took": 5,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 1,
          "hits": [
             {
                "_index": "test_index",
                "_type": "type1",
                "_id": "1",
                "_score": 1,
                "_source": {
                   "name": "type1 doc1"
                }
             },
             {
                "_index": "test_index",
                "_type": "type2",
                "_id": "1",
                "_score": 1,
                "_source": {
                   "name": "type2 doc1"
                }
             }
          ]
       }
    }
    

    这是我使用的代码:

    http://sense.qbox.io/gist/a8085b57c22631148dd4c67769307caf6425fd95

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 2015-04-12
      • 2021-08-30
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2012-03-26
      • 2015-03-20
      相关资源
      最近更新 更多