我有一个分析字段,例如,将其命名为“座右铭”。我想全文搜索“生活”并按计数进行汇总。

...
"query":{
    "term":{
        "motto":"life"
    }
},
"aggs": {
    "match_count": {
        "terms": "motto"
    }
}
...

我想要的结果是:
...
{
    ...
    "buckets": [
        {
            "key":"life is good",
            "doc_count":3
        }
    ]
    ...
}
...

结果实际上是:
{
    ...
    "buckets": [
        {
            "key": "life",
            "doc_count": 3
        },
        {
            "key": "good",
            "doc_count": 3
        },
        {
            "key": "is",
            "doc_count": 3
        }
    ]
    ...
}

如何按照自己的方式汇总它们?

最佳答案

您可以做的是在not_analyzed字段中创建一个motto子字段,如下所示:

curl -XPUT localhost:9200/your_index/your_type/_mapping -d '{
    "your_type": {
        "properties": {
            "motto": {
                "type": "string",
                "fields": {
                    "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}'

完成后,您需要重新索引数据以填充motto.raw子字段。

最后,您将能够运行这样的查询,即在motto上进行搜索,但在motto.raw上进行聚合:
...
"query":{
    "term":{
        "motto":"life"
    }
},
"aggs": {
    "match_count": {
        "terms": { "field": "motto.raw" }
    }
}
...

10-06 14:08
查看更多