怎么又是你

如何查询置顶商品并排序

Elasticsearch | 作者 WangChuanfu | 发布于2020年10月18日 | 阅读数:1785

问题表述:
 
   搜索电商商品,后台运营将商品标志为是否置顶,
   如果这个商品被标志为置顶商品(is_ontop),在查询的时候,
   使其排在最前面.同时先按照销量排序,
   如何销量相同再按照商品上架时间来编写怎么编写dsl语句
 
 
GET  index/_search
{
  "from": 0,
  "size": 0,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "constant_score": {
                "filter": {
                  "match": {
                    "sk": {
                      "query": "耐克"
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "saleCount": {
        "order": "desc"
      }
    },
    {
      "saleTime": {
        "order": "desc"
      }
    }
  ]
}
 
 
 
 
 
 
 
已邀请:

FFFrp

赞同来自:

https://www.elastic.co/guide/e ... .html  新版本有一个类似置顶的查询,可以先将置顶的查出来完后用这个查询就会排在最前面

envy666

赞同来自:

效率满足的话,从业务上分两次查询呗

- Elasticsearch,php

赞同来自:

GET  index/_search
{
"from": 0,
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"should": [{
"constant_score": {
"filter": {
"match": {
"sk": {
"query": "耐克"
}
}
}
}
}]
}
}
}
},
"sort": [{
"is_ontop": {
"order": "desc"
}
}, {
"saleCount": {
"order": "desc"
}
},
{
"saleTime": {
"order": "desc"
}
}
]
}
 
sort排序中,排在前面的优先级高,后面的优先级低,当前一个的值相同时,才会根据后一个排序。 is_ontop 排第一位,优先排置顶的,当is_ontop 相同时,才会根据saleCount降序,以此类推。

要回复问题请先登录注册