Is it possible to reference the outer object in a nested query?

Hi everybody,
First of all- thanks in advance for helping.

I'm running on Elastic 5.6 (I know, we should upgrade).
I have an index that holds documents with a nested property. Simplified:

{
	"mappings": {
		"my_type": {
			"properties": {
				"user": {
					"type": "nested"
				},
				"group": {
					"type": "integer"
				}
			}
		}
	}
}

I'm trying to create a score_function that returns a different score for each nested value, but the score needs to be computed by referencing a property of the outer object that holds the nested value. Simply put, the score must be computed using both the "group" property, and the "users.age" property for each user.

However, it seems like the function that computes a score for the "group" property always returns the same value (as if it doesn't get the real value from the object, but some default value).

The function I'm running:

{
	"query": {
		"nested": {
			"path": "user",
			"inner_hits": {},
			"query": {
				"function_score": {
					"boost_mode": "sum",
					"functions": [
						{
							"gauss": {
								"user.age": {
									"origin": 10,
									"scale": 10
								}
							}
						},
						{
							"gauss": {
								"group": {
									"origin": 230,
									"scale": 10
								}
							}
						}
					]
				}
			}
		}
	}
}

The result:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1.6060474,
    "hits": [
      {
        "_index": "test_index",
        "_type": "my_type",
        "_id": "10",
        "_score": 1.3208565,
        "_source": {
          "group": 230,
          "user": [
            {
              "first": "Micky",
              "last": "Longs",
              "age": 104
            },
            {
              "first": "Eva",
              "last": "Popper",
              "age": 2.5
            }
          ]
        },
        "inner_hits": {
          "user": {
            "hits": {
              "total": 2,
              "max_score": 1.6417129,
              "hits": [
                {
                  "_index": "test_index",
                  "_type": "my_type",
                  "_id": "10",
                  "_nested": {
                    "field": "user",
                    "offset": 1
                  },
                  "_score": 1.6417129,
                  "_source": {
                    "first": "Eva",
                    "last": "Popper",
                    "age": 2.5
                  }
                },
                {
                  "_index": "test_index",
                  "_type": "my_type",
                  "_id": "10",
                  "_nested": {
                    "field": "user",
                    "offset": 0
                  },
                  "_score": 1.0,
                  "_source": {
                    "first": "Micky",
                    "last": "Longs",
                    "age": 104
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index": "test_index",
        "_type": "my_type",
        "_id": "3",
        "_score": 1.3208565,
        "_source": {
          "group": 23,
          "user": [
            {
              "first": "Micky",
              "last": "Longs",
              "age": 104
            },
            {
              "first": "Eva",
              "last": "Popper",
              "age": 2.5
            }
          ]
        },
        "inner_hits": {
          "user": {
            "hits": {
              "total": 2,
              "max_score": 1.6417129,
              "hits": [
                {
                  "_index": "test_index",
                  "_type": "my_type",
                  "_id": "3",
                  "_nested": {
                    "field": "user",
                    "offset": 1
                  },
                  "_score": 1.6417129,
                  "_source": {
                    "first": "Eva",
                    "last": "Popper",
                    "age": 2.5
                  }
                },
                {
                  "_index": "test_index",
                  "_type": "my_type",
                  "_id": "3",
                  "_nested": {
                    "field": "user",
                    "offset": 0
                  },
                  "_score": 1.0,
                  "_source": {
                    "first": "Micky",
                    "last": "Longs",
                    "age": 104
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

I would expect the results for the inner hits for the document with "group: 230" and the document with "group: 23" to be different due to the Guassian score function. What am I doing wrong?

Thank you!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.