1.导包
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.1</version>
<exclusions>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.6.1</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--thymleaf相关-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
2.配置启动类
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client=new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")//可以配置多个es服务(自行搜索),这里配置的是本地的
)
);
return client;
}
}
3.查询es数据及查询字段高亮
@Autowired
private RestHighLevelClient restHighLevelClient;
@GetMapping("/prepareData")
public List<Map<String,Object>> prepareData() {
/*保存有问题
List<Pson> blogDOS = new ArrayList<>();
blogDOS.add(new Pson( "小明", "男", "22", 22.00));
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
blogDOS.stream().forEach(x -> {
bulkRequest.add(new IndexRequest("canal_pson").source(JSON.toJSONString(x), XContentType.JSON));
});
BulkResponse responses = null;
try {
responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return String.valueOf(responses.status());*/
/* 查询无问题
//生成搜索对象
SearchRequest request = new SearchRequest("canal_pson");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//设置分页参数
searchSourceBuilder.from(0);
searchSourceBuilder.size(2);
//设置搜索的字段
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "小飞");
searchSourceBuilder.query(matchQueryBuilder);
searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));
request.source(searchSourceBuilder);
SearchResponse search=null;
try {
search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
//将结果返回
List<Map<String,Object>> result=new ArrayList();
SearchHit[] hits = search.getHits().getHits();
for (SearchHit searchHit:hits){
result.add(searchHit.getSourceAsMap());
}
return result;*/
SearchRequest request = new SearchRequest("canal_pson");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(0);
searchSourceBuilder.size(2);
//高亮构造器
HighlightBuilder highlightBuilder=new HighlightBuilder();
//高亮查询字段
highlightBuilder.field("title");
//是否将所有匹配到的字段高亮显示,false表示只显示一个
highlightBuilder.requireFieldMatch(false);
//高亮的标签
highlightBuilder.preTags("<span style='color:red'>");
highlightBuilder.postTags("</span>");
searchSourceBuilder.highlighter(highlightBuilder);
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "小飞");
searchSourceBuilder.query(matchQueryBuilder);
searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));
request.source(searchSourceBuilder);
SearchResponse search=null;
try {
search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
List<Map<String,Object>> result=new ArrayList();
SearchHit[] hits = search.getHits().getHits();
//遍历结果,将高亮返回值title替换到原来的title中
for (SearchHit searchHit:hits){
Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
HighlightField title = highlightFields.get("name");
if (title!=null){
StringBuilder highLightTitle=new StringBuilder();
Text[] texts = title.fragments();
for(Text text:texts){
highLightTitle.append(text);
}
sourceAsMap.put("name",highLightTitle);
}
result.add(sourceAsMap);
}
return result;
}