弹性搜索嵌套多匹配查询

26

我的问题基本上与这里描述的相同,但是在该讨论组中仍未得到答案。

我的映射:

{
    "abstract": {
        "properties": {
            "summary": {
                "type": "string"
            }
        }
    },
    "authors": {
        "type": "nested",
        "properties": {
            "first_name": {
                "type": "string"
            },
            "last_name": {
                 "type": "string"
            }
        }
    }
}
我想在这两个字段上执行全文搜索,可能是不均匀加权的。我能想到的查询语句是这样的,但不幸的是它不起作用:
{
    "query": {
        "bool": {
            "should": [{
                "multi_match": {
                    "query": "higgs boson",
                    "fields": ["abstract.summary^5", "author.last_name^2"]
                }
            }]
        }
    }
}

由于作者字段的嵌套映射,我没有得到任何结果。但我也无法摆脱嵌套属性 - 我需要它进行聚合。有没有什么优雅的想法来解决这个问题?


在文档映射中,我没有看到这两个对象之间的链接。我认为如果您正在使用“嵌套”对象,则需要将其映射为子对象,在“属性”内,并指定一个“嵌套查询”。https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html - Jaider
2个回答

15

我设法想出的唯一解决方案虽然不太方便也不太优雅,但却可以解决问题。查询如下:

"query": {
    "bool": {
        "should": [
            {
                "nested": {
                    "path": "authors",
                    "query": {
                        "multi_match": {
                            "query": "higgs",
                            "fields": ["last_name^2"]
                        }
                    }
                } 
            },
            {
                "multi_match": {
                    "query": "higgs",
                    "fields": ["abstract.summary^5"]
                }
            }
        ]
    }
}

我也不确定增强会不会按预期工作,如果设置在不同的查询中。欢迎任何建议。


根据 https://www.elastic.co/guide/en/elasticsearch/guide/current/_boosting_query_clauses.html,您需要将嵌套的多重匹配查询重写为几个简单的嵌套查询,并添加 boost 参数。因此,在仅有一个字段的情况下,只需在与查询相同级别的位置添加 boost 参数,如下所示: - ulkas
"nested": { "path": "authors", "query": { "multi_match": { "query": "higgs", "fields": ["last_name"] } }, "boost": 2} - ulkas

14

将您的映射更改为以下使用 include_in_root: true 的方式,将允许您使用原始编写的查询:

{
    "abstract": {
        "properties": {
            "summary": {
                "type": "string"
            }
        }
    },
    "authors": {
        "type": "nested",
        "include_in_root": true,
        "properties": {
            "first_name": {
                "type": "string"
            },
            "last_name": {
                 "type": "string"
            }
        }
    }
}

您可能希望将内部对象作为嵌套字段和扁平化对象字段进行索引。这可以通过将include_in_parent设置为true来实现。-链接

注意:在未来的Elasticsearch版本中,include_in_root可能会被废弃,取而代之的是copy_to


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接