Sort is incorrect

My mapping:

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

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

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

Welcome!

Could you provide a full recreation script as described in About the Elasticsearch category. 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 page. It contains also lot of useful information on how to ask for help.

How Can I edit my topic?

You can add the content as a reply

What is that?

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

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.

2 Likes

Hi,
My query

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

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

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

If I change the order with your suggest:

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

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

Can you set the minimum_should_match parameter 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.

1 Like

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

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