1

我在弹性搜索中有一个问题,我无法解决:

我在索引中有两种不同的类型,一种称为“关键字”,另一种称为“产品”。关键字具有字符串属性“keyword_content”,产品具有文本属性“content”。

我需要查询所有关键字及其前 N 个相关产品。相关关系由标准查询定义

我知道我在 ElasticSearch 中有父子功能,但是当我插入关键字时,我事先并不知道相关产品(唯一的方法是在插入关键字之前,对每个关键字使用搜索查询以了解哪些产品是相关的,并用正确的产品索引关键字,但这意味着在插入之前查询每个关键字,这是昂贵的)。

让我们假设我在 ElasticSearch 中已经有了两个 UNRELATED 类型。我如何查询所有关键字及其关联的 N 个相关产品。我正在寻找一种“动态字段”,它是使用每个文档的“keyword_content”属性作为查询字符串对不同类型进行查询的结果。

我想要类似的东西:

关键字文档示例:

{ "_id": 1,
  "keyword_content": "happy dogs"
}
{ "_id": 2, 
"keyword_content": "spaceship"
}

产品文件示例

{ "_id": "P1", "content": "This is a collar for happy dogs"}
{ "_id": "P2", "content": "This is a collar for angry dogs"}
{ "_id": "P3", "content": "This is a spaceship"}

我想要这样的东西(我正在简化elasticsearch格式以更好地解释我的用例)(不是说这些类型不相关,或者我可以将它们关联起来,但我无法指定哪些keyowrds与哪个产品相关,我希望elasticSearch告诉我):

[
    {
         "_id": 1,
        "keyword_content": "happy dogs"
        "related_products": [
            { "_id": "P1", "content": "This is a collar for happy dogs"},
            { "_id": "P2", "content": "This is a collar for angry dogs"}
        ]
    },
    {
         "_id": 2,
        "keyword_content": "spaceship"
        "related_products": [
            { "_id": "P3", "content": "This is a spaceship"}
        ]
    }
]
4

0 回答 0