1.准备测试数据
DELETE blog
PUT blog
GET blog/_mapping
PUT blog/article/_mapping
{
"dynamic":"strict",
"properties": {
"id":{
"type": "long"
},
"title":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"content":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"postdate":{
"type": "date",
"format": "yyyy-MM-dd HH:mm || yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
}
}
PUT blog/article/1
{
"id":1,
"title":"git简介",
"content":"Svn与Git的最主要区别。",
"postdate":"2016-06-09"
}
PUT blog/article/2
{
"id":2,
"title":"Java中泛型的介绍与简单应用",
"content":"学习目标 掌握泛型的产生意义。",
"postdate":"2016-08-06"
}
PUT blog/article/3
{
"id":3,
"title":"SQL的基本操作",
"content":"基本操作 CRUD。",
"postdate":"2017-05-06"
}
PUT blog/article/4
{
"id":4,
"title":"Hibernate框架基础",
"content":"Hibernate框架基础。",
"postdate":"2017-08-03"
}
PUT blog/article/5
{
"id":5,
"title":"Shell基本知识",
"content":"Shell是什么。",
"postdate":"2017-11-03"
}
PUT blog/article/6
{
"id":6,
"title":"Java基本知识",
"content":"Java是什么。",
"postdate":"2017-11-15"
}
PUT blog/article/7
{
"id":7,
"title":"Java连接mysql",
"content":"通过mybaits连接mysql。",
"postdate":"2018-12-15"
}
PUT blog/article/8
{
"id":8,
"title":"什么语言最好",
"content":"Java是世上最好的言。",
"postdate":"2018-12-15"
}
PUT blog/article/9
{
"id":9,
"title":"Java",
"content":"Java语言博大精深。",
"postdate":"2018-10-15"
}
PUT blog/article/10
{
"id":10,
"title":"什么语言最好",
"content":"Python是世上最好的言。",
"postdate":"2018-10-15"
}
PUT blog/article/11
{
"id":11,
"title":"Java基本思想",
"content":"面向对象的编程思想。",
"postdate":"2018-07-15"
}
2.测试方法:
@RequestMapping(value ="/searchElastic")
@ResponseBody
public Map<String,Object> searchElastic(String queryText,int pageNum) {
long startTime = System.currentTimeMillis();
Map<String,Object> map = new HashMap<String,Object>();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
TransportClient client = ESClientUtil.getSingleClient();
MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery(queryText, "title","content");
HighlightBuilder highlightBuilder = new HighlightBuilder()
.preTags("<span style=\"color:red\">")
.postTags("</span>")
.field("title")
.field("content");
SearchResponse response = client.prepareSearch("blog")
.setTypes("article")
.setQuery(multiMatchQuery)
.highlighter(highlightBuilder)
.setFrom((pageNum-1)*3)//开始进入默认第一页
.setSize(3)
.execute()
.actionGet();
SearchHits hits = response.getHits();
long total = hits.getTotalHits(); //搜索的总数目
for(SearchHit hit:hits) {
Map<String, Object> hitMap = hit.getSourceAsMap();
//--------高亮字段
HighlightField hlTitle = hit.getHighlightFields().get("title");
if(hlTitle!=null) {
Text[] fragment = hlTitle.fragments();
String hlString = "";
for(Text text:fragment) {
hlString+=text;
}
hitMap.put("title", hlString);
}
HighlightField hlConment = hit.getHighlightFields().get("conment");
if(hlConment!=null) {
Text[] fragment = hlConment.fragments();
String hlStr = "";
for(Text text:fragment) {
hlStr+=text;
}
hitMap.put("conment", hlStr);
}
//-----------
list.add(hitMap);
System.out.println(hit.getSourceAsMap());
}
long endTime = System.currentTimeMillis();
map.put("list", list);
map.put("total", total);
map.put("time", endTime - startTime); //耗时
return map;
}
启动项目并访问:http://localhost:8080/searchElastic?queryText="Java"&&pageNum=1
结果: