如果指定现有字段,如何聚合字段
How to aggregate field if specify field existing
我有这样的文件
{
"_index": "message_2017-08",
"_type": "comment",
"_id": "c000001",
"_source": {
"companies": {
"58f99f99ba63d10006291b59": {
"id": "58f99f99ba63d10006291b59",
"sentiment": "58f8898273fabd33ee09d317"
}
},
"sentiment": "58f8898273fabd33ee09d319",
"purpose": "58f8898373fabd33ee09d31a"
}
},
{
"_index": "message_2017-08",
"_type": "comment",
"_id": "c000002",
"_source": {
"companies": {
"58f99f99ba63d10006291b59": {
"id": "58f99f99ba63d10006291b59"
}
},
"sentiment": "58f8898273fabd33ee09d317",
"purpose": "58f8898373fabd33ee09d31a"
}
},
{
"_index": "message_2017-08",
"_type": "comment",
"_id": "c000003",
"_source": {
"sentiment": "58f8898273fabd33ee09d318",
"purpose": "58f8898373fabd33ee09d31a"
}
}
我想汇总文档以防万一,
if _source.companies.{company_id}.sentiment
现有使用此字段
如果不用_source.sentiment
代替
我尝试像这样聚合,但出现关于 null_pointer_exception
的错误
"aggs": {
"by_sentiment": {
"terms": {
"script": {
"inline": "if(params['_source']['companies']['58f99f99ba63d10006291b59']['sentiment'].empty == 'true'){return params['_source']['sentiment']} else {return params['_source']['companies']['58f99f99ba63d10006291b59']['sentiment']}"
}
}
}
}
有人知道吗?如何检查脚本中存在的字段?
我猜如果路径中的某个元素丢失,params['_source']['companies']['58f99f99ba63d10006291b59']['sentiment']
将为 null,因此调用 isEmpty 会抛出 NPE。
在对路径中的每个元素调用 isEmpty 之前尝试检查是否为 null。
我有这样的文件
{
"_index": "message_2017-08",
"_type": "comment",
"_id": "c000001",
"_source": {
"companies": {
"58f99f99ba63d10006291b59": {
"id": "58f99f99ba63d10006291b59",
"sentiment": "58f8898273fabd33ee09d317"
}
},
"sentiment": "58f8898273fabd33ee09d319",
"purpose": "58f8898373fabd33ee09d31a"
}
},
{
"_index": "message_2017-08",
"_type": "comment",
"_id": "c000002",
"_source": {
"companies": {
"58f99f99ba63d10006291b59": {
"id": "58f99f99ba63d10006291b59"
}
},
"sentiment": "58f8898273fabd33ee09d317",
"purpose": "58f8898373fabd33ee09d31a"
}
},
{
"_index": "message_2017-08",
"_type": "comment",
"_id": "c000003",
"_source": {
"sentiment": "58f8898273fabd33ee09d318",
"purpose": "58f8898373fabd33ee09d31a"
}
}
我想汇总文档以防万一,
if
_source.companies.{company_id}.sentiment
现有使用此字段如果不用
_source.sentiment
代替
我尝试像这样聚合,但出现关于 null_pointer_exception
"aggs": {
"by_sentiment": {
"terms": {
"script": {
"inline": "if(params['_source']['companies']['58f99f99ba63d10006291b59']['sentiment'].empty == 'true'){return params['_source']['sentiment']} else {return params['_source']['companies']['58f99f99ba63d10006291b59']['sentiment']}"
}
}
}
}
有人知道吗?如何检查脚本中存在的字段?
我猜如果路径中的某个元素丢失,params['_source']['companies']['58f99f99ba63d10006291b59']['sentiment']
将为 null,因此调用 isEmpty 会抛出 NPE。
在对路径中的每个元素调用 isEmpty 之前尝试检查是否为 null。