# Random score on nested query with inner hits

**URL:** <https://discuss.elastic.co/t/random-score-on-nested-query-with-inner-hits/167112>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 11:56am UTC](https://discuss.elastic.co/t/random-score-on-nested-query-with-inner-hits/167112 "2019-02-05T11:56:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomasn4a](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomasn4a/32/40571_2.png) [@tomasn4a](https://discuss.elastic.co/u/tomasn4a)\
**Post date:** [February 5, 2019, 11:56am UTC](https://discuss.elastic.co/t/random-score-on-nested-query-with-inner-hits/167112/1 "2019-02-05T11:56:54Z")

</div>

I am having trouble getting a random set of documents when performing a nested query with inner\_hits. I need the documents to be random, not the inner hits themselves.

Here's the mapping for the examples:

```
{
    "mappings": {
        "doc": {
            "properties": {
                "faces": {
                    "type": "nested",
                    "properties": {
                        "cluster_id": {
                            "type": "integer"
                        }
                    }
                }
            }
        }
    }
}

```

Now, just as a sanity check, the following query returns a random set of documents:

```
{
    "query": {
        "function_score": {
            "random_score": {},
            "query": {
                "match_all": {}
            }
        }
    }
}

```

But now if I try to modify that to return the inner hits for a specific cluster id, then the results are not random anymore. Here's the query:

```
{
    "query": {
        "function_score": {
            "random_score": {},
            "query": {
                "nested": {
                    "path": "faces",
                    "query": {
                        "bool": {
                            "filter": [
                                {
                                    "term": {
                                        "faces.cluster_id": 0
                                    }
                                }
                            ]
                        }
                    },
                    "inner_hits": {}
                }
            }
        }
    }
}

```

I have also tried wrapping the query bit of the nested query in the function\_score, but the results are the same, no random ordering.

Any help is greatly appreciated!

---

<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:** [March 5, 2019, 11:56am UTC](https://discuss.elastic.co/t/random-score-on-nested-query-with-inner-hits/167112/2 "2019-03-05T11:56:56Z")

</div>

Closed on March 5, 2019, 11:56am UTC

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