带有top_hits聚合的Elasticsearch null_pointer_exception

时间:2019-01-21 11:20:29

标签: elasticsearch

当在子项聚合中的嵌套术语聚合中有嵌套的top_hits聚合时,我得到了null_pointer_exception。我希望得到一个有效的答复。

复制步骤:

创建映射

PUT http://localhost:9200/test

{
    "mappings": {
        "doc": {
            "properties": {
                "docType": {
                    "type": "text"
                },
                "userId": {
                    "type": "long"
                },
                "userName": {
                    "type": "text"
                },
                "title": {
                    "type": "text"
                },
                "joinField": {
                    "type": "join",
                    "relations": {
                        "post": "comment"
                    }
                }
            }
        }
    }
}

插入示例帖子

PUT http://localhost:9200/test/doc/1
{
    "joinField": {
        "name": "post"
    },
    "docType": "post",
    "title": "Example Post"
}

插入评论

PUT http://localhost:9200/test/doc/2?routing=1

{
    "joinField": {
        "name": "comment",
        "parent": "1"
    },
    "userId": 22,
    "userName": "John Doe",
    "title": "Random comment",
    "docType": "comment"
}

执行搜索

POST http://localhost:9200/test/doc/_search

{
    "aggs": {
        "to-comment": {
            "children": {
                "type": "comment"
            },
            "aggs": {
                "by-user": {
                    "terms": {
                        "field": "userId"
                    },
                    "aggs": {
                        "data": {
                            "top_hits": {
                                "size": 1
                            }
                        }
                    }   
                }
            }
        }
    },
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "docType": "post"
                    }
                }
            ]
        }
    }
}

响应:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 4,
        "skipped": 0,
        "failed": 1,
        "failures": [
            {
                "shard": 3,
                "index": "test",
                "node": "0RbF1bIbRO-yN5C1m-HXPA",
                "reason": {
                    "type": "null_pointer_exception",
                    "reason": null
                }
            }
        ]
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "to-comment": {
            "doc_count": 0,
            "by-user": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": []
            }
        }
    }
}

如果我删除查询,它会起作用,但是在实际的匹配中,我只想获取所有帖子。如果我删除了术语聚合,但我想通过其他查询(例如标题匹配)来过滤帖子,也可以使用。

1 个答案:

答案 0 :(得分:0)

这似乎是弹性搜索的错误。该错误已报告,有望很快修复(https://github.com/elastic/elasticsearch/issues/37650)。

如果您对如何建立类似的聚合有其他解决方案,请告诉我。

编辑:您可以使用轻松的脚本语言来解决:

"script": {
  "lang": "painless",
  "source": "params._source.userName"
}