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

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-reference-the-outer-object-in-a-nested-query/193171>\
**Category:** Elasticsearch\
**Created:** [July 31, 2019, 3:53pm UTC](https://discuss.elastic.co/t/is-it-possible-to-reference-the-outer-object-in-a-nested-query/193171 "2019-07-31T15:53:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeledYuval](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@PeledYuval](https://discuss.elastic.co/u/PeledYuval)\
**Post date:** [July 31, 2019, 3:53pm UTC](https://discuss.elastic.co/t/is-it-possible-to-reference-the-outer-object-in-a-nested-query/193171/1 "2019-07-31T15:53:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 28, 2019, 4:06pm UTC](https://discuss.elastic.co/t/is-it-possible-to-reference-the-outer-object-in-a-nested-query/193171/2 "2019-08-28T16:06:46Z")

</div>

Closed on August 28, 2019, 4:06pm UTC

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