ElasticSearch 多字段不工作

ElasticSearch multifield not working

在我的文档中,我有一个字段 collaboration,我想对其进行聚合查询。但是我也希望它可以全文搜索,所以我想我应该把它做成一个多字段。该字段可能看起来像这样:

...
"collaboration" : "CMS"
or
"collaboration" : ["ATLAS", "CMS"]
or
"collaboration" : "LHCb"
...

遵循此建议:ElasticSearch term aggregation 我将映射更改为:

"collaboration": {
    "type": "string",
    "fields": {
        "raw": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
},

而我运行一个查询:

POST /my_index/_search
{
   "aggs": {
        "collaboration": {
            "terms": {
                "field": "collaboration.raw"
            }
        }
    }
}

一无所获:

"hits": {
   "total": 5,
   "max_score": 1,
   "hits": [...]
},
"aggregations": {
   "collaboration": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
   }
}

即使我尝试使用此字段进行搜索,它也不起作用:

POST /my_index/_search
{
    "query": {
        "query_string": {
            "query": "CMS",
            "fields": ["collaboration.raw"]
        }
    }
}

我是否应该以某种方式更改映射,因为该字段有时是一个列表,有时是一个字符串?我的研究发现 arrays are supposed to be supported out of the box。有什么建议吗?

感谢 Andrei Stefan!

解决了这个问题

更改映射后需要重新索引所有文档,小而痛苦的细节。