【发布时间】:2017-08-22 22:23:28
【问题描述】:
您好,我有一个嵌套的文档结构
"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。
这可能是由于每个嵌套查询都单独执行的原因。
您能否建议一种方法来实现这一目标?
请不要建议我在问题级别索引分数,因为我将增强脚本以过滤掉脚本分数内联方法中的某些嵌套文档或其他复杂性。
【问题讨论】:
-
分数是嵌套文档数的平均值
标签: elasticsearch lucene nested