# Random\_score is ignoring a simple\_sort

**URL:** <https://discuss.elastic.co/t/random-score-is-ignoring-a-simple-sort/189548>\
**Category:** Elasticsearch\
**Created:** [July 9, 2019, 12:40pm UTC](https://discuss.elastic.co/t/random-score-is-ignoring-a-simple-sort/189548 "2019-07-09T12:40:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blacksoul](https://avatars.discourse-cdn.com/v4/letter/b/838e76/32.png) [@blacksoul](https://discuss.elastic.co/u/blacksoul)\
**Post date:** [July 9, 2019, 12:40pm UTC](https://discuss.elastic.co/t/random-score-is-ignoring-a-simple-sort/189548/1 "2019-07-09T12:40:13Z")

</div>

Hello,

I have a search within my index that currently works, I want to get the professionals that have a subscription first (is\_subscribed BOOLEAN) and then follow with the rest of users. I achieve this using the query:

```auto
{
  "track_scores": true,
  "from": 0,
  "size": 57,
  "query": {
    "function_score": {
      "query": {
        "constant_score": {
          "filter": {
            "bool": {
              "should": [
                {
                  "term": {
                    "primary_locality_id": 4
                  }
                },
                {
                  "term": {
                    "secondary_locality_id": 4
                  }
                }
              ]
            }
          }
        }
      },
      "random_score": {
        "seed": 549345209,
        "field": "is_subscribed"
      }
    }
  },
  "sort": [
    {
      "is_subscribed": {
        "order": "desc"
      },
      "_score": {
        "order": "desc"
      }
    }
  ]
}

```

I enable the score tracking and sort by `is_subscribed` first and then by `_score` , but my results aren't showing the `is_subscribed: true` first

Here is the detailed output:

```auto
{
  "hits": {
    "hits": [
      {
        "sort": [
          0.92897844,
          0
        ],
        "_type": "professionals",
        "_source": {
          "is_subscribed": false,
          "primary_locality_id": 4,
          "secondary_locality_id": 4,
          "avg_cost": null
        },
        "_score": 0.92897844,
        "_index": "my_index",
        "_id": "2050487"
      },
      {
        "sort": [
          0.92897844,
          0
        ],
        "_type": "professionals",
        "_source": {
          "is_subscribed": false,
          "primary_locality_id": 4,
          "secondary_locality_id": 4,
          "avg_cost": null
        },
        "_score": 0.92897844,
        "_index": "my_index",
        "_id": "2050501"
      },
      {
        "sort": [
          0.92897844,
          0
        ],
        "_type": "professionals",
        "_source": {
          "is_subscribed": false,
          "primary_locality_id": 4,
          "secondary_locality_id": 4,
          "avg_cost": null
        },
        "_score": 0.92897844,
        "_index": "my_index",
        "_id": "2050505"
      },
      {
        "sort": [
          0.92897844,
          0
        ],
        "_type": "professionals",
        "_source": {
          "is_subscribed": false,
          "primary_locality_id": 4,
          "secondary_locality_id": 4,
          "avg_cost": null
        },
        "_score": 0.92897844,
        "_index": "my_index",
        "_id": "2050451"
      },
      {
        "sort": [
          0.92897844,
          0
        ],
        "_type": "professionals",
        "_source": {
          "is_subscribed": false,
          "primary_locality_id": 4,
          "secondary_locality_id": 4,
          "avg_cost": null
        },
        "_score": 0.92897844,
        "_index": "my_index",
        "_id": "2050768"
      },
      {
        "sort": [
          0.92897844,
          0
        ],
        "_type": "professionals",
        "_source": {
          "is_subscribed": false,
          "primary_locality_id": 4,
          "secondary_locality_id": 4,
          "avg_cost": null
        },
        "_score": 0.92897844,
        "_index": "my_index",
        "_id": "2050678"
      },
      {
        "sort": [
          0.92897844,
          0
        ],
        "_type": "professionals",
        "_source": {
          "is_subscribed": false,
          "primary_locality_id": 4,
          "secondary_locality_id": 4,
          "avg_cost": "60"
        },
        "_score": 0.92897844,
        "_index": "my_index",
        "_id": "2050679"
      }
    ],
    "total": {
      "relation": "eq",
      "value": 92
    },
    "max_score": 0.92897844
  },
  "_shards": {
    "successful": 1,
    "failed": 0,
    "skipped": 0,
    "total": 1
  },
  "took": 4,
  "timed_out": false
}

```

---

<div class="post-metadata">

**Author:** ![telendt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telendt/32/3416_2.png) [@telendt](https://discuss.elastic.co/u/telendt)\
**Post date:** [July 9, 2019, 1:01pm UTC](https://discuss.elastic.co/t/random-score-is-ignoring-a-simple-sort/189548/2 "2019-07-09T13:01:45Z")

</div>

You should have two objects in sort array, not one with two keys.

Instead of this

```auto
"sort": [
  {
    "is_subscribed": {
      "order": "desc"
    },
    "_score": {
      "order": "desc"
    }
  }
]

```

Do this:

```auto
"sort": [
  {
    "is_subscribed": {
      "order": "desc"
    }
  }, {
    "_score": {
      "order": "desc"
    }
  }
]

```

---

<div class="post-metadata">

**Author:** ![blacksoul](https://avatars.discourse-cdn.com/v4/letter/b/838e76/32.png) [@blacksoul](https://discuss.elastic.co/u/blacksoul)\
**Post date:** [July 9, 2019, 1:29pm UTC](https://discuss.elastic.co/t/random-score-is-ignoring-a-simple-sort/189548/3 "2019-07-09T13:29:16Z")

</div>

Awesome! It works now.

Thanks!

---

<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 6, 2019, 1:29pm UTC](https://discuss.elastic.co/t/random-score-is-ignoring-a-simple-sort/189548/4 "2019-08-06T13:29:26Z")

</div>

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