ElasticSearch 中的父子关系 - 在所有子文档中搜索一个句子

Parent child relationship in ElasticSearch - search for a sentence in all the child docs combined

我是 Elastic Search 的超级新手。我有一个用例,似乎可以通过父子关系来解决。父文档包含非政府组织的描述。儿童文档包含发送给非政府组织的各种反馈。

Parent Doc structure
{
    name
    address
    description
}

Child doc
{
    feedbackContent
}

比方说,NGO-A 4 个反馈(意味着 4 个子文档)

另一个 NGO-B 有 2 个反馈(意味着 2 个子文件)

客户端应该能够查找到所有查询字符串中的术语都已通过的非政府组织。示例 - 客户搜索 "best" 和 "location".

由于 best 存在于 child1 和 child2 中,location 存在于 child 4 中,因此 NGO-A 是有效输出。但是,对于 NGO-B,child2 包含一个搜索词,而另一个搜索词不存在于任何其他子文档中,因此 NGO-B 不是有效结果。

我阅读了文档 - https://blog.mimacom.com/parent-child-elasticsearch/ 非常好,但无法得出是否可以完成的结论。

我试过的例子

PUT message_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "mapping.single_type": true
  },
  "mappings": {
    "doc": {
      "properties": {
        "ngo": {"type": "text"},
        "feedback": {"type": "text"},
        "ngo_relations": {
          "type": "join",
          "relations": {
            "ngo": "feedback"
          }
        }
      }
    }
  }
}

POST message_index/doc/_bulk
{"index": {"_id":1}}
{"name":"teach for india", "ngo_relations": {"name":"ngo"}}
{"index":{"_id":2}}
{"name":"hope for autism", "ngo_relations": {"name":"ngo"}}

PUT message_index/doc/3?routing=1
{"feedback":"best food","ngo_relations":{"name":"feedback", "parent":1}}

PUT message_index/doc/4?routing=1
{"feedback":"average location","ngo_relations":{"name":"feedback", "parent":1}}

PUT message_index/doc/5?routing=1
{"feedback":"awesome staff","ngo_relations":{"name":"feedback", "parent":1}}

PUT message_index/doc/6?routing=2
{"feedback":"best teachers","ngo_relations":{"name":"feedback", "parent":2}}

PUT message_index/doc/7?routing=2
{"feedback":"awesome overload","ngo_relations":{"name":"feedback", "parent":2}}

为了最佳和位置搜索,应该返回为印度非政府组织教授。

没有命中:

GET message_index/_search
{
  "query": {
    "has_child": {
      "type": "feedback",
      "query": {
        "bool": {
          "must": {
            "term": {"feedback": "best"}
          },
          "must": {
            "term": {"feedback": "location"}
          }
        }
      }
    }
  }
}

两份文件都已返回

GET message_index/_search
{
  "query": {
    "has_child": {
      "type": "feedback",
      "query": {
        "bool": {
          "should": {
            "term": {"feedback": "best"}
          },
          "should": {
            "term": {"feedback": "location"}
          }
        }
      }
    }
  }
}

这是可以做到的。您只是查询中的一个小错误而已。

在您的 child 查询中,您正在执行一个包含两个 must/should 的布尔值。因此,您的查询是:给我所有文档,使它们具有 child 使得 child 具有(或 'one of the' 在应该的情况下)术语 "best" 和"location".

然而,您想要的是:给我所有文档,使它们具有 child,使得 child 具有术语 "best",并且还具有 child 使得 child 具有术语 "location".

按如下方式调整您的查询:

GET message_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "feedback",
            "query": {
              "term": {
                "feedback": "best"
              }
            }
          }
        },
        {
          "has_child": {
            "type": "feedback",
            "query": {
              "term": {
                "feedback": "location"
              }
            }
          }
        }
      ]
    }
  }
}