# Sort is incorrect

**URL:** <https://discuss.elastic.co/t/sort-is-incorrect/349906>\
**Category:** Elasticsearch\
**Created:** [December 25, 2023, 4:14am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906 "2023-12-25T04:14:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Binh\_Phan\_Thanh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binh_phan_thanh/32/126063_2.png) [@Binh\_Phan\_Thanh](https://discuss.elastic.co/u/Binh_Phan_Thanh)\
**Post date:** [December 25, 2023, 4:14am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/1 "2023-12-25T04:14:17Z")

</div>

My mapping:

```auto
{
  "my_index": {
    "mappings": {
      "properties": {
        "attributesRecommend": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "attributesSearch": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "price": {
          "type": "double"
        }
      }
    }
  }
}

```

My query to ES:

```auto
{"query":{"bool":{"must":[{"terms":{"status":["publish","sold","sold_by_merchant"]}},{"match":{"type":"sale"}}],"should":[{"multi_match":{"query":"*Toyota*","type":"phrase_prefix","fields":["textSearch","attributesSearch"]}},{"match":{"textSearch":{"query":"*Toyota*"}}},{"match":{"attributesSearch":{"query":"*Toyota*"}}}]}},"from":0,"size":20,"sort":["_score",{"price":{"order":"asc"}}]}

```

Sort with "price" is incorrect.  
I checked in my result hits

```auto
{
...
 "sort": [15.456814, 34000]
}

```

When I remove "\_score" from the sort, the keyword query is no longer correct

I want search match and sort correctly with the above condition.  
How can I resolve it? Or have any solutions?  
Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 25, 2023, 5:24am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/2 "2023-12-25T05:24:33Z")

</div>

