您好我有一个嵌套的文档结构
"question":{
"questionId":{"type":"integer"},
"userId":{"type":"integer"},
"action": {
"type":"nested",
"properties": {
"created": {"type": "date"},
"interactionType": {"type": "text"}, //values can be answer,upvote etc
"userId": {"type": "integer"},
"score":{"type":"long"}
}
....//other properties
}
现在我想对几个功能进行评分。其中一个功能是:
实际查询稍微复杂一些。但基本上我喜欢看的是 这个查询应该返回val =(所有/脚本过滤的)嵌套结构的得分总和。
{
"query": {"function_score": {
"query": {"bool": {"should": [
{"function_score": {"boost_mode": "max",
"query": {"terms": {
"questionId": [
1,2,3,4,5,6,7,8,9,10
]
}},
"functions": [
{"weight": 0}
]
}},{
"nested": {
"path": "action",
"query": {"bool": {"must": [
{
"function_score": {
"query": {"script": {
"script": "doc['userId'].value != doc['action.userId'].value"
}},
"functions": [
{"weight": 0}
]
}
},{
"function_score": {
"query": {"bool": {"must": [
{"terms": {
"action.userId": [
1100,1200,1300,1400,1500,1600,1700,1800
]
}}
]}},
"functions": [
{"script_score": {
"script": {
"inline": "double val=0; for(item in doc['action.score']){ val+=score;} return val;"
}
}}
]
}}]}}
}
}
]}},
"functions": [{
"weight": 0}
],"boost_mode": "sum","score_mode":"sum"
}}
}
For eg.
question:{
questionId:1,
userId:1,
action:[{
created:null,
interactionType: "answer",
userId:1100,
score:100},
{
created:null,
interactionType: "upvote",
userId:1200,
score:10
}]
应该返回val = 200。但它实际上返回100.
这可能是由于每个嵌套查询分别执行的原因。你能建议一个实现这个目标的方法吗?
请不要建议我在问题级别对得分进行索引。我将增强脚本以过滤掉脚本得分内联方法中的某些嵌套文档或其他复杂性。
答案 0 :(得分:0)
在嵌套级别名称分数模式下还有一个属性,默认情况下为avg,但可以将其更改为总和以满足我的需求。