# Accurate and relevant user-defined sorting

**URL:** https://discuss.elastic.co/t/accurate-and-relevant-user-defined-sorting/382611
**Category:** Elasticsearch
**Created:** [October 12, 2025, 5:18pm UTC](https://discuss.elastic.co/t/accurate-and-relevant-user-defined-sorting/382611 "2025-10-12T17:18:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![GalacticHypernova](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@GalacticHypernova](https://discuss.elastic.co/u/GalacticHypernova)
#### Post date: [October 12, 2025, 5:18pm UTC](https://discuss.elastic.co/t/accurate-and-relevant-user-defined-sorting/382611/1 "2025-10-12T17:18:44Z")

</div>

Hey guys, quite new to ES.

I have an app that supports user-provided queries, user-provided filters, and user-provided sorts. The query is fine in multi\_match, and filters are good in the filter query. My main issue is the user-provided sorts.

Users interacting with my app can choose a field to sort by and the order (from a predefined list of options, of course). Some are numbers (e.g. like count, views) and others are dates (e.g. creation date), and order is asc/desc.

When I try sorting based on the fields, the date fields appear to behave oddly (with numbers it works fine). My dates are ISO dates (for context, passed from mongodb via monsatche, with a default pipeline to keep the subset I need for search), and when I try using the `sort` option with order: “desc” and a format of `strict_date_optional_time_nanos` I get inconsistent results (something from 2 months ago, followed by 10 months ago, followed by 1 month ago).

If I in stead try to use the field for boosting (e.g. `gauss`) it seems like the results are in the opposite direction (ascending in stead of descending).

My query looks like this:

```ts

{
  query: {
    bool: {
      filter: [user-selected filters],
      must: { multi_match: { query: user-query } },
    }
  },
  sort: [user-provided sort with _score tie breaker (either just order if number, or order with the format if date]
}

```

If I try to use the boosting approach, I remove the sort and in stead wrap the query in a `function_score` with `funcs` being a single-element array (`field_value_factor` for numbers, `gauss` for dates). Example:

```ts
gauss: {
  [sort]: { // user-selected date field
	scale: order === "desc" ? "30d" : "730d",
	offset: order === "desc" ? "7d" : "0d",
	decay: 0.5,
	origin: order === "desc" ? "now" : "some-past-date"
  }
}

```

Am I doing something wrong? I know that boosting is generally less appropriate for sorting because it's not "absolute" like sorting.

What is the correct way to sort results based on user-provided sorts in a way that would maintain accuracy and relevance, but still respect the user-defined sort?

---

<div class="post-metadata">

### Author: ![GalacticHypernova](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@GalacticHypernova](https://discuss.elastic.co/u/GalacticHypernova)
#### Post date: [October 13, 2025, 8:20pm UTC](https://discuss.elastic.co/t/accurate-and-relevant-user-defined-sorting/382611/2 "2025-10-13T20:20:19Z")

</div>

Sorry for the trouble everyone, I managed to make it work.

If anyone else encounters this problem in the future, this is what I did:

I removed the function\_score section, as it's generally less suitable for user-input sorts.

In stead, I used the `sort` option with 2 sorts: first being `_score` descending (for relevance), second being `[sort]` (the user selected sort).

This appears to give the right balance between relevant results and compliant results (with the user's sort), at least in my use case.
