Elasticsearch查询搜索按关键字,然后按价格排序结果

发布于 2025-02-11 03:47:43 字数 1165 浏览 0 评论 0原文

这是我目前的搜索查询,

"query":{
     "bool":{
        "filter":[
           {
              "term":{
                 "status":{
                    "value":1
                 }
              }
           }
     
        ],
       
        "should":[
          
           {
              "multi_match":{
                 "fields":[
                    "title",
                    "categories.title",
                    "subtitle"
                 ],
                 "type":"most_fields",
                 "fuzziness":"AUTO",
                 "query":"GTX"
              }
           }
        ]
     }
  },

我要做的就是按价格上升顺序排序。但是,当我将价格放在排序中时,它将按顺序排列所有文档并忽略过滤器。 这是我的查询,

"sort": [
    {
        "_script":{
           "type":"number",
           "script":{
              "lang":"painless",
              "source":"if(Math.abs(doc['product_quantity'].value) > 0) { 1 } else { 0 }"
           },
           "order":"desc"
        }
     },
    {
        "_score": {
            "order": "desc"
        }
    },
    {
        "price": {
            "order": "desc"
        }
    }
    
],

我应该如何按价格按顺序排序搜索结果

this is my current search query

"query":{
     "bool":{
        "filter":[
           {
              "term":{
                 "status":{
                    "value":1
                 }
              }
           }
     
        ],
       
        "should":[
          
           {
              "multi_match":{
                 "fields":[
                    "title",
                    "categories.title",
                    "subtitle"
                 ],
                 "type":"most_fields",
                 "fuzziness":"AUTO",
                 "query":"GTX"
              }
           }
        ]
     }
  },

what i am trying to do is to sort by price in ascending order. but when i put price in my sort query it will sort all documents in ascending order and ignore the filters.
this is my sort query

"sort": [
    {
        "_script":{
           "type":"number",
           "script":{
              "lang":"painless",
              "source":"if(Math.abs(doc['product_quantity'].value) > 0) { 1 } else { 0 }"
           },
           "order":"desc"
        }
     },
    {
        "_score": {
            "order": "desc"
        }
    },
    {
        "price": {
            "order": "desc"
        }
    }
    
],

how should i sort the searched results in ascending order by price

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文