หน้านี้ยังไม่พร้อมใช้งานในภาษาท้องถิ่นของคุณ เรากำลังพยายามอย่างหนักเพื่อเพิ่มเวอร์ชันภาษาอื่น ๆ เพิ่มเติม ขอบคุณสำหรับการสนับสนุนเสมอมา

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Cloud Search Service/ Best Practices/ Ranking Search Results Using Elasticsearch Custom Rules

Ranking Search Results Using Elasticsearch Custom Rules

Updated on 2025-01-06 GMT+08:00

You can use the Elasticsearch cluster to sort the search results based on customized rules.

Scenario

Elasticsearch is a highly scalable open-source search and analysis engine. It allows users to sort search results based on customized rules. Custom sorting enables the definition of specific sorting rules based on service requirements, optimizing the relevance of search results and enhancing user experience. This solution is applicable in the following scenarios:
  • E-commerce: Sort offerings based on factors such as sales volume, user comments, and prices.
  • Content Management: Sort articles or blog entries based on the number of views and publishing time.
  • Financial Services: Sort transaction records based on transaction amount, frequency, or risk score.
  • Customer Support: Sort customer requests based on the urgency or opening time of service tickets.

Solution Architecture

The sorting API in Elasticsearch is used to sort search results according to customized rules. By calling the sorting API, you can query and sort data based on customized rules.

You can query with customized rules using either of the following methods:

  • Calculate the final scores (new_score) of query results based on vote and sort the results in descending order.
    new_score = query_score x (vote x factor)
    • query_score: calculated based on the total number of search keywords found in a record. A record earns 1 point for each keyword it contains.
    • vote: vote of a record.
    • factor: user-defined weight of vote.
  • Calculate the final scores (new_score) of query results based on inline and sort the results in descending order.
    new_score = query_score x inline
    • query_score: calculated based on the total number of search keywords found in a record. A record earns 1 point for each keyword it contains.
    • vote: vote of a record.
    • inline: Configure two value options for this parameter and a threshold for vote. One option is used if vote exceeds the threshold, and the other is used if vote is smaller than or equal to the threshold. In this way, the query accuracy will not be affected by abnormal vote values.

Advantages

  • Flexibility: Customized sorting rules can meet various complex service requirements.
  • Scalability: The distributed nature of Elasticsearch supports horizontal expansion to accommodate increasing data volumes.
  • Performance: Elasticsearch's optimization mechanisms ensure efficient sorting operations, maintaining good performance even with large-scale datasets.
  • Real-time: The near real-time search capability of Elasticsearch ensures the timeliness of sorting results.

Prerequisites

An Elasticsearch cluster is available.

Procedure

NOTE:

