# How to sort results based on another field's value or weightage

**URL:** <https://discuss.elastic.co/t/how-to-sort-results-based-on-another-fields-value-or-weightage/259713>\
**Category:** Elasticsearch\
**Created:** [December 28, 2020, 8:08am UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-another-fields-value-or-weightage/259713 "2020-12-28T08:08:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pushanbhattacharya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pushanbhattacharya/32/34104_2.png) [@pushanbhattacharya](https://discuss.elastic.co/u/pushanbhattacharya)\
**Post date:** [December 28, 2020, 8:08am UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-another-fields-value-or-weightage/259713/1 "2020-12-28T08:08:14Z")

</div>

Hi,

Let's take an example.

Say the index has 2 fields, viz., `name` and `weightage`.

Values-

`name: Black Bus, weightage: 1`  
`name: Blue Bus, weightage: 2`

`name: Black Car, weightage: 2`  
`name: Blue Car, weightage: 1`

Now, I would like to show the query results based on the `_score` and the `weightage`. E.g.,

if the user searches-

`black` =\> the results are obviously `Black Bus` and `Black Car`. But as `Black Car` has higher weightage (2), so it will be displayed first (above `Black Bus`)

Similarly,

`blue` =\> `Blue Bus` (2), `Blue Car` (1)  
`bus` =\> `Blue Bus` (2), `Black Bus` (1)  
`car` =\> `Black Car` (2), `Blue Car` (1)

Now, I tried to use the below sorting algorithm-

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

```

But the above has a problem for exact match, say-

if the user searches for `Black Car` or `Blue Bus` or `Black Bus` etc., then I want to show the exact match at the top, which will not work if `_score` comes second in the sort criteria.

Is there a way to boost a document in the search with the value of another field or is it possible to have this as part of the sort?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pushanbhattacharya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pushanbhattacharya/32/34104_2.png) [@pushanbhattacharya](https://discuss.elastic.co/u/pushanbhattacharya)\
**Post date:** [December 28, 2020, 10:03am UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-another-fields-value-or-weightage/259713/2 "2020-12-28T10:03:26Z")

</div>

Hi,

I have used the below sorting query to get the desired results. So far it looks like this is working.

Does it look correct or is there any fault in this?  
Also is there any other way to achieve the same without using `script sort`, that can have support from `node.js`?

```auto
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": "doc['weightage'].value + _score"
      },
      "order": "desc"
    }
  },

```

---

<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:** [January 25, 2021, 10:03am UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-another-fields-value-or-weightage/259713/3 "2021-01-25T10:03:27Z")

</div>

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