Random_score is ignoring a simple_sort

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:

{
  "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:

{
  "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
}

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

Instead of this

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

Do this:

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

Awesome! It works now.

Thanks!

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