Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch nested functionScoreQuery cannot access parent properties

I have a type in elasticsearch that looks like this:

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

where rooms is of type nested. I sort using a nested 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"
        }
      }
   }
}

Problem is hotel.field returns 0 always. Is there a way to access the parent field inside a nested query? I know I can always pack the field inside the nested document but its a hack not a solution. Would using a dismax query help me? https://discuss.elastic.co/t/nested-value-on-function-score/29935

The query I am actually using looks something like this:

{
  "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"
            }
          } ]
        }
      }
    }
  }
}

What I am trying to achieve is to access for example the cityId inside the function_score query which is nested.

like image 400
m1416 Avatar asked Nov 16 '15 11:11

m1416


1 Answers

The question is why are you accessing the parent values in a nested query. Once you are in the nested context, you cannot access parent fields or other fields from other nested fields.

From the documentation:

The nested clause “steps down” into the nested comments field. It no longer has access to fields in the root document, nor fields in any other nested document.

So, rewrite your queries so that the nested part touches the fields in that nested field and anything else is accessed outside the nested part.

like image 193
Andrei Stefan Avatar answered Nov 15 '22 07:11

Andrei Stefan