【问题标题】:elasticsearch nested functionScoreQuery cannot access parent propertieselasticsearch嵌套functionScoreQuery无法访问父属性
【发布时间】:2015-11-16 11:25:33
【问题描述】:

我在 elasticsearch 中有一个如下所示的类型:

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

其中房间是嵌套类型。我使用嵌套函数ScoreQuery 进行排序:

{
  "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。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    问题是您为什么要访问 nested 查询中的父值。一旦您处于nested 上下文中,您就无法从其他nested 字段访问父字段或其他字段。

    来自the documentation

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

    因此,重写您的查询,使nested 部分触及nested 字段中的字段,并且在nested 部分外部访问其他任何内容。

    【讨论】:

    • 您好,我该如何评分?我需要将嵌套函数得分查询和父字段查询的得分相加。
    • 请发布您实际使用的查询。您发布的似乎只是一个示例。
    • 嗨,我编辑了问题以添加我实际使用的查询
    • @m1416 所以,在脚本中你需要rooms.magicScore(即nestedAND cityId 不是nested
    • 正确,这正是我想要做的。我曾尝试使用 _ctx.source 但这使得查询从 50 毫秒变为 3 秒。有没有办法可以重写查询,以便计算两个分数,然后将它们相加?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 2013-12-16
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多