文章目录
- 效果展示
- 二、使用步骤
- 1.引入maven依赖
- 2. 编写spplication.properties
- 3.配置客户端
- 4.高亮搜索
- 5.转换成实体对象
- 总结
效果展示
虽然看起来巴适,但是挺简单的!
📝 英文检索
📝 中文单字检索
二、使用步骤
1.引入maven依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2. 编写spplication.properties
elasticsearch.host=localhost
elasticsearch.port=9200
3.配置客户端
/*** 配置客户端* @author BreezAm*/
@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class EsConfig extends AbstractElasticsearchConfiguration {private String host;private int port;@Overridepublic RestHighLevelClient elasticsearchClient() {RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));return new RestHighLevelClient(builder);}
}
4.高亮搜索
/*** 文章搜索** @author BreezAm* @email lx90352@163.com* @date 2021-08-15 18:05:46*/
@Service
public class EsSearchService {@Autowiredprivate RestHighLevelClient restHighLevelClient;/*** 高亮搜索** @param indices 索引名称* @param name 搜索的字段 高亮显示的字段* @param key 搜索的关键字* @return* @throws IOException*/public List<Map<String, Object>> search(String indices, String name, String key) throws IOException {List<Map<String, Object>> list = new ArrayList<>();SearchRequest request = new SearchRequest();request.indices(indices);SearchSourceBuilder builder = new SearchSourceBuilder();TermQueryBuilder queryBuilder = QueryBuilders.termQuery(name, key);HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.requireFieldMatch(false);highlightBuilder.preTags("<font color='red' size=4>");highlightBuilder.postTags("</font>");highlightBuilder.field(name);builder.highlighter(highlightBuilder);builder.query(queryBuilder);request.source(builder);SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);SearchHits hits = search.getHits();for (SearchHit hit : hits) {Map<String, Object> sourceAsMap = hit.getSourceAsMap();Map<String, HighlightField> fields = hit.getHighlightFields();HighlightField title = fields.get(name);if (title != null) {Text[] fragments = title.getFragments();String t = "";for (Text text : fragments) {t = text + t;}sourceAsMap.put(name, t);}list.add(sourceAsMap);}return list;}
}
5.转换成实体对象
private List<Article> getArticles(String key) throws IOException {List<Article> list = new ArrayList<>();List<Map<String, Object>> search = esSearchService.search("article", "title", key);for (Map<String, Object> map : search) {Article article = new Article();article.setId(Integer.valueOf(map.get("id").toString()));article.setTitle(map.get("title").toString());article.setCoverUrl(map.get("coverUrl").toString());article.setContent(map.get("content").toString());article.setPublishDate(stringToDate(map.get("publishDate").toString()));article.setReadNum(Integer.valueOf(map.get("readNum").toString()));article.setCommentNum(Integer.valueOf(map.get("commentNum").toString()));article.setSummary(map.get("summary").toString());list.add(article);}return list;}
总结
本文简单实现了Elasticsearch的一个应用场景,其中还有些问题,那就是对中文只能单字搜索,英文可以进行单词检索,如果要对中文进行多字检索,那就需要使用中文分词器,推荐一款中文分词器:elasticsearch-analysis-ik