【问题标题】:Searching all fields in a nested object in ElasticSearch在 ElasticSearch 中搜索嵌套对象中的所有字段
【发布时间】:2014-02-16 03:00:30
【问题描述】:

假设我有一个如下所示的记录:

{
    "title": "Random title",
    "authors": [
        {
            "first": "Jordan",
            "last": "Reiter"
            … more fields here …
        },
        {
            "first": "Joe",
            "last": "Schmoe"
            … more fields here …
        },
    ] 
}

我希望用户能够按作者搜索记录,如果他们输入完整的查询 Jordan Reiter,它会提取该记录(以及至少有一位作者与这些字段匹配的其他记录。

我不希望用户必须指定第一个/最后一个字段,我不一定知道他们是否在搜索 Jordan Reiter Reiter Jordan 甚至 J Reiter 所以理想情况下我想匹配所有这些案例中的一个,同时仍然对其进行过滤,例如,这些记录都不匹配:

{
    "title": "About Jordan Reiter",
    "authors": [
        {
            "first": "Susan",
            "last": "Schmusan"
            … more fields here …
        }
    ] 
}


{
    "title": "Completely different authors",
    "authors": [
        {
            "first": "Robert",
            "last": "Jordan"
            … more fields here …
        },
        {
            "first": "Samuel",
            "last": "Reiter"
            … more fields here …
        },
    ] 
}

刚开始使用 ElasticSearch,所以如果我缺少基本概念,请告诉我。

我打算让 authors 字段成为嵌套类型。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    第一个和最后一个多匹配查询,我猜: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html(可能带有短语前缀子句) 包装到嵌套查询中: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

    您必须提供一个映射,明确指定您的“作者”是嵌套类型: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html

    如果您不将作者映射为嵌套类型,则 ElasticSearch 将在内部创建一个包含所有“first”值的长列表和一个包含所有“last”值的长列表,因此您将无法匹配组合特定的“第一个”和“最后一个”字段。

    【讨论】:

    • 是的,这样就可以了。我没有意识到您可以搜索字段列表。但是,我将其作为嵌套查询搜索进行。改编自这里的答案:stackoverflow.com/a/17937816/255918
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多