全文檢索工具elasticsearch:第四章:開(kāi)發(fā)電商的搜索列表功能
1、功能簡(jiǎn)介
1.1入口: 兩個(gè)
首頁(yè)的分類
搜索欄
列表展示頁(yè)面
2 根據(jù)業(yè)務(wù)搭建數(shù)據(jù)結(jié)構(gòu)
這時(shí)我們要思考三個(gè)問(wèn)題:
哪些字段需要分詞
我們用哪些字段進(jìn)行過(guò)濾
哪些字段我們需要通過(guò)搜索顯示出來(lái)。
需要分詞的字段
sku名稱 sku描述
分詞、定義分詞器
有可能用于過(guò)濾的字段
平臺(tái)屬性、三級(jí)分類、價(jià)格
要索引
其他需要顯示的字段
skuId 圖片路徑
不索引
第一種方式:
根據(jù)以上制定出如下結(jié)構(gòu):
執(zhí)行:
PUT gmall
{
"mappings": {
"SkuInfo":{
"properties": {
"id":{
"type": "keyword"
, "index": false
},
"price":{
"type": "double"
},
"skuName":{
"type": "text",
"analyzer": "ik_max_word"
},
"skuDesc":{
"type": "text",
"analyzer": "ik_smart"
},
"catalog3Id":{
"type": "keyword"
},
"skuDefaultImg":{
"type": "keyword",
"index": false
},
"skuAttrValueList":{
"properties": {
"valueId":{
"type":"keyword"
}
}
}
}
}
}
}
查看:
GET /gmall
結(jié)果:
{
"gmall": {
"aliases": {},
"mappings": {
"SkuLsInfo": {
"properties": {
"catalog3Id": {
"type": "keyword"
},
"hotScore": {
"type": "double"
},
"id": {
"type": "keyword",
"index": false
},
"price": {
"type": "double"
},
"skuAttrValueList": {
"properties": {
"attrId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"skuId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"valueId": {
"type": "keyword"
}
}
},
"skuDefaultImg": {
"type": "keyword",
"index": false
},
"skuDesc": {
"type": "text",
"analyzer": "ik_smart"
},
"skuName": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
},
"settings": {
"index": {
"creation_date": "1548161868908",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "1ZSLxTDOScubSqhf2c18rQ",
"version": {
"created": "5060499"
},
"provided_name": "gmall"
}
}
}
}
執(zhí)行測(cè)試類:
@RunWith(SpringRunner.class)
@SpringBootTest
public class GmallListServiceApplicationTests {
@Autowired
JestClient jestClient;
@Reference
SkuService skuService;
@Test
public void test02() throws IOException {
// 查詢sku表中的所有數(shù)據(jù)skuInfo
List<SkuInfo> skuInfos = skuService.SkuListByCatalog3Id("4");
// skuInfo轉(zhuǎn)化成skuLsInfo
for (SkuInfo skuInfo : skuInfos) {
SkuLsInfo skuLsInfo = new SkuLsInfo();
BeanUtils.copyProperties(skuInfo,skuLsInfo);
// 將skuLsInfo導(dǎo)入到es中
Index index = new Index.Builder(skuLsInfo).index("gmall").type("SkuLsInfo").id(skuLsInfo.getId()).build();
jestClient.execute(index);
}
}
}
@Override
public List<SkuInfo> SkuListByCatalog3Id(String catalog3Id) {
SkuInfo skuInfo = new SkuInfo();
skuInfo.setCatalog3Id(catalog3Id);
List<SkuInfo> skuInfos = skuInfoMapper.select(skuInfo);
for (SkuInfo info : skuInfos) {
SkuAttrValue skuAttrValue = new SkuAttrValue();
skuAttrValue.setSkuId(info.getId());
List<SkuAttrValue> skuAttrValues = skuAttrValueMapper.select(skuAttrValue);
info.setSkuAttrValueList(skuAttrValues);
}
return skuInfos;
}
再次查看
GET /gmall/SkuLsInfo/_search
數(shù)據(jù)庫(kù)中的數(shù)據(jù)就出來(lái)了
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "gmall",
"_type": "SkuLsInfo",
"_id": "105",
"_score": 1,
"_source": {
"id": "105",
"price": 1000,
"skuName": "360手機(jī)",
"skuDesc": "360N5,360N6",
"catalog3Id": "4",
"skuDefaultImg": "http://192.168.0.100/group1/M00/00/00/wKgAZFxF1V6AeGnrAAB0eitaW6M234.jpg",
"hotScore": 0,
"skuAttrValueList": [
{
"id": "770",
"attrId": "39",
"valueId": "90",
"skuId": "105"
},
{
"id": "771",
"attrId": "40",
"valueId": "90",
"skuId": "105"
},
{
"id": "772",
"attrId": "41",
"valueId": "90",
"skuId": "105"
}
]
}
},
{
"_index": "gmall",
"_type": "SkuLsInfo",
"_id": "101",
"_score": 1,
"_source": {
"id": "101",
"price": 5000,
"skuName": "三體的sku",
"skuDesc": "三體的sku描述",
"catalog3Id": "4",
"skuDefaultImg": "http://192.168.0.100/group1/M00/00/00/wKgAZFw_4VOAD-e4AACOjs59iN8474.jpg",
"hotScore": 0,
"skuAttrValueList": [
{
"id": "751",
"attrId": "39",
"valueId": "91",
"skuId": "101"
},
{
"id": "752",
"attrId": "40",
"valueId": "91",
"skuId": "101"
}
]
}
},
{
"_index": "gmall",
"_type": "SkuLsInfo",
"_id": "106",
"_score": 1,
"_source": {
"id": "106",
"price": 2000,
"skuName": "華為手機(jī)",
"skuDesc": "華為榮耀",
"catalog3Id": "4",
"skuDefaultImg": "http://192.168.0.100/group1/M00/00/00/wKgAZFxF1WWAOA3hAADKNM6pL68983.jpg",
"hotScore": 0,
"skuAttrValueList": [
{
"id": "773",
"attrId": "39",
"valueId": "90",
"skuId": "106"
},
{
"id": "774",
"attrId": "40",
"valueId": "90",
"skuId": "106"
},
{
"id": "775",
"attrId": "41",
"valueId": "90",
"skuId": "106"
}
]
}
},
{
"_index": "gmall",
"_type": "SkuLsInfo",
"_id": "102",
"_score": 1,
"_source": {
"id": "102",
"price": 55555,
"skuName": "三體第二部的sku名稱",
"skuDesc": "三體第耳部的sku描述",
"catalog3Id": "4",
"skuDefaultImg": "http://192.168.0.100/group1/M00/00/00/wKgAZFw_4VOAD-e4AACOjs59iN8474.jpg",
"hotScore": 0,
"skuAttrValueList": [
{
"id": "753",
"attrId": "39",
"valueId": "91",
"skuId": "102"
},
{
"id": "754",
"attrId": "40",
"valueId": "91",
"skuId": "102"
}
]
}
}
]
}
}
第二種方式:
之前已經(jīng)將數(shù)據(jù)庫(kù)中的數(shù)據(jù)保存到es中了,就可以用這種
@Test
public void contextLoads() throws IOException {
// 查詢
String dsl = getMyDsl();
System.out.print(dsl);
Search build = new Search.Builder(dsl).addIndex("gmall0906").addType("SkuLsInfo").build();
SearchResult execute = jestClient.execute(build);
List<SearchResult.Hit<SkuLsInfo, Void>> hits = execute.getHits(SkuLsInfo.class);
List<SkuLsInfo> skuLsInfos = new ArrayList<>();
for (SearchResult.Hit<SkuLsInfo, Void> hit : hits) {
SkuLsInfo source = hit.source;
skuLsInfos.add(source);
}
System.out.println(skuLsInfos.size());
}
public String getMyDsl() {
// 查詢語(yǔ)句封裝
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 聯(lián)合查詢
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
/* TermQueryBuilder termQueryBuilder = new TermQueryBuilder(null, null);
boolQueryBuilder.filter(termQueryBuilder);*/
//分詞查詢:按skuName中有0725查詢
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("skuName", "華為");
boolQueryBuilder.must(matchQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
//高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("skuName");
searchSourceBuilder.highlight(highlightBuilder);
return searchSourceBuilder.toString();
}