嵌套对象中多个字段的聚合在弹性搜索1.7x中不起作用?

时间:2015-08-20 22:03:02

标签: elasticsearch nested field aggregation

亚马逊索引中的映射如下所示。本书具有嵌套的具有多字段“别名”的作者。 “alias”和“alias.raw”应该是作者的有效字段。

{
    mappings: {
        book: {
            properties: {
                title: {
                    type: "string",
                    fields: {
                        raw: {type: "string", index: "not_analyzed"}
                    }
                },
                authors: {
                    type: "nested",
                    properties: {
                        alias: {
                            type: "string",
                            fields: {
                                raw : {
                                    type: "string",
                                    index: "not_analyzed"
                                }
                            }
                        },
                        alias_raw: {
                            type: "string",
                            index: "not_analyzed"
                        }

                    }
                }
            }
        }
    }
}

示例数据:

{index:{_ id:“1”,_ type:“book”}} {title:“真棒傻瓜”,页数:“100”,作者:[{firstName:“Apollo”,lastName:“Cabrera”,别名:“Mister Awesome”,alias_raw:“Mister Awesome”},{firstName: “Mark”,lastName:“Twain”,别名:“Julius Caesar”,alias_raw:“Julius Caesar”}}}

{index:{_ id:“2”,_ type:“book”}} {title:“Understanding Women”,页数:“100000”,作者:[{firstName:“Megyn”,lastName:“Kelly”,别名:“神奇女侠”,alias_raw:“神奇女侠”},{firstName:“Donald “,lastName:”特朗普“,别名:”Lone Ranger“,alias_raw:”Lone Ranger“}}}

{index:{_ id:“3”,_ type:“book”}} {title:“Snap Chat”,页面:“30”,作者:[{firstName:“Hilary”,lastName:“Clinton”,别名:“Code Zero”,alias_raw:“Code Zero”},{firstName:“Harry “,lastName:”Houdini“,别名:”Abra Cadabra“,alias_raw:”Abra Cadabra“}}}

我的聚合查询看起来像这样......

{
    query: {
        match_all: {}
    },
    aggs: {
        authors: {
            nested: {
                path: "authors"
            },
            aggs: {
                aliases: {
                    terms: {
                        field: "alias.raw"
                    }
                }   
            }
        }
    }
}

当我执行聚合查询时,我根本没有聚合。如果我使用“alias_raw”(这是我添加的另一个单独的字段),我会按预期获得完整的别名......

"aggregations" : {
    "authors" : {
      "doc_count" : 6,
      "aliases" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [ {
          "key" : "Abra Cadabra",
          "doc_count" : 1
        }, {
          "key" : "Code Zero",
          "doc_count" : 1
        }, {
          "key" : "Julius Caesar",
          "doc_count" : 1
        }, {
          "key" : "Lone Ranger",
          "doc_count" : 1
        }, {
          "key" : "Mister Awesome",
          "doc_count" : 1
        }, {
          "key" : "Wonder Woman",
          "doc_count" : 1
        } ]
      }
    }
  }

还有另一种方法来获取聚合多字段的“alias.raw”吗?如果我只使用“alias”,我会得到解析的,标记化的,分析的小写字段。我想要未经分析的原始别名。我真的不认为我需要也不想要冗余的“alias_raw”字段。

提前致谢:)

1 个答案:

答案 0 :(得分:2)

想出来。您必须使用字段名称的完整路径...

字段:“authors.alias.raw”

{
    query: {
        match_all: {}
    },
    aggs: {
        authors: {
            nested: {
                path: "authors"
            },
            aggs: {
                aliases: {
                    terms: {
                        field: "authors.alias.raw"
                    }
                }   
            }
        }
    }
}

Owe right:)