嵌套字段上的聚合,嵌套字段属性上的 fielddata=true

Aggregation on nested field, fielddata=true on nested field attribute

我有 json 这种格式

{
"id":1,
"name":"CBG",
"studDetails":{
"address":"Banglore"
}
}

尝试聚合 studDetails.address 出现此错误

Fielddata is disabled on text fields by default. Set fielddata=true on [studDetails.address].

尝试在 curl 命令下设置为 true,但不起作用。

curl -XPUT -H "Content-Type: application/json" http://localhost:9200/student -d '{
  "mappings": {
    "type": {
      "properties": {
        "studDetails.address": {
          "type": "keyword",
          "fielddata": true
        }
      }
    }
  }
}'

抱歉,我在聚合中缺少完全限定的路径 api、'student.studDetails.address'

 srb.addAggregation(AggregationBuilders.nested("agg1", "studDetails").
                subAggregation(AggregationBuilders.terms("agg2").field("student.studDetails.address")));

不要使用 fieldata:true 进行聚合,这可能会导致性能问题。

Fielddata 会消耗大量堆 space,尤其是在加载高基数文本字段时。一旦 fielddata 被加载到堆中,它就会在段的生命周期内保留在那里。此外,加载字段数据是一个昂贵的过程,可能会导致用户遇到延迟问题。这就是默认情况下禁用字段数据的原因。

https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

改用关键字。

假设您的映射如下所示:

   "studDetails":{
         "type":"nested",
           "properties":{
               "address":{
                   "type": "text",
                    "fields": {
                       "keyword": {
                         "type": "keyword",
                        "ignore_above": 256
                       }
                   }
               }
           }
       }

那么您的查询应该如下所示:

srb.addAggregation(AggregationBuilders.nested("agg1", 
"studDetails").subAggregation(AggregationBuilders.terms("agg2").field("studDetails.address.keyword")))

希望对您有所帮助