目录
1 通过es的mapping定义数据结构
Es的默认数据类型
定义如下es的mapping结构
2 导入Jest依赖
3 es的mysql数据导入
配置application.properties
定义es数据结构对应的java类
将mysql数据插入es中
测试查询
4 es的复杂查询
查询以下条件的手机
es中的并集处理
查询api
es用jest执行查询语句
查询dsl的封装工具类
5 搜索服务的完成+排序+高亮
定义查询参数的pojo
Controller
service
结果
PUT gmall0105
{
"mappings": {
"PmsSkuInfo":{
"properties": {
"id":{
"type": "double"
},
"skuName":{
"type": "text",
"analyzer": "ik_max_word"
},
"skuDesc":{
"type": "text"
, "analyzer": "ik_smart"
},
"catalog3Id":{
"type": "keyword"
},
"price":{
"type": "double"
},
"skuDefaultImg":{
"type": "keyword",
"index": false
},
"hotScore":{
"type": "double"
},
"productId":{
"type": "keyword"
},
"skuAttrValueList":{
"properties": {
"attrId":{
"type":"keyword"
},
"valueId":{
"type":"keyword"
}
}
}
}
}
}
}
注意:es里面的string类型(比如keyword和text在后面排序的时候可能存在问题,就不会变成数字比较,而是字符串比较。所以id定义为了double类型)
<dependency>
|
其中jest和jna请将版本号,部分纳入gmall-parent中管理。spring-boot-starter-data-elasticsearch不用管理版本号,其版本跟随springboot的1.5.10大版本号。 |
spring.elasticsearch.jest.uris=http://192.168.67.163:9200 |
public class PmsSkuSearchInfo implements Serializable {
@Id
private long id;
private String skuName;
private String skuDesc;
private String catalog3Id;
private BigDecimal price;
private String skuDefaultImg;
private double hotScore;
private String productId;
private List skuAttrValueList;
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class GmallSearchServiceApplicationTests {
@Reference
private SkuService skuService; //查询mysql
@Autowired
private JestClient jestClient;
@Test
public void put() throws IOException {
// 查询mysql数据
List pmsSkuInfoList = new ArrayList<>();
pmsSkuInfoList = skuService.getAllSku();
// 转化为es的数据结构
List pmsSearchSkuInfos = new ArrayList<>();
for (PmsSkuInfo pmsSkuInfo : pmsSkuInfoList) {
PmsSkuSearchInfo pmsSearchSkuInfo = new PmsSkuSearchInfo();
BeanUtils.copyProperties(pmsSkuInfo,pmsSearchSkuInfo);
pmsSearchSkuInfo.setId(Long.parseLong(pmsSkuInfo.getId()));
pmsSearchSkuInfos.add(pmsSearchSkuInfo);
}
// 导入es
for (PmsSkuSearchInfo pmsSearchSkuInfo : pmsSearchSkuInfos) {
Index put = new Index.Builder(pmsSearchSkuInfo).index("gmall0105").type("PmsSkuInfo").id(pmsSearchSkuInfo.getId()+"").build();
jestClient.execute(put);
}
}
}
注意:
1 查询所有名字中带华为
2 查询所有4寸以下,16G内存
Query{
Bool:{// 先过滤,后查询
Filter:{term,term}
must:{match}
}
}
在一个过滤条件中加入并集
Search search = new Search.Builder("dsl的json语句").addIndex("索引名").addType("表名").build();
@Test
public void get() throws IOException {
// Search search = new Search.Builder("dsl的json语句").addIndex("索引名").addType("表名").build();
// 用api执行复杂查询
List pmsSearchSkuInfos = new ArrayList<>();
Search search = new Search.Builder("{\n" +
" \"query\": {\n" +
" \"bool\": {\n" +
" \"filter\": [\n" +
" {\n" +
" \"term\": {\n" +
" \"skuAttrValueList.valueId\": \"39\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"term\": {\n" +
" \"skuAttrValueList.valueId\": \"45\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"must\": [\n" +
" {\n" +
" \"match\": {\n" +
" \"skuName\": \"小米\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }").addIndex("gmall0105").addType("PmsSkuInfo").build();
SearchResult execute = jestClient.execute(search);
List> hits = execute.getHits(PmsSkuSearchInfo.class);
for (SearchResult.Hit hit : hits) {
PmsSkuSearchInfo source = hit.source;
pmsSearchSkuInfos.add(source);
}
System.out.println(pmsSearchSkuInfos.size());
}
@Test
public void get() throws IOException {
// jest的dsl工具
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// bool
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
// filter
TermQueryBuilder termQueryBuilder = new TermQueryBuilder("skuAttrValueList.valueId","43");
boolQueryBuilder.filter(termQueryBuilder);
// must
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("skuName","小米");
boolQueryBuilder.must(matchQueryBuilder);
// query
searchSourceBuilder.query(boolQueryBuilder);
// from
searchSourceBuilder.from(0);
// size
searchSourceBuilder.size(20);
// highlight
searchSourceBuilder.highlight(null);
String dslStr = searchSourceBuilder.toString();
System.err.println(dslStr);
// Search search = new Search.Builder("dsl的json语句").addIndex("索引名").addType("表名").build();
// 用api执行复杂查询
List pmsSearchSkuInfos = new ArrayList<>();
Search search = new Search.Builder("").addIndex("gmall0105").addType("PmsSkuInfo").build();
SearchResult execute = jestClient.execute(search);
List> hits = execute.getHits(PmsSkuSearchInfo.class);
for (SearchResult.Hit hit : hits) {
PmsSkuSearchInfo source = hit.source;
pmsSearchSkuInfos.add(source);
}
System.out.println(pmsSearchSkuInfos.size());
}
(前端代码省略)
public class PmsSearchParam implements Serializable {
private String catalog3Id;
private String keyword;
private List skuAttrValueList;
}
@Controller
@CrossOrigin
public class SearchController {
@Reference
private SearchService searchService;
@RequestMapping("/list.html")
public String list(PmsSearchParam pmsSearchParam, ModelMap map){ //三级分类id,关键字
//调用搜索服务,返回搜索结果
List pmsSkuSearchInfos = searchService.list(pmsSearchParam);
map.put("skuLsInfoList",pmsSkuSearchInfos);
return "list";
}
}
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private JestClient jestClient;
@Override
public List list(PmsSearchParam pmsSearchParam) {
String dslStr = getSearchDsl(pmsSearchParam);
System.err.println(dslStr);
// 用api执行复杂查询
List pmsSearchSkuInfos = new ArrayList<>();
Search search = new Search.Builder(dslStr).addIndex("gmall0105").addType("PmsSkuInfo").build();
SearchResult execute = null;
try {
execute = jestClient.execute(search);
} catch (IOException e) {
e.printStackTrace();
}
List> hits = execute.getHits(PmsSkuSearchInfo.class);
for (SearchResult.Hit hit : hits) {
PmsSkuSearchInfo source = hit.source;
//获取高亮内容
Map> highlight = hit.highlight;
if(highlight!=null){
String skuName = highlight.get("skuName").get(0);
source.setSkuName(skuName);
}
pmsSearchSkuInfos.add(source);
}
System.err.println(pmsSearchSkuInfos.size());
return pmsSearchSkuInfos;
}
private String getSearchDsl(PmsSearchParam pmsSearchParam) {
List skuAttrValueList = pmsSearchParam.getSkuAttrValueList();
String keyword = pmsSearchParam.getKeyword();
String catalog3Id = pmsSearchParam.getCatalog3Id();
// jest的dsl工具
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// bool
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
// filter
if (StringUtils.isNotBlank(catalog3Id)){
TermQueryBuilder termQueryBuilder = new TermQueryBuilder("catalog3Id",catalog3Id);
boolQueryBuilder.filter(termQueryBuilder);
}
if (skuAttrValueList != null) {
for (PmsSkuAttrValue pmsSkuAttrValue : skuAttrValueList) {
TermQueryBuilder termQueryBuilder = new TermQueryBuilder("skuAttrValueList.valueId", pmsSkuAttrValue.getValueId());
boolQueryBuilder.filter(termQueryBuilder);
}
}
// must
if (StringUtils.isNotBlank(keyword)) {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("skuName", keyword);
boolQueryBuilder.must(matchQueryBuilder);
}
// query
searchSourceBuilder.query(boolQueryBuilder);
// from
searchSourceBuilder.from(0);
// size
searchSourceBuilder.size(20);
// highlight:高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("");//前缀
highlightBuilder.field("skuName");
highlightBuilder.postTags("");//后缀
searchSourceBuilder.highlight(highlightBuilder);
//sort
searchSourceBuilder.sort("id", SortOrder.DESC);
String dslStr = searchSourceBuilder.toString();
return dslStr;
}
}