【问题标题】:Elasticsearch multi index filterElasticsearch 多索引过滤器
【发布时间】:2021-02-04 17:05:12
【问题描述】:

我有 2 个索引,一个存储 用户,一个存储 文章。

用户文档:

{
  "id" : "152ce52d-e975-4ebd-849a-0a12f535e644",
  "email": "bob@email.com",
  ...
}

索引映射:

{
  "mapping": {
    "properties": {
      "id": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "email": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

文章文档:

{
  "articleId" : "002ce52d-e975-4ebd-849a-0a12f535a536",
  "userId": "152ce52d-e975-4ebd-849a-0a12f535e644",
  ...
}

索引映射:

{
  "mapping": {
    "properties": {
      "articleId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "userId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

我正在尝试创建一个多索引查询,它返回所有没有文章的用户。

我不知道如何从第一个索引中排除包含在第二个索引中且具有相同 id/userId 字段的项目。

假设我有以下用户和文章:

用户:

 [
        {
            "id": "user1",
            "email": "bob@email.com"
        },
        {
            "id": "user2",
            "email": "john@email.com"
        },
        {
            "id": "user3",
            "email": "tom@email.com"
        },
        {
            "id": "user4",
            "email": "ben@email.com"
        }
    ]

文章:

[
    {
        "articleId": "id1",
        "userId": "user1"
    },
    {
        "articleId": "id1",
        "userId": "user2"
    }
]

作为查询的结果,我想要所有没有文章的用户:

      [{
            "id": "user3",
            "email": "tom@email.com"
        },
        {
            "id": "user4",
            "email": "ben@email.com"
      }]

【问题讨论】:

  • 您能否分享您的索引映射和示例预期搜索结果?
  • @Bhavya 使用索引映射和预期结果编辑了描述,谢谢!
  • 您想要做的是类似于 SQL 中的 JOIN 查询,这在弹性搜索中不受支持。您将需要两个查询,一个用于获取拥有文章的用户的 ID,另一个用于排除在您的用户索引中具有相同 ID 的用户。另一种选择是非规范化您的数据并只有一个索引。

标签: elasticsearch elasticsearch-5 elasticsearch-aggregation elasticsearch-dsl


【解决方案1】:

正如@leandrojmp 所指出的,您正在尝试在 SQL 中实现类似 JOIN 查询的功能,您希望从一个索引返回所有剩余记录,这些记录与第二个索引中的记录不匹配。请参阅此文档,了解更多关于joining queries in elasticsearch

我不知道如何从包含的第一个索引项中排除 在第二个索引中并具有相同的 id/userId 字段。

您可以在跨多个索引执行查询时使用_index field。

搜索查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must_not": [
              {
                "match": {
                  "id": "user1"
                }
              },
              {
                "match": {
                  "id": "user2"
                }
              }
            ],
            "must": {
              "term": {
                "_index": "64469393user"
              }
            }
          }
        },
       {
          "bool": {
            "must_not": [
              {
                "match": {
                  "userId": "user1"
                }
              },
              {
                "match": {
                  "userId": "user2"
                }
              }
            ],
            "must": {
              "term": {
                "_index": "64469393article"
              }
            }
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "64469393user",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "id": "user3",
          "email": "tom@email.com"
        }
      },
      {
        "_index": "64469393user",
        "_type": "_doc",
        "_id": "4",
        "_score": 1.0,
        "_source": {
          "id": "user4",
          "email": "ben@email.com"
        }
      }
    ]

【讨论】:

  • 谢谢@Bhavya。在这种情况下,我需要事先知道 id 才能将它们添加到查询中。如果查询是“获取所有未写文章的用户,id = id1?
  • @Maucan 感谢您接受我的回答,您能否也为我的回答投票 ? +1 为您的问题 ?。关于您的问题,您必须使用 bool 和 must_not 的某种组合来实现您的用例,但为此,这将是一个完全不同的查询。
  • @Maucan 随意提出一个新问题,您的用例在上面的 cmets 中提到 ?,请不要忘记也为我的答案投票 ?
猜你喜欢
  • 1970-01-01
  • 2020-08-08
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2014-09-08
  • 1970-01-01
相关资源
最近更新 更多