# Elasticsearch random scoring with filters

**URL:** <https://discuss.elastic.co/t/elasticsearch-random-scoring-with-filters/157253>\
**Category:** Elasticsearch\
**Created:** [November 18, 2018, 11:49pm UTC](https://discuss.elastic.co/t/elasticsearch-random-scoring-with-filters/157253 "2018-11-18T23:49:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abeering](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@abeering](https://discuss.elastic.co/u/abeering)\
**Post date:** [November 18, 2018, 11:49pm UTC](https://discuss.elastic.co/t/elasticsearch-random-scoring-with-filters/157253/1 "2018-11-18T23:49:20Z")

</div>

I've got a situation, using ES 6.2, where I can either perform a `bool` or a `random_score` , but not both at the same time. Here is one of those potential queries:

```
{
  "from": 0,
  "size": 50,
  "query": {
    "function_score": {
      "random_score": {
        "seed": 10,
        "field": "_seq_no"
      }
    },
    "bool": {
      "filter": [
        {
          "terms": {
            "primary_category": [
              "foobar"
            ]
          }
        },
        {
          "terms": {
            "primary_type": [
              "barbaz"
            ]
          }
        }
      ]
    }
  }
}

```

If I were to remove either the `function_score` block or the `bool` block, the query works, but in combination, it does not:

`[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]`

Am I missing something about the example at: [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-function-score-query.html#function-random](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-function-score-query.html#function-random)

All I want to do is "randomly sort" my results in a predictable way which will work across pagination, etc. Really I am just trying to display the filtered results with high variance, as any sort of standard sorting will create patterns in the result which I am trying to avoid.

Any help would be appreciated, and I'll keep tinkering with it.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 20, 2018, 12:15am UTC](https://discuss.elastic.co/t/elasticsearch-random-scoring-with-filters/157253/2 "2018-11-20T00:15:38Z")

</div>

The `query` element of a search request can only have one sub-object. You likely want to put the bool query inside the function\_score query under the `query` element there.

---

<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:** [December 18, 2018, 12:15am UTC](https://discuss.elastic.co/t/elasticsearch-random-scoring-with-filters/157253/3 "2018-12-18T00:15:47Z")

</div>

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