elasticsearch嵌套functionScoreQuery无法访问父属性

时间:2015-11-16 11:25:34

标签: elasticsearch

我有一个弹性搜索类型,如下所示:

    "hotel" : {    
         "field" : 1,    
         "rooms" : [
            {
           "type" : "single",
           "magicScore" : 1
            }, 
            {
            "type" : "double",
            "magicScore" : 2
            }
        ] 
  }

其中房间是嵌套类型。我使用嵌套的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"
        }
      }
   }
}

问题是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"
            }
          } ]
        }
      }
    }
  }
}

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

1 个答案:

答案 0 :(得分:2)

问题是为什么要访问nested查询中的父值。进入nested上下文后,您无法访问其他nested字段中的父字段或其他字段。

来自the documentation

  

嵌套子句“降低”到嵌套注释字段中。它不再能够访问根文档中的字段,也不能访问任何其他嵌套文档中的字段。

因此,重写您的查询,以便nested部分触及该nested字段中的字段,其他任何内容都在外部 nested部分。< / p>

相关问题