Welcome!

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can **copy and paste in Kibana dev console** , click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Have a look at the [Elastic Stack and Solutions Help · Forums and Slack | Elastic](https://elastic.co/community/help) page. It contains also lot of useful information on how to ask for help.

---

<div class="post-metadata">

**Author:** ![Binh\_Phan\_Thanh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binh_phan_thanh/32/126063_2.png) [@Binh\_Phan\_Thanh](https://discuss.elastic.co/u/Binh_Phan_Thanh)\
**Post date:** [December 25, 2023, 7:28am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/3 "2023-12-25T07:28:02Z")

</div>

How Can I edit my topic?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 25, 2023, 9:04am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/4 "2023-12-25T09:04:07Z")

</div>

You can add the content as a reply

---

<div class="post-metadata">

**Author:** ![Binh\_Phan\_Thanh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binh_phan_thanh/32/126063_2.png) [@Binh\_Phan\_Thanh](https://discuss.elastic.co/u/Binh_Phan_Thanh)\
**Post date:** [December 25, 2023, 10:04am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/5 "2023-12-25T10:04:37Z")

</div>

- [elasticsearch clients](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 25, 2023, 2:58pm UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/6 "2023-12-25T14:58:55Z")

</div>

What is that?

---

<div class="post-metadata">

**Author:** ![Binh\_Phan\_Thanh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binh_phan_thanh/32/126063_2.png) [@Binh\_Phan\_Thanh](https://discuss.elastic.co/u/Binh_Phan_Thanh)\
**Post date:** [December 25, 2023, 3:23pm UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/7 "2023-12-25T15:23:14Z")

</div>

I want have a solution for my issue. Please help me.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 25, 2023, 3:48pm UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/8 "2023-12-25T15:48:35Z")

</div>

> [@Binh\_Phan\_Thanh](#):
>
> When I remove "\_score" from the sort, the keyword query is no longer correct

What happens if you change the order or the sort criteria and sort on price first and score second?

If this does not help you need to provide a few sample documents that help show the issue so we can recreate it (which is what David was asking for). You also need to explain the result you are expecting and what you are actually seeing.

It is also always recommended to tell which version of Elasticsearch you are using as behavioir can change over time.

---

<div class="post-metadata">

**Author:** ![Binh\_Phan\_Thanh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binh_phan_thanh/32/126063_2.png) [@Binh\_Phan\_Thanh](https://discuss.elastic.co/u/Binh_Phan_Thanh)\
**Post date:** [December 26, 2023, 9:16am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/9 "2023-12-26T09:16:06Z")

</div>

Hi,  
My query

```auto
{
  "query": {
    "bool": {
      "must": [
        { "terms": { "status": ["publish", "sold", "sold_by_merchant"] } },
        { "match": { "type": "sale" } },
        { "match": { "manufactureYear": 2021 } },
        {
          "bool": {
            "should": [
              {
                "multi_match": {
                  "query": "*toyota Avensis 2021*",
                  "type": "phrase_prefix",
                  "fields": ["textSearch", "attributesSearch"]
                }
              },
              {
                "match": { "textSearch": { "query": "*toyota Avensis 2021*" } }
              },
              {
                "match": {
                  "attributesSearch": { "query": "*toyota Avensis 2021*" }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 5,
  "sort": ["_score", {"price":{"order":"desc"}}]
}

```

and here my result hits:

```auto
{
  "took": 4,
  "timed_out": false,
  "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
  "hits": {
    "total": { "value": 6, "relation": "eq" },
    "max_score": null,
    "hits": [
      {
        "_index": "my_index",
        "_id": "3500",
        "_score": 17.454771,
        "_source": {
          "id": 3500,
          "textSearch": "តូយ៉ូតាAvensisToyotaAvensis丰田Avensis តូយ៉ូតា Avensis Toyota Avensis 丰田 Avensis",
          "price": 31000,
          "publicCoverImage": "https://carz-public-cover-stg.s3.ap-southeast-1.amazonaws.com/cars/images/1683703092800-oJpIun",
          "attributesSearch": "ឡានមួយទឹក Used 二手"
        },
        "sort": [17.454771, 31000]
      },
      {
        "_index": "my_index",
        "_id": "3435",
        "_score": 17.454771,
        "_source": {
          "id": 3435,
          "textSearch": "តូយ៉ូតាAvensisToyotaAvensis丰田Avensis តូយ៉ូតា Avensis Toyota Avensis 丰田 Avensis",
          "price": 25000,
          "attributesSearch": "ចង្កឹះលេខអូតូ Automatic 自动 ម៉ាស៊ូត Diesel 柴油机 Sedan Sedan 轿车 ក្រហម Red 红色 ឡានមួយទឹក Used 二手"
        },
        "sort": [17.454771, 25000]
      },
      {
        "_index": "my_index",
        "_id": "6941",
        "_score": 6.08157,
        "_source": {
          "id": 6941,
          "textSearch": "តូយ៉ូតាVenzaToyotaVenza丰田威飒 តូយ៉ូតា Venza Toyota Venza 丰田 威飒",
          "price": 230,
          "attributesSearch": "ឡានមួយទឹក Used 二手"
        },
        "sort": [6.08157, 230]
      },
      {
        "_index": "my_index",
        "_id": "5011",
        "_score": 5.859531,
        "_source": {
          "id": 5011,
          "textSearch": "តូយ៉ូតាអាល់ហ្វាToyotaAlphard丰田埃尔法 តូយ៉ូតា អាល់ហ្វា Toyota Alphard 丰田 埃尔法",
          "price": 9999,
          "attributesSearch": "ថ្មី New 新"
        },
        "sort": [5.859531, 9999]
      },
      {
        "_index": "my_index",
        "_id": "4137",
        "_score": 5.859531,
        "_source": {
          "id": 4137,
          "textSearch": "តូយ៉ូតាកាមរីToyotaCamry丰田凯美瑞 តូយ៉ូតា កាមរី Toyota Camry 丰田 凯美瑞",
          "price": 2000,
          "attributesSearch": "ថ្មី New 新"
        },
        "sort": [5.859531, 2000]
      },
      {
        "_index": "my_index",
        "_id": "7830",
        "_score": 5.859531,
        "_source": {
          "id": 7830,
          "textSearch": "តូយ៉ូតាបែលតាToyotaBelta丰田贝尔塔 តូយ៉ូតា បែលតា Toyota Belta 丰田 贝尔塔",
          "price": 555,
          "attributesSearch": "ចង្កឹះលេខដៃ Manual 手动 Hybrid Hybrid 混合动力汽车 Sedan Sedan 轿车 ប្រផេះ Gray 灰色 ឡានមួយទឹក Used 二手",
        },
        "sort": [5.859531, 555]
      }
    ]
  }
}

```

My expected result is sort by price from low to high.  
I tried leaving "\_score" out of my query. Sort by price is correct but results by keyword are no longer correct.  
Elasticsearch v8.7.0  
Looking forward to hearing from everyone soon.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 26, 2023, 9:33am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/10 "2023-12-26T09:33:35Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> What happens if you change the order or the sort criteria and sort on price first and score second?

Did you try what I suggested? If so, what was the result?

---

<div class="post-metadata">

**Author:** ![Binh\_Phan\_Thanh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binh_phan_thanh/32/126063_2.png) [@Binh\_Phan\_Thanh](https://discuss.elastic.co/u/Binh_Phan_Thanh)\
**Post date:** [December 26, 2023, 9:46am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/11 "2023-12-26T09:46:43Z")

</div>

If I change the order with your suggest:

```auto
{"query":{"bool":{"must":[{"terms":{"status":["publish","sold","sold_by_merchant"]}},{"match":{"type":"sale"}}],"should":[{"multi_match":{"query":"*Toyota Alphard*","type":"phrase_prefix","fields":["textSearch","attributesSearch"]}},{"match":{"textSearch":{"query":"*Toyota Alphard*"}}},{"match":{"attributesSearch":{"query":"*Toyota Alphard*"}}}]}},"from":0,"size":20,"sort":[{"price":{"order":"asc"}},"_score"]}

```

Result with query above:

```auto
{
  "took": 5,
  "timed_out": false,
  "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
  "hits": {
    "total": { "value": 3009, "relation": "eq" },
    "max_score": null,
    "hits": [
      {
        "_index": "lb_carz",
        "_id": "6456",
        "_score": 1.177574,
        "_source": {
          "id": 6456,
          "textSearch": "ChevroletKMCamaroChevroletCamaro雪佛兰ZH科迈罗 Chevrolet KM Camaro Chevrolet Camaro 雪佛兰 ZH 科迈罗",
          "price": -1,
          "attributesSearch": "ថ្មី New 新"
        },
        "sort": [-1, 1.177574]
      },
      {
        "_index": "lb_carz",
        "_id": "3663",
        "_score": 1.177574,
        "_source": {
          "id": 3663,
          "textSearch": "BMWi8BMWi8宝马i8 BMW i8 BMW i8 宝马 i8",
          "price": 0,
          
          "attributesSearch": "ថ្មី New 新"
        },
        "sort": [0, 1.177574]
      },
      {
        "_index": "lb_carz",
        "_id": "3667",
        "_score": 1.177574,
        "_source": {
          "id": 3667,
          "textSearch": "BMWi8BMWi8宝马i8 BMW i8 BMW i8 宝马 i8",
          "price": 0,
          "attributesSearch": "ថ្មី New 新"
        },
        "sort": [0, 1.177574]
      },
      {
        "_index": "lb_carz",
        "_id": "3719",
        "_score": 1.177574,
        "_source": {
          "id": 3719,
          "textSearch": "BMWi8BMWi8宝马i8 BMW i8 BMW i8 宝马 i8",
          "price": 0,
          "attributesSearch": "ថ្មី New 新"
        },
        "sort": [0, 1.177574]
      },
      {
        "_index": "lb_carz",
        "_id": "3720",
        "_score": 1.177574,
        "_source": {
          "id": 3720,
          "textSearch": "BMWi3SeriesBMWi3Series宝马i3系 BMW i3 Series BMW i3 Series 宝马 i3 系",
          "price": 0,
          "attributesSearch": "ថ្មី New 新"
        },
        "sort": [0, 1.177574]
      }
    ]
  }
}

```

The results seem to have nothing to do with the keyword. The query retrieves the smallest price in my dataset and sort

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 26, 2023, 10:24am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/12 "2023-12-26T10:24:04Z")

</div>

Can you set [the minimum\_should\_match parameter](https://www.elastic.co/guide/en/elasticsearch/reference/8.11/query-dsl-bool-query.html) to `1` for the should block? There is an example in the docs I linked to. Keep the sorting order used in the last query.

---

<div class="post-metadata">

**Author:** ![Binh\_Phan\_Thanh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binh_phan_thanh/32/126063_2.png) [@Binh\_Phan\_Thanh](https://discuss.elastic.co/u/Binh_Phan_Thanh)\
**Post date:** [December 26, 2023, 10:45am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/13 "2023-12-26T10:45:04Z")

</div>

Cool. It worked.  
Thank you very much for your help.

---

<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 23, 2024, 10:45am UTC](https://discuss.elastic.co/t/sort-is-incorrect/349906/14 "2024-01-23T10:45:14Z")

</div>

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