In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Elasticsearch grading sorting background changes the scoring background through the script
Recently, there is a demand to add a ranking to the list of items available for coupons, only for face value, not including discount coupons.
The demand is like this. Suppose there is a face value coupon of 50 yuan, and the list of available goods A 100, B 40, C 10. When users query the list of products available for the current coupons, the card coupons can be deducted directly and the products that do not require users to pay extra are at the top of the list.
C 10
B 40
A 100
In fact, sorting has a lot of emphasis, such as:
1. According to the principle of maximizing the interests of users, the sorted list should be B, C, A
two。 According to the user's purchasing habits, it may be A, B, C
3. According to the operation strategy, third party interests, etc., it may be C, B, A
For the time being, let's not expand how to intelligently sort the product list. If you need a complete personalized product recommendation, it involves a lot of things, and there is experience to share later.
For this simple case, the most direct idea at the beginning is to add a sort sequence and write the sort value directly when building the index. Later, it is analyzed that the original index (index) structure is not the arrangement of this Cartesian product, so it is difficult to go online immediately in a short time, so it is necessary to build a new index structure.
Later, it is discussed that the method of affecting the score can be solved, which can save time and go online quickly.
Change the score through a script
ES query DSL supports many types of queries. If there is no special declaration, sort field is sorted according to the es score (score). The higher the score score, the higher the ranking.
ES score calculation is more complex, involving TF (word frequency) / IDF (inverse document frequency), rare words, matching document length, weighted boost vector space model, etc., but ES provides several encapsulated scoring plug-ins for use.
Function_score query allows us to change the document scoring method according to the business scenario. According to the business scenario, we need to fully control the logic of score generation, so we choose script_score.
Script_score
If the demand is beyond the above range, you can completely control the score calculation and achieve the required logic with a custom script.
(reference: https://www.elastic.co/guide/cn/elasticsearch/guide/current/function-score-query.html)
The script defaults to groovy, but you can also use other scripting languages as needed. Let's take a look at the implementation.
Script.inline: onscript.enfine.groovy.inline.aggs: onscript.indexed: onscript.file: on
First turn on the options for script support in the es.yml configuration.
{"query": {"function_score": {"query": {"bool": {"should": [{"match": {"productName": "English" "}]} "score_mode": "first", "script_score": {"lang": "groovy", "params": {"couponPrice": 100}, "script": "def deduct = couponPrice-doc ['unitCost'] .value.toFloat () If (deduct > 0) {return 10000 + deduct;} else if (deduct==0 | | (deduct0)) {return 20000;} else {return doc ['unitCost'] .value.toFloat ()-couponPrice;} "}," boost_mode ":" replace "}}," from ": 0," size ": 100}
The query condition can be arbitrary, the key is the _ _ script_score object, and script is the script code that needs to be executed by the ES__ script engine.
A more important option, boost_mode, boost_mode is to control the scoring method of the entire document, here we choose to replace (replace) the calculated score by default.
There is a little trick in sorting here, how to sort negative numbers first, positive numbers later, and how to deal with zero after deduction.
Def deduct= couponPrice-doc ['unitCost'] .value.toFloat (); if (deduct > 0) {return 10000 + deduct;} else if (deduct==0 | | (deduct0)) {return 20000;} else {return doc [' unitCost'] .value.toFloat ()-couponPrice;}
The face value of the coupon is represented by the couponPrice variable. If the current product deduction is negative, it means that it needs to be sorted first, then how to separate it from the positive value of the deduction? here you can take a slightly larger value plus the negative value after the deduction, so that converting a negative value into a positive number will naturally be sorted in front.
After deduction, the value equal to 0 or less than 1 greater than 0 can also be arranged first, of course, it is not flexible enough here, and the best way is to calculate accurately according to the current face value and commodity price.
Finally, the deduction is at the bottom of the list of extra payments, and the value of the amount that needs to be paid is directly sorted.
There are a lot of things we can do with ES scores, and this case is just a simple scenario.
Author: Wang Qingpei (Senior architect of Hujiang Group)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.