【发布时间】:2019-07-02 18:34:12
【问题描述】:
我正在尝试使用来自 ElasticSearch 6 结果的数据来为我的结果设置评分。
我的部分映射如下所示:
{
"properties": {
"annotation_date": {
"type": "date"
},
"annotation_date_time": {
"type": "date"
},
"annotations": {
"properties": {
"details": {
"type": "nested",
"properties": {
"filter": {
"type": "text",
"fielddata": True,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"bucket": {
"type": "text",
"fielddata": True,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"keyword": {
"type": "text",
"fielddata": True,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"frequency": {
"type": "long",
}
}
}
}
}
}
}
文档 JSON 的示例部分:
"annotations": {
"details": [
{
"filter": "filter_A",
"bucket": "bucket_A",
"keyword": "keyword_A",
"frequency": 6
},
{
"filter": "filter_B",
"bucket": "bucket_B",
"keyword": "keyword_B",
"frequency": 7
}
]
如果我的 annotation.details 命中某个“桶”,我想使用它的频率,我尝试使用以下方法:
GET my_index/_search
{
"size": 10000,
"query": {
"function_score": {
"query": {
"match": { "title": "<search term>" }
},
"script_score": {
"script": {
"lang": "painless",
"source": """
int score = 0;
for (int i = 0; i < doc['annotations.details.filter'].length; i++){
if (doc['annotations.details.filter'][i].keyword == "bucket_A"){
score += doc['annotations.details.frequency'][i].value;
}
}
return score;
"""
}
}
}
}
}
最终,这意味着在这种特定情况下,预期得分为 6。如果它会击中更多的桶,则分数会随着它击中的频率而增加。
【问题讨论】:
-
你能解决这个问题吗?因为我在使用您提到的 for 循环访问嵌套对象时遇到问题。
标签: python elasticsearch kibana elasticsearch-painless