本文共 4030 字,大约阅读时间需要 13 分钟。
众所周知,Elasticsearch作为搜索引擎被广泛应用在各个领域, 索引作为Elasticsearch搜索实现核心之一,Elasticsearch底层使用倒排索引对分词进行文档映射,默认使用大数据常用的相似推荐算法(TF-IDF)进行打分,既实现文档高效检索,又完成文档评分排序。在上一篇基础上,本章将说明如何定义索引,并实现完整检索客户端。
下面以实战商品搜索为例,说明复杂的商品索引定义,常用关键点如下:
{ "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" } } } } } }}
搜索客户端包括如下两部分核心内容;
/** * Copyright (c) 2020-2088 LEE POSSIBLE All Rights Reserved * Project:product-center * Package:com.leepossible.search.client * Version 1.0 * 搜索客户端接口定义 * @author
/** * Copyright (c) 2020-2088 LEE POSSIBLE All Rights Reserved * Project:product-center * Package:com.leepossible.search.client.builder * Version 1.0 * Aggregate聚合Builder * @author
/** * Copyright (c) 2020-2088 LEE POSSIBLE All Rights Reserved * Project:product-center * Package:com.leepossible.search.client.builder * Version 1.0 * 排序builder模式 * @author
转载地址:http://eccpi.baihongyu.com/