1.导入测试数据
2. 搜索功能样例数据
{ "index": {"_index": "pditems", "_id": "536563"}}
{ "id":"536563","brand":"联想","title":"联想(Lenovo)小新Air13 Pro 13.3英寸14.8mm超轻薄笔记本电脑","sell_point":"清仓!仅北京,武汉仓有货!","price":"6688.0","barcode":"","image":"/images/server/images/portal/air13/little4.jpg","cid":"163","status":"1","created":"2015-03-08 21:33:18","updated":"2015-04-11 20:38:38"}
3.创建索引和映射
PUT /pditems
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"brand": {
"type": "text",
"analyzer": "ik_smart"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"sell_point": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"price": {
"type": "float"
},
"image": {
"type": "keyword"
},
"cid": {
"type": "long"
},
"status": {
"type": "byte"
},
"created": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"updated": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
说明:“number_of_shards”: 3 ,创建分片数为3, “number_of_replicas”: 2 ,每个分片创建两个副本.mappings映射,properties映射所有字段.以及规定了搜索的方式.
在Kibana工具中执行上述代码,创建索引.
4.用head查看索引
边框粗的代表是主控,下边数字对应的是对应主控的副本,当主服务器宕机,则会从副本中进行数据操作.
5.导入数据
在服务器上,进入 pditems.json 所在的文件夹,执行批量数据导入:
curl -XPOST 'localhost:9200/pditems/_bulk' \
-H 'Content-Type:application/json' \
--data-binary @pditems.json
6.查看数据
搜索 pditems 索引中全部 3160 条数据:
GET /pditems/_search
{
"query": {
"match_all": {
}
},
"size": 3160
}
7.搜索所有数据
搜索 pditems 索引中全部数据
POST /pditems/_search
{
"query": {
"match_all": {
}
}
}
8.关键词搜索
# 查询 pditems 索引中title中包含"电脑"的商品
POST /pditems/_search
{
"query": {
"match": {
"title": "电脑"
}
}
}
9.搜索结果过滤器
多个条件搜索,需要对结果进行过滤,例如:搜索价格大于2000,并且title中包含"电脑"的商品.我们搜索时"电脑"设置必须匹配,"价格大于2000"是一个范围,范围用range来表示,gte表示大于.
# 价格大于2000,并且title中包含"电脑"的商品
POST /pditems/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "电脑"
}
}
],
"filter": [
{
"range": {
"price": {
"gte": "2000"
}
}
}
]
}
}
}
10.搜索结果高亮显示
例如我们在优快云中搜索一个关键词,关键词在结果中的颜色或亮度会突出展现,我们可以通过样式来设置.
POST /pditems/_search
{
"query": {
"multi_match":{
"query": "手机",
"fields": ["title", "sell_point"]
}
},
"highlight" : {
"pre_tags" : ["<i class=\"highlight\">"],
"post_tags" : ["</i>"],
"fields" : {
"title" : {
},
"sell_point" : {
"pre_tags": "<em>",
"post_tags": "</em>"
}
}
}
}
11.Elasticsearch实现搜索框案例:
a) 添加ItemRepository类,设置高亮显示搜索结果
package com.pd.es;
import com.pd.pojo.Item;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ItemRepository
extends ElasticsearchRepository<Item, Long> {
@Highlight(
parameters = @HighlightParameters(
preTags = "<em>",
postTags = "</em>"
),
fields = @HighlightField(name="title")
)
List<SearchHit<Item>> findByTitleOrSellPoint(String key1, String key2, Pageable pageable);
}
b)新建service接口
package com.pd.service;
import com.pd.pojo.Item;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchHit;
import java.util.List;
public interface SearchService {
List<SearchHit<Item>> search(String key, Pageable pageable);
}
c)新建controller
package com.pd.controller;
import com.pd.pojo.Item;
import com