ElasticSearch7.1 高级搜索demo

本文详细介绍了使用Elasticsearch进行商品搜索的优化方法,包括mapping创建、DSL查询、keyword优化及constant_score应用,通过具体案例解析如何提升搜索精准度与效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求A:

搜索范围:商品名称、产品类目(四级、三级、二级、一级分类,分类从小到大筛选)

步骤1: 创建mapping

{
  "caicongyang-product": {
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "brandname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        },

        "cancelflag": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "createdate": {
          "type": "date"
        },
        "createuser": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "isimport": {
          "type": "long"
        },
   
        "modify_date": {
          "type": "date"
        },
        "modifyuser": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
    
      
        "product_id": {
          "type": "long"
        },
        "productalias": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
      
        "productcatename0": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        },
        "productcatename1": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        },
        "productcatename2": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        },
        "productcatename3": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        },
        "productcity": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "productionplacetext": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        },
        "productname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        },
       
     
        "state": {
          "type": "long"
        },
     
        "suppliername": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          },
          "analyzer": "ik_smart"
        }
       
      }
    }
  }
}

2. 搜索dsl

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "isimport":0
                    }
                },
                {
                    "match":{
                        "state":5
                    }
                },
                {
                    "match":{
                        "cancelflag":"0"
                    }
                },
                {
                    "bool":{
                        "should":[
                            {
                                "match":{
                                    "productname":"牛肉"
                                }
                            },
                            {
                                "match":{
                                    "productcatename0":{
                                        "query":"牛肉",
                                        "boost":10
                                    }
                                }
                            },
                            {
                                "match":{
                                    "productcatename1":{
                                        "query":"牛肉",
                                        "boost":8
                                    }
                                }
                            },
                            {
                                "match":{
                                    "productcatename2":{
                                        "query":"牛肉",
                                        "boost":6
                                    }
                                }
                            },
                            {
                                "match":{
                                    "productcatename3":{
                                        "query":"牛肉",
                                        "boost":6
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "sort":[
        {
            "_score":{
                "order":"desc"
            }
        }
    ],
    "from":0,
    "size":10
}

优化思考:

a. keyword 优化(试用上面搜索结果的二次搜索,精确匹配)

{ 
                             "match": {
                                 "suppliername.keyword": "福州东展国际贸易有限公司"
                             }
                         }

b..constant_score 优化 (减少score计算过程 )

  {
                             "constant_score": {
                               "filter": {"term": {
                                 "state": 5
                               }}
                               
                             }
                           }
                            

query bool 权重分析

bool 下级包含must, must_not,filter ,should

同⼀一层级下的竞争字段,具有有相同的权重

通过嵌套 bool 查询,可以改变对算分的影响;

 

后续:

es安装可以查看我的上篇文章:https://blog.csdn.net/caicongyang/article/details/107180168

如果我的文章对你有帮助,可以请我喝瓶水!

如果我的文章对你有帮助,可以请我喝瓶水!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值