elasticsearch 嵌套函数ScoreQuery 无法访问父属性

m14*_*416 5 elasticsearch

我在 elasticsearch 中有一个看起来像这样的类型:

    "hotel" : {    
         "field" : 1,    
         "rooms" : [
            {
           "type" : "single",
           "magicScore" : 1
            }, 
            {
            "type" : "double",
            "magicScore" : 2
            }
        ] 
  }
Run Code Online (Sandbox Code Playgroud)

其中房间是嵌套类型。我使用嵌套的 functionScoreQuery 进行排序:

{
  "query" : {
    "filtered" : {
      "query" : {
        "nested" : {
          "query" : {
            "function_score" : {
              "filter" : {
                "match_all" : { }
              },
              "functions" : [ {
                "script_score" : {
                  "script" : "return doc['hotel.field'].value"      
                }
              } ]
            }
          },
          "path" : "rooms",
          "score_mode" : "max"
        }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

问题是hotel.field 总是返回0。有没有办法访问嵌套查询中的父字段?我知道我总是可以将字段打包在嵌套文档中,但它是一个黑客而不是解决方案。使用 dismax 查询对我有帮助吗?https://discuss.elastic.co/t/nested-value-on-function-score/29935

我实际使用的查询如下所示:

{
  "query" : {
    "bool" : {
      "must" : {
        "nested" : {
          "query" : {
            "function_score" : {
              "query" : {
                "not" : {
                  "query" : {
                    "terms" : {
                      "rooms.type" : [ "single", "triple" ]
                    }
                  }
                }
              },
              "functions" : [ {
                "script_score" : {
                  "script" : {
                    "inline" : "return doc['rooms.magicScore'].value;",
                    "lang" : "groovy",
                    "params" : {
                      "ratings" : {
                        "sample" : 0.5
                      },
                      "variable" : [ 0.0, 0.0, 0.0, 0.0, -0.5, -2.5]
                    }
                  }
                }
              } ],
              "score_mode" : "max"
            }
          },
          "path" : "rooms"
        }
      },
      "filter" : {
        "bool" : {
          "filter" : [ {
            "bool" : {
              "should" : [ {
                "term" : {
                  "cityId" : "166"
                }
              }, {
                "term" : {
                  "cityId" : "165"
                }
              } ]
            }
          }, {
            "nested" : {
              "query" : {
                "not" : {
                  "query" : {
                    "terms" : {
                      "rooms.type" : [ "single", "triple" ]
                    }
                  }
                }
              },
              "path" : "rooms"
            }
          } ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想要实现的是访问例如嵌套的 function_score 查询中的 cityId。

And*_*fan 5

问题是您为什么要访问nested查询中的父值。一旦进入nested上下文,就不能从其他人访问父字段或其他字段nested字段字段。

文档

嵌套子句“向下”进入嵌套的注释字段。它不再可以访问根文档中的字段,也不能访问任何其他嵌套文档中的字段。

因此,重写您的查询,以便该nested部件接触该字段中的nested字段,并且可以访问该部件之外的任何其他内容nested