首页 > 解决方案 > 在一个索引中通过 1-N 个相关数据的多个字段进行弹性搜索

问题描述

我已经被这个问题困扰了几天,反复阅读 Elastic 文档无济于事。我希望有人能以正确的方式推动我。

假设有两种类型的数据 - 父母和孩子,它们具有 1-N 关系。我使用自定义type字段将它们存储在同一个索引中。他们有一些共同的领域和一些不同的领域。举一个具体的例子,索引和一些输入数据可能看起来像:

PUT /myindex
{
  "mappings":
  {
    "properties": {
      "type": {"type": "keyword"},
      "fileid" : {"type": "keyword"},
      "dirid": {"type": "keyword"},
      "filename": {"type": "text"},
      "fileauthor": {"type": "text"},
      "dirpath": {"type": "text"}
    }
  }
}
PUT /myindex/_doc/1
{
  "type": "dir",
  "dirid": 1,
  "dirpath": "/home/jd/"
}
PUT /myindex/_doc/2
{
  "type": "dir",
  "dirid": 2,
  "dirpath": "/home/jd/Documents/CV/"
}
PUT /myindex/_doc/3
{
  "type": "file",
  "dirid": 2,
  "fileid": 1,
  "filename": "My-Resume.pdf",
  "fileauthor": "John Doe"
}
PUT /myindex/_doc/4
{
  "type": "file",
  "dirid": 1,
  "fileid": 2,
  "filename": "Some-CV.pdf",
  "fileauthor": "John Doe"
}
PUT /myindex/_doc/5
{
  "type": "file",
  "dirid": 1,
  "fileid": 3,
  "filename": "book.pdf",
  "fileauthor": "John Doe"
}

现在我想使用相当复杂的(多词)查询来搜索文本字段,例如John Doe CV. 对于个人type="dir"type="file"它很简单(查询将返回带有 的文档_id=4),但我也想获得“混合”结果,其中部分查询包含在父特定字段中,其余部分包含在子特定字段中。在我的示例数据和查询中,我希望它也返回_id=3,因为作者是John Doe并且 parentdirpath包含关键字CV

我很高兴得到父母,这足以满足我的需要。(我确实更希望得到父母,但这是我目前遇到的问题之上的事情。)

不知何故,我还没有找到一种方法来做到这一点。我知道这种join类型,但这似乎没有帮助,相反。随着以前推荐的做法被弃用,许多建议、SO 问题和文档项目都已过时。

当然,我可以用所有数据填充每个条目(每个条目type="file"只会复制父级的dirpath值),但这违背了我对内存和空间最佳使用的所有看法。

请问有谁知道在哪里可以看的更远吗?非常感谢。

标签: elasticsearchrelationshipparent-child

解决方案


推荐阅读