uot;ik_smart\",\n" +
" \"fields\": {\n" +
" \"keyword\" : {\"ignore_above\" : 256, \"type\" : \"keyword\"}\n" +
" }\n" +
" },\n" +
" \"venue\" : {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\", \n" +
" \"search_analyzer\": \"ik_smart\",\n" +
" \"fields\": {\n" +
" \"keyword\" : {\"ignore_above\" : 256, \"type\" : \"keyword\"}\n" +
" }\n" +
" },\n" +
" \"currentPrice\" : {\n" +
" \"type\": \"keyword\",\n" +
" \"ignore_above\": 64\n" +
" }\n" +
" }\n" +
"}";
clientService.createIndex("idx_item", settings, mappings);
}
分词技巧:
- 索引时最小分词,搜索时最大分词,例如"Java知音"索引时分词包含Java、知音、音、知等,最小粒度分词可以让我们匹配更多的检索需求,但是我们搜索时应该设置最大分词,用“Java”和“知音”去匹配索引库,得到的结果更贴近我们的目的,
- 对分词字段同时也设置keyword,便于后续排查错误时可以精确匹配搜索,快速定位。
我们向es导入十万条淘宝双11活动数据作为我们的样本数据,数据结构如下所示
{
"_id": "https://detail.tmall.com/item.htm?id=538528948719\u0026skuId=3216546934499",
"卖家地址": "上海",
"快递费": "运费: 0.00元",
"优惠活动": "满199减10,满299减30,满499减60,可跨店",
"商品ID": "538528948719",
"原价": "2290.00",
"活动开始时间": "2016-11-11 00:00:00",
"活动结束时间": "2016-11-11 23:59:59",
"标题": "【天猫海外直营】 ReFa CARAT RAY 黎珐 双球滚轮波光美容仪",
"服务保障": "正品保证;赠运费险;极速退款;七天退换",
"会场": "进口尖货",
"现价": "1950.00"
}
调用上面封装的批量导入方法进行导入
@Test
public void importAll() throws IOException {
clientService.importAll("idx_item", true, itemService.getItemsJson());
}
我们调用封装的搜索方法进行搜索,搜索产地为武汉、价格在11-149之间的相关酒产品,这与我们淘宝中设置筛选条件搜索商品操作一致。
@Test
public void search() throws IOException {
SearchResponse search = clientService.search("title", "酒", "currentPrice",
"11", "149", "sellAddress", "武汉");
SearchHits hits = search.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println( documentFields.getSourceAsString());
}
}
我们得到以下搜索结果,其中_score为某一项的得分,商品就是按照它来排序。
{
"_index": "idx_item",
"_type": "_doc",
"_id": "Rw3G7HEBDGgXwwHKFPCb",
"_score": 10.995819,
"_source": {
"itemId": "525033055044",
"urlId": "https://detail.tmall.com/item.htm?id=525033055044&skuId=def",
"sellAddress": "湖北武汉",
"courierFee": "快递: 0.00",
"promotions": "满199减10,满299减30,满499减60,可跨店",
"originalPrice": "3768.00",
"startTime": "2016-11-01 00:00:00",
"endTime": "2016-11-11 23:59:59",
"title": "酒嗨酒 西班牙原瓶原装进口