elasticsearch nested functionScoreQuery cannot access parent properties

m1416

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.

Andrei Stefan

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Elasticsearch: querying on both nested object properties and parent properties

From Dev

nested aggregation elasticsearch with access parent field for sub-aggregation

From Dev

How to access parent data properties in JsRender nested templates

From Dev

Elasticsearch aggregation on parent properties

From Dev

cannot access 'this' in nested function

From Dev

How to access parent object properties?

From Dev

Access parent function properties Javascript

From Dev

Why can nested child classes access private members of their parent class, but grandchildren cannot?

From Dev

Why can nested child classes access private members of their parent class, but grandchildren cannot?

From Dev

Why can nested child classes access private members of their parent class, but grandchildren cannot?

From Dev

ElasticSearch aggregate nested fields as part of parent document

From Dev

Objects in a list needs to access parent list properties

From Dev

Gradle apply from no access to parent properties

From Dev

access javascript parent class properties from subclass

From Dev

Access the child class properties from a parent

From Dev

Javascript Access to parent properties from child method

From Dev

Polymer: access properties of parent or other elements

From Dev

ElasticSearch: access document nested value in groovy script

From Dev

How to access third Nested ListView properties

From Dev

Easy way to access nested properties in a Map

From Dev

Access parent variable from nested block in JsRender

From Dev

Nested Python class and access to parent's method

From Dev

Python: Access all Parent Variables in Nested Function

From Dev

AngularJS access to $first of ngRepeat parent in a nested ngRepeat

From Dev

extJs access a parent function from nested object

From Dev

NodeJs/Javascript access parent method in nested class

From Dev

Nested Python class and access to parent's method

From Dev

AngularJS access to $first of ngRepeat parent in a nested ngRepeat

From Dev

NodeJs/Javascript access parent method in nested class

Related Related

  1. 1

    Elasticsearch: querying on both nested object properties and parent properties

  2. 2

    nested aggregation elasticsearch with access parent field for sub-aggregation

  3. 3

    How to access parent data properties in JsRender nested templates

  4. 4

    Elasticsearch aggregation on parent properties

  5. 5

    cannot access 'this' in nested function

  6. 6

    How to access parent object properties?

  7. 7

    Access parent function properties Javascript

  8. 8

    Why can nested child classes access private members of their parent class, but grandchildren cannot?

  9. 9

    Why can nested child classes access private members of their parent class, but grandchildren cannot?

  10. 10

    Why can nested child classes access private members of their parent class, but grandchildren cannot?

  11. 11

    ElasticSearch aggregate nested fields as part of parent document

  12. 12

    Objects in a list needs to access parent list properties

  13. 13

    Gradle apply from no access to parent properties

  14. 14

    access javascript parent class properties from subclass

  15. 15

    Access the child class properties from a parent

  16. 16

    Javascript Access to parent properties from child method

  17. 17

    Polymer: access properties of parent or other elements

  18. 18

    ElasticSearch: access document nested value in groovy script

  19. 19

    How to access third Nested ListView properties

  20. 20

    Easy way to access nested properties in a Map

  21. 21

    Access parent variable from nested block in JsRender

  22. 22

    Nested Python class and access to parent's method

  23. 23

    Python: Access all Parent Variables in Nested Function

  24. 24

    AngularJS access to $first of ngRepeat parent in a nested ngRepeat

  25. 25

    extJs access a parent function from nested object

  26. 26

    NodeJs/Javascript access parent method in nested class

  27. 27

    Nested Python class and access to parent's method

  28. 28

    AngularJS access to $first of ngRepeat parent in a nested ngRepeat

  29. 29

    NodeJs/Javascript access parent method in nested class

HotTag

Archive