1

假设我在 index1 中有 100 条记录,在 index2 中有 10 条记录。我想得到这样的:

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

我们是否可以在弹性搜索中进行相同的上述查询,或者甚至有可能。

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

请建议!

下面的查询我试过:

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

  }
}
4

1 回答 1

0

ES不支持子查询。您可以使用两个查询来获得结果。

查询 1:索引 2中获取所有 id 值

    POST index2/_search
    {
      "_source": [
        "id"
      ]
    }

查询 2:使用 != 条件获取索引 1 中的所有记录

POST index1/_search
{
  "query": {

    "bool": {

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

        }
      ]

    }
  }
}
于 2019-12-03T13:01:23.293 回答