【问题标题】:Is it possible to filter records of one index from another index in elasticsearch query?是否可以在弹性搜索查询中从另一个索引中过滤一个索引的记录?
【发布时间】:2019-12-03 11:36:42
【问题描述】:

假设我在 index1 中有 100 条记录,在 index2 中有 10 条记录。我想变成这样:

select * from index1 where id not in (select id from index2)

我们能否在 elasticsearch 中使用上述相同的查询,或者甚至有可能。

我尝试了多个索引搜索并且它正在工作,但是我不知道过滤器部分。我正在使用 elasticsearch-7.3.0

请推荐!

我尝试过以下查询:

GET index1/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "terms": {
        "id.keyword": {
            "index" : "index2",
            "id" : "_all",
            "path" : "id"
        }
    }
        }
      ]
    } 

  }
}

【问题讨论】:

  • I have tried multiple index search and it is working。请分享您到目前为止尝试过的内容。
  • @Soumendra:我尝试过更新查询,它只适用于 1 个或某些 id。我需要从另一个索引中过滤掉大约数百万个 ID。更新了我久经考验的查询。

标签: elasticsearch elastic-stack kibana-7


【解决方案1】:

子查询不被ES支持。你可以使用两个查询来达到结果。

查询 1: 从 index 2

获取所有 id 值
    POST index2/_search
    {
      "_source": [
        "id"
      ]
    }

查询 2: 获取 index 1 with != conditions

中的所有记录
POST index1/_search
{
  "query": {

    "bool": {

      "must_not": [
        {
          "terms": {
            "id": [
              "val1",
              "val2"
            ]
          }

        }
      ]

    }
  }
}

【讨论】:

  • 感谢您的回复,只是想在您的第二个查询中了解如何从索引 index1 中识别“id”,我相信这些 id 仅来自 index2。我认为过滤仍然没有发生,如果我错了,请纠正我:"must_not": [{"terms": {"id": ["val1","val2"]}}]
  • 我更改了索引名称。在第一个查询中,我们从索引 2 中找到所有 id,然后我们使用第二个查询通过条件运算符从索引 1 中获取所有记录(第一个查询索引 2 结果,即 id值)
  • 好的。那么我将如何执行查询。同时或一个一个,因为在 index2 中我有数百万个 id。我不能手动输入每个 id :(。请建议
  • 你可以一个一个的运行查询,但是数百万的id我们不能手动完成。所以使用python或node js编写一些自动脚本来运行查询。数以百万计的 id 使您的查询非常缓慢,您的应用程序将崩溃。
猜你喜欢
  • 2016-03-13
  • 2020-02-02
  • 2017-11-25
  • 1970-01-01
  • 2014-06-22
  • 2014-01-06
  • 2018-06-22
  • 2011-07-12
  • 1970-01-01
相关资源
最近更新 更多