23

所以我的问题与这里描述的基本相同,但是在小组中仍然没有答案。

我的映射:

{
    "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"]
                }
            }]
        }
    }
}

由于其嵌套映射,我没有从作者字段中得到任何结果。我也无法摆脱嵌套属性 - 我将它用于聚合。任何优雅的想法如何解决它?

4

2 回答 2

14

我设法解决的唯一解决方案是这样的查询,既不方便也不优雅,但不知何故有效:

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

我也不确定提升是否会按预期工作,前提是它是在不同的查询中设置的。任何建议表示赞赏。

于 2015-08-05T10:25:45.527 回答
13

将您的映射更改为以下使用的映射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 来实现。-链接

注意:include_in_root可能会在未来版本的 elasticsearch 中被弃用,以支持copy_to.

于 2016-03-23T13:45:33.220 回答