【商城】Elasticsearch搜索引擎-02.构建智能搜索系统客户端-实战

11 篇文章 0 订阅
5 篇文章 0 订阅
本文详细介绍了如何在Elasticsearch中为商品搜索定义复杂索引,包括分片、副本设置、分词器、标准化和嵌套搜索等关键点。同时,阐述了搜索客户端的核心内容,如检索、聚合、排序功能的实现,以及提供相应的接口和Builder模式。
摘要由CSDN通过智能技术生成

众所周知,Elasticsearch作为搜索引擎被广泛应用在各个领域, 索引作为Elasticsearch搜索实现核心之一,Elasticsearch底层使用倒排索引对分词进行文档映射,默认使用大数据常用的相似推荐算法(TF-IDF)进行打分,既实现文档高效检索,又完成文档评分排序。在上一篇构建搜索系统之查询-实战基础上,本章将说明如何定义索引,并实现完整检索客户端。

1.索引定义

下面以实战商品搜索为例,说明复杂的商品索引定义,常用关键点如下:

  1. 分片和副本:定义索引分片数和副本数;
  2. 分词器:analyzer,如果搜索中文,可自定义索引字段的分词器;
  3. 标准化:normalizer,对字段标准化统一处理;
  4. 嵌套:Elasticsearch支持嵌套搜索,常用在商品优惠活动(子文档)与商品(主文档)一起搜索。
{
  "aliases": {
    "index_product": {}
  },
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    },
    "analysis": {
      "normalizer": {
        "lowercase": {
          "type": "custom",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "index_product": {
      "_all": {
        "enabled": false
      },
      "dynamic_templates": [
        {
          "attribute_values": {
            "match_mapping_type": "*",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ],
      "properties": {
        "id": {
          "type": "long"
        },
        "spuId": {
          "type": "long"
        },
        "categoryId": {
          "type": "long"
        },
        "categoryIds": {
          "type": "long"
        },
        "productCode": {
          "type": "keyword"
        },
        "shopId": {
          "type": "long"
        },
        "shopName": {
          "type": "keyword"
        },
        "brandId": {
          "type": "long"
        },
        "brandName": {
          "type": "text",
          "analyzer": "keyword"
        },
        "name": {
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "nameKw": {
          "type": "keyword",
          "normalizer": "lowercase"
        },
        "mainImage": {
          "type": "keyword",
          "index": false
        },
        "status": {
          "type": "integer"
        },
        "type": {
          "type": "integer"
        },
        "businessType": {
          "type": "integer"
        },
        "bitTag": {
          "type": "long",
          "index": false
        },
        "updatedAt": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
        },
        "lowPrice": {
          "type": "long"
        },
        "highPrice": {
          "type": "long"
        },
        "keyword": {
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "saleQuantity": {
          "type": "long"
        },
        "inStock": {
          "type": "integer"
        },
        "shopCategoryIds": {
          "type": "long"
        },
        "source": {
          "type": "integer"
        },
        "channelList": {
          "type": "long"
        },
        "integral": {
          "type": "long"
        },
        "version": {
          "type": "keyword"
        },
        "颜色": {
          "type": "keyword"
        },
        "尺寸": {
          "type": "keyword"
        },
        "长度": {
          "type": "keyword"
        },
        "priceJson": {
          "type": "keyword",
          "index": false
        },
        "activityObject": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "long"
            },
            "code": {
              "type": "keyword"
            },
            "warmStartAt": {
              "type": "long"
            },
            "startAt": {
              "type": "long"
            },
            "expiredAt": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

2. 索引客户端

搜索客户端包括如下两部分核心内容;

搜索索引客户端

索引客户端

2.1 调用接口封装
/**
 * Copyright (c) 2020-2088 LEE POSSIBLE All Rights Reserved
 * Project:product-center
 * Package:com.leepossible.search.client
 * Version 1.0
 * 搜索客户端接口定义
 * @author <a href="mailto:974064580@163.com>lee possible</a>
 * @date 2021-02-06 10:07
 */
public interface SearchClient {
    /**
     * 搜索
     * @param request 搜索请求
     * @param <T> 类型
     * @return
     */
    <T>SearchResult<T> search(SearchRequest request);

    /**
     * 批量搜索
     * @param request 搜索请求
     * @return
     */
    MSearchResult search(MSearchRequest request);

    /**
     * 批量索引文档
     * @param request 批量请求
     * @return
     */
    BulkResult bulk(BulkRequest request);

    /**
     * 判断索引或索引别名是否存在
     * @param index 索引或索引别名
     * @return
     */
    boolean exist(String index);

    /**
     * 创建索引结构
     * @param index 索引名
     * @param json 索引结构
     * @return
     */
    boolean setup(String index, String json);

    /**
     * 通过别名从已存在的索引中创建一份新的索引(别名切换)
     * @param alias 别名
     * @return 新的索引名
     */
    String createFrom(String alias);

    /**
     * 将别名切换到新的索引上(同时删除别名旧指向,新索引必须存在才能切换)
     * @param alias 别名
     * @param newIndex 新索引
     * @return 旧索引
     */
    String switchAlias(String alias, String newIndex);

    /**
     * 关闭索引,不允许读写操作
     * @param index 索引名
     * @return
     */
    boolean close(String index);

    /**
     * 获取原生客户端
     * @param <T> 类型
     * @return
     */
    <T> T getClient();
}
2.2 聚合
/**
 * Copyright (c) 2020-2088 LEE POSSIBLE All Rights Reserved
 * Project:product-center
 * Package:com.leepossible.search.client.builder
 * Version 1.0
 * Aggregate聚合Builder
 * @author <a href="mailto:974064580@163.com>lee possible</a>
 * @date 2021-02-06 17:00
 */
public final class AggregateBuilder {
    /**
     * terms聚合
     *
     * @param field field
     * @param size  size
     * @return
     */
    public static TermsAggregate terms(String field, Integer size) {
        TermsAggregate aggregate = new TermsAggregate();
        aggregate.setField(field);
        aggregate.setSize(size);
        return aggregate;
    }

    /**
     * 度量聚合min
     *
     * @param field field
     * @return
     */
    public static MetricAggregate min(String field) {
        return MetricAggregate.builder().field(field).mode(MetricMode.min).build();
    }

    /**
     * 度量聚合max
     *
     * @param field field
     * @return
     */
    public static MetricAggregate max(String field) {
        return MetricAggregate.builder().field(field).mode(MetricMode.max).build();
    }

    /**
     * 度量聚合avg
     *
     * @param field field
     * @return
     */
    public static MetricAggregate avg(String field) {
        return MetricAggregate.builder().field(field).mode(MetricMode.avg).build();
    }

    /**
     * 度量聚合sum
     *
     * @param field field
     * @return
     */
    public static MetricAggregate sum(String field) {
        return MetricAggregate.builder().field(field).mode(MetricMode.sum).build();
    }
}
2.3 排序
/**
 * Copyright (c) 2020-2088 LEE POSSIBLE All Rights Reserved
 * Project:product-center
 * Package:com.leepossible.search.client.builder
 * Version 1.0
 * 排序builder模式
 * @author <a href="mailto:974064580@163.com>lee possible</a>
 * @date 2021-02-06 16:53
 */
public final class SortBuilder {
    /**
     * 字段排序
     * @param field 字段
     * @param order 排序方向
     * @return
     */
    public static Sort build(String field, Order order){
        return Sort.builder().field(field).order(order).build();
    }

    /**
     * 脚本排序
     * @param script Script脚本
     * @param order 排序方向
     * @param type 排序类型
     * @return
     */
    public static Sort build(Script script, Order order, String type){
        return Sort.builder().script(script).order(order).type(type).build();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Moutai码哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值