gpt4 book ai didi

elasticsearch - 将嵌套文档中的特定字段作为一个文档进行搜索

转载 作者:行者123 更新时间:2023-12-02 22:31:57 24 4
gpt4 key购买 nike

我有以下结构:

{
"mappings": {
"document": {
"properties": {
"title": {
"type": "string"
},
"paragraphs": {
"type": "nested",
"properties": {
"paragraph": {
"type" : "object",
"properties" : {
"content": { "type": "string"},
"number":{"type":"integer"}
}
}
}
}
}
}
}
}

带有这些样本文件
{
"title":"Dubai seeks cause of massive hotel fire at New Year",
"paragraphs":[
{"paragraph": {"number": "1", "content":"Firefighters managed to subdue the blaze, but part of the Address Downtown Hotel is still smouldering."}},
{"paragraph": {"number": "2", "content":"A BBC reporter says a significant fire is still visible on the 20th floor, where the blaze apparently started."}},
{"paragraph": {"number": "3", "content":"The tower was evacuated and 16 people were hurt. But a fireworks show went ahead at the Burj Khalifa tower nearby."}},
{"paragraph": {"number": "4", "content":"The Burj Khalifa is the world's tallest building and an iconic symbol of the United Arab Emirates (UAE)."}}]
}

{
"title":"Munich not under imminent IS threat",
"paragraphs":[{"paragraph": {"number": "1", "content":"German officials say there is no sign of any imminent terror attack, after an alert that shut down two Munich railway stations on New Year's Eve."}}]
}

我现在可以使用搜索每个段落
{ 
"query": {
"nested": {
"path": "paragraphs", "query": {
"query_string": {
"default_field": "paragraphs.paragraph.content",
"query": "Firefighters AND still"
}
}
}
}
}

问题:如何编写一个查询,该查询仅搜索内容字段中的多个段落?

这可行,但会搜索所有字段
{
"query": {
"query_string": {
"query": "Firefighters AND apparently AND 1"
}
}
}

我要与第1段中的 消防员和显然与第2段中的 匹配。但是,我不希望 1 匹配,因为它不是内容字段。

澄清:第一次搜索针对我想要的段落进行搜索。但是,我确实也希望有时能够搜索整个文档(所有段落)。

解决方案
我添加了“include_in_parent”: https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-nested-type.html中提到的true

最佳答案

您查询的方式是错误的,因为nested documents是单独索引的。请参阅doc的最后一个段落。

您的查询

{
"query": {
"nested": {
"path": "paragraphs",
"query": {
"query_string": {
"default_field": "paragraphs.paragraph.content",
"query": "Firefighters AND apparently"
}
}
}
}
}

在相同的 中查找两个单词,因此您没有得到结果。您需要像这样单独查询它们
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "paragraphs",
"query": {
"match": {
"paragraphs.paragraph.content": "firefighters"
}
}
}
},
{
"nested": {
"path": "paragraphs",
"query": {
"match": {
"paragraphs.paragraph.content": "apparently"
}
}
}
}
]
}
}
}

这将为您提供正确的结果。

作为附带说明,我认为您不需要在段落内添加object datatype。关注也可以
"paragraphs": {
"type": "nested",
"properties": {
"content": {
"type": "string"
},
"number": {
"type": "integer"
}
}
}

希望这可以帮助!!

关于elasticsearch - 将嵌套文档中的特定字段作为一个文档进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34557434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com