我有一个Elasticsearch文档结构,我想为其提供一个词方面(或凝集),为此我可以独立于它们出现的字段来获取文档数量。
例如,以下结果同时显示文档和分面搜索结果:
{
"_shards": {
"failed": 0, "successful": 5, "total": 5
},
"hits": {
"hits": [
{
"_id": "003", "_index": "test", "_score": 1.0, "_type": "test",
"_source": {
"root": {
"content": [
"five",
"five",
"five"
],
"title": "four"
}
}
},
{
"_id": "002", "_index": "test", "_score": 1.0, "_type": "test",
"_source": {
"root": {
"content": "two three",
"title": "three"
}
}
},
{
"_id": "001", "_index": "test", "_score": 1.0, "_type": "test",
"_source": {
"root": {
"content": "one two",
"title": "one"
}
}
}
],
"max_score": 1.0, "total": 3
},
"facets": {
"terms": {
"_type": "terms", "missing": 0, "other": 0,
"terms": [
{
"count": 2,
"term": "two"
},
{
"count": 2,
"term": "three"
},
{
"count": 2,
"term": "one"
},
{
"count": 1,
"term": "four"
},
{
"count": 1,
"term": "five"
}
],
"total": 8
}
},
"timed_out": false,
"took": 18,
}
我们可以看到,术语“一个”和“三个”的计数为2(同一文档的每个字段一次),我希望它们的计数为1。唯一的计数为2的术语应为“二”。
我调查了聚合,看是否有帮助,但似乎不适用于多个字段(或者我错过了一些东西)。
在“根”上而不是在各个字段上构建“术语”构面会很好,但是似乎也不可能。
有什么想法,如何解决?
最佳答案
您可以在聚合中使用脚本来实现此目的。
在脚本内部,从两个字段中收集 token ,执行集合联合操作,然后返回集合。
{
"aggs" : {
"genders" : {
"terms" : {
"script" : "union(doc['content'].values, doc['title'].values) "
}
}
}
}
您需要查看如何使用用作脚本语言的任何一种语言来应用联合操作。
关于elasticsearch - Elasticsearch:构面或聚合返回多个字段上的文档数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24012182/