php - 如何在elasticsearch中的功能增强查询中排序

标签 php search elasticsearch lucene

我有一个索引,其中包含有关手机、保护壳等的详细信息。

下面是使用的查询。

{
"query": {
    "function_score": {
      "query": { "match": {"title" :"Apple iPhone 6s"} },
      "boost": "5", 
      "functions": [
          {
              "filter": { "match": { "main_category": "mobiles"             } },
              "weight": 8
          },
          {
              "filter": { "match": {"main_category": "cases-and-covers"  } },
              "weight": 6
          }
      ],
      "max_boost": 8,
      "score_mode": "max",
      "boost_mode": "multiply",
      "min_score" : 5
    }
},
"_source":["title","main_category","selling_price"],
"size" : 1000

}

是否可以像下面这样提升手机类别,并通过销售价格升序在手机类别内排序。

Boost 工作正常。如何在特定的 boost 函数内排序?

如果用户搜索apple iphone 6s,我希望提升手机类别,并且最低价格应该排在第一位,然后是保护壳和保护套品类产品也按售价升序排列。

以下是所需的结果

{
   "took": 6,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 104,
      "max_score": 40.645245,
      "hits": [
         {
            "_index": "test",
            "_type": "products",
            "_id": "shop_24",
            "_score": 40.645245,
            "_source": {
               "selling_price": 72000,
               "main_category": "mobiles",
               "title": "Apple iPhone 6s"
            }
         },
         {
            "_index": "test",
            "_type": "products",
            "_id": "shop_20",
            "_score": 40.168346,
            "_source": {
               "selling_price": 82000,
               "main_category": "mobiles",
               "title": "Apple iPhone 6s Plus"
            }
         },
         {
            "_index": "test",
            "_type": "products",
            "_id": "shop_15",
            "_score": 39.365562,
            "_source": {
               "selling_price": 92000,
               "main_category": "mobiles",
               "title": "Apple iPhone 6s Plus"
            }
         },
         {
            "_index": "test",
            "_type": "products",
            "_id": "shop_17",
            "_score": 39.365562,
            "_source": {
               "selling_price": 2000,
               "main_category": "cases-and-covers",
               "title": "Case cover for Apple iPhone 6s"
            }
         },
         {
            "_index": "test",
            "_type": "products",
            "_id": "shop_18",
            "_score": 39.365562,
            "_source": {
               "selling_price": 2300,
               "main_category": "cases-and-covers",
               "title": "Case cover for Apple iPhone 6s Plus"
            }
         }
      ]
   }
}

请帮忙?

最佳答案

您可以尝试以下查询吗:

{
"query": {
  "function_score": {
     "query": {
        "match_all": {}          // Change this to query as per your need
     },
     "boost": "5",
     "functions": [
        {
           "filter": {
              "match": {
                 "main_category": "mobiles"
              }
           },
           "weight": 50
        },
        {
           "filter": {
              "match": {
                 "main_category": "cases-and-covers"
              }
           },
           "weight": 25
        }
      ]

     }
    },
   "sort": [
   {
     "_score": {
        "order": "desc"
     }
   },
   {
     "selling_price": {
        "order": "asc"
     }
   }
  ]
 }

我们提供高权重weight=50具有移动类别且权重较低的文档weight=25到具有case-and-cover类别的文档。

最后,我们首先根据分数进行排序,然后根据售价进行排序。

关于php - 如何在elasticsearch中的功能增强查询中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609871/

相关文章:

php - 如何在 echo <td> 中应用样式?

ios - 位置搜索 : Specific Address vs local businesses

c# - 如何使用具有特定子目录的通配符扫描目录

Elasticsearch,在私有(private) IP 上发布

docker - docker上magento2的Elasticsearch设置给出 “no alive nodes”错误

php - 用于解析 float 、小数、 double 的 MySQL 列类型的正则表达式

php - 从 mysql 规范化表中获取最受欢迎的标签

php - WordPress 插件如何区分 WordPress

c# - 使用 wpf 在列表框中搜索

java - 如果对象在Elastic search中具有对象列表,那么我们可以在一个文档中对数据进行排序吗?我还应该对嵌套文档进行排序