【问题标题】:How to boost results on the basis of fields values如何根据字段值提升结果
【发布时间】:2021-12-14 07:46:44
【问题描述】:

我一直坚持为这个问题创建查询。查询将关键字搜索以及产品评分。 我有这些产品列表

[
  {
    name: 'pro',
    clickCount: 110,
    bookingCount: 57,
    isPromoted: 0,
  },
  {
    name: 'prod',
    clickCount: 13,
    bookingCount: 77,
    isPromoted: 0,
  },
  {
    name: 'produ',
    clickCount: 43,
    bookingCount: 10,
    isPromoted: 0,
  },
  {
    name: 'produc',
    clickCount: 5,
    bookingCount: 17,
    isPromoted: 0,
  },
  {
    name: 'product',
    clickCount: 89,
    bookingCount: 67,
    isPromoted: 0,
  },
  {
    name: 'products',
    clickCount: 1,
    bookingCount: 2,
    isPromoted: 1,
  },
  {
    name: 'products2',
    clickCount: 3,
    bookingCount: 4,
    isPromoted: 1,
  },
]

我需要将它们按以下顺序排序:

  1. isPromoted=1 的产品将位于顶部(如果有多个具有相同值的产品,则具有 MAX(clickCount+bookingCount) 的产品将优先。
  2. 随后会出现具有 MAX(clickCount+bookingCount) 的产品。

关键字搜索pro的最终排序应该是这样的:

[
  {
    name: 'products2',
    clickCount: 3,
    bookingCount: 4,
    isPromoted: 1,
  },
  {
    name: 'products',
    clickCount: 1,
    bookingCount: 2,
    isPromoted: 1,
  },
  {
    name: 'pro',
    clickCount: 110,
    bookingCount: 57,
    isPromoted: 0,
  },
  {
    name: 'product',
    clickCount: 89,
    bookingCount: 67,
    isPromoted: 0,
  },
  {
    name: 'prod',
    clickCount: 13,
    bookingCount: 77,
    isPromoted: 0,
  },
  {
    name: 'produ',
    clickCount: 43,
    bookingCount: 10,
    isPromoted: 0,
  },
  {
    name: 'produc',
    clickCount: 5,
    bookingCount: 17,
    isPromoted: 0,
  },
]

我正在编写这个查询,但它没有给出想要的结果。

GET /products/_search
{
  
  "query": {
    "function_score": {
      "query": {
        "wildcard": {
          "name": "*pro*"
        }
      },
      
      "functions": [
         {
          "field_value_factor": {
            "field": "clickCount",
            "factor": 1.2,
            "modifier": "sqrt"
          }
         },
         {
          "field_value_factor": {
            "field": "clickCount",
            "factor": 1,
            "modifier": "sqrt"
          }
         },
         {
          "field_value_factor": {
            "field": "isPromoted",
            "factor": 1.5,
            "modifier": "sqrt"
          },
          "weight": 20
         }
        
      ],
      "boost_mode": "sum"
    }
  }
}

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-5


    【解决方案1】:

    对于你想要的,我认为你可以在运行时计算一个 totalCount 字段,它返回计数的总和。然后您只需先使用isPromoted 字段进行排序,然后使用totalCount 字段:

    映射:

    {
        "mappings": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "clickCount": {
                    "type": "integer"
                },
                "bookingCount": {
                    "type": "integer"
                },
                "isPromoted": {
                    "type": "integer"
                }
            }
        }
    }
    

    搜索查询:

    {
      "runtime_mappings": {
        "totalCount": {
          "type": "long",
          "script": "emit(doc['clickCount'].value + doc['bookingCount'].value);"
        }
      },
      "query": {
            "match_all": {}
      },
        "sort": [
            {"isPromoted": "desc"},
            {"totalCount": "desc"}
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多