The code examples in this section can only be used for clusters Elasticsearch 7.x or later.

  1. Log in to the CSS management console.
  2. In the navigation pane on the left, click Clusters to go to the Elasticsearch cluster list.
  3. Click Access Kibana in the Operation column of a cluster.
  4. In the navigation tree on the left of Kibana, choose Dev Tools. The command execution page is displayed.
  5. Create an index and specify a custom mapping to define the data type.

    For example, the content of the tv.json file is as follows:

    {
    "tv":[
    { "name": "tv1", "description": "USB, DisplayPort", "vote": 0.98 }
    { "name": "tv2", "description": "USB, HDMI", "vote": 0.99 }
    { "name": "tv3", "description": "USB", "vote": 0.5 }
    { "name": "tv4", "description": "USB, HDMI, DisplayPort", "vote": 0.7 }
    ]
    }

    Run the following command to create the mall index and specify the user-defined mapping to define the data type:

    PUT /mall?pretty
    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "description": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "vote": {
            "type": "float"
          }
        }
      }
    }
  6. Import data.
    Run the following command to import data in the tv.json file to the mall index:
    POST /mall/_bulk?pretty
    { "index": {"_id": "1"}}
    { "name": "tv1", "description": "USB, DisplayPort", "vote": 0.98 }
    { "index": {"_id": "2"}}
    { "name": "tv2", "description": "USB, HDMI", "vote": 0.99 }
    { "index": {"_id": "3"}}
    { "name": "tv3", "description": "USB", "vote": 0.5 }
    { "index": {"_id": "4"}}
    { "name": "tv4", "description": "USB, HDMI, DisplayPort", "vote": 0.7 }
  7. Query data based on customized rules. The query results can be scored based on vote or inline.
    Assume a user wants to query TVs with USB, HDMI, and/or DisplayPort ports. The final query score can be calculated in the following ways and used for sorting:
    • Scoring based on vote

      The score is calculated using the formula new_score = query_score x (vote x factor). Run the following command:

      GET /mall/_doc/_search?pretty
      {
        "query":{
          "function_score":{
            "query":{
              "bool":{
                "should":[
                  {"match": {"description": "USB"}},
                  {"match": {"description": "HDMI"}},
                  {"match": {"description": "DisplayPort"}}
                ]
              }
            },
            "field_value_factor":{
              "field":"vote",
              "factor":1
            },
            "boost_mode":"multiply",
            "max_boost":10
          }
        }
      }
      The query results are displayed in descending order of the score. The command output is as follows:
      {
        "took" : 4,
        "timed_out" : false,
        "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
        },
        "hits" : {
          "total" : {
            "value" : 4,
            "relation" : "eq"
          },
          "max_score" : 0.8388366,
          "hits" : [
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "4",
              "_score" : 0.8388366,
              "_source" : {
                "name" : "tv4",
                "description" : "USB, HDMI, DisplayPort",
                "vote" : 0.7
              }
            },
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "2",
              "_score" : 0.7428025,
              "_source" : {
                "name" : "tv2",
                "description" : "USB, HDMI",
                "vote" : 0.99
              }
            },
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "1",
              "_score" : 0.7352994,
              "_source" : {
                "name" : "tv1",
                "description" : "USB, DisplayPort",
                "vote" : 0.98
              }
            },
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "3",
              "_score" : 0.03592815,
              "_source" : {
                "name" : "tv3",
                "description" : "USB",
                "vote" : 0.5
              }
            }
          ]
        }
      }
    • Scoring based on inline.

      The score is calculated using the formula new_score = query_score x inline. In this example, if vote > 0.8, the value of inline is 1. If vote ≤ 0.8, the value of inline is 0.5. Run the following command:

      GET /mall/_doc/_search?pretty
      {
        "query":{
          "function_score":{
            "query":{
              "bool":{
                "should":[
                  {"match":{"description":"USB"}},
                  {"match":{"description":"HDMI"}},
                  {"match":{"description":"DisplayPort"}}
                ]
              }
            },
            "script_score": {
              "script": {
                "params": {
                  "threshold": 0.8
                },
                "inline": "if (doc[\"vote\"].value > params.threshold) {return 1;} return 0.5;"
              }
            },
            "boost_mode":"multiply",
            "max_boost":10
          }
        }
      }

      The query results are displayed in descending order of the score. The command output is as follows:

      {
        "took" : 4,
        "timed_out" : false,
        "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
        },
        "hits" : {
          "total" : {
            "value" : 4,
            "relation" : "eq"
          },
          "max_score" : 0.75030553,
          "hits" : [
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "1",
              "_score" : 0.75030553,
              "_source" : {
                "name" : "tv1",
                "description" : "USB, DisplayPort",
                "vote" : 0.98
              }
            },
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "2",
              "_score" : 0.75030553,
              "_source" : {
                "name" : "tv2",
                "description" : "USB, HDMI",
                "vote" : 0.99
              }
            },
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "4",
              "_score" : 0.599169,
              "_source" : {
                "name" : "tv4",
                "description" : "USB, HDMI, DisplayPort",
                "vote" : 0.7
              }
            },
            {
              "_index" : "mall",
              "_type" : "_doc",
              "_id" : "3",
              "_score" : 0.03592815,
              "_source" : {
                "name" : "tv3",
                "description" : "USB",
                "vote" : 0.5
              }
            }
          ]
        }
      }

เราใช้คุกกี้เพื่อปรับปรุงไซต์และประสบการณ์การใช้ของคุณ การเรียกดูเว็บไซต์ของเราต่อแสดงว่าคุณยอมรับนโยบายคุกกี้ของเรา เรียนรู้เพิ่มเติม

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback