【问题标题】:Elastic Search nested multimatch queryElastic Search 嵌套多匹配查询
【发布时间】:2016-03-23 21:45:33
【问题描述】:

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

我的映射:

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

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

【问题讨论】:

标签: elasticsearch nested match nested-properties


【解决方案1】:

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

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

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

【讨论】:

  • 根据elastic.co/guide/en/elasticsearch/guide/current/…,您将不得不将嵌套的多重匹配查询重写为几个简单的嵌套查询,并添加boost 参数。因此,在每个嵌套多匹配只有一个字段的简单情况下,只需将 boost 参数添加到与 query 相同的级别,如下所示:
  • "nested": { "path": "authors", "query": { "multi_match": { "query": "higgs", "fields": ["last_name"] } }, "boost": 2}
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 2021-02-20
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多