赞
踩
Step5: 虚拟机中ElasticSearch进行如下配置,配置完成后重启ElasticSearch:
vim elasticsearch-6.8.0/plugins/ik/config/IKAnalyzer.cfg.xml
# 而后配置如下:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict"></entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">http://192.168.1.104:8989/mypoem/init.dic</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords"></entry>
</properties>
package com.salieri.entity; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @Document(type = "poem", indexName = "poems") public class Poem { @Id private String id; @Field(analyzer = "ik_max_word", type = FieldType.Text) private String name; @Field(type = FieldType.Keyword) private String author; @Field(type = FieldType.Keyword) private String type; @Field(analyzer = "ik_max_word", type = FieldType.Text) private String content; @Field(type = FieldType.Keyword) private String href; @Field(analyzer = "ik_max_word", type = FieldType.Text) private String authordes; @Field(type = FieldType.Keyword) private String origin; @Field(type = FieldType.Nested) private Category category = new Category(); ... } ================================================================= package com.salieri.entity; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; public class Category { @Field(type=FieldType.Keyword, index=false) private String id; @Field(type=FieldType.Keyword) private String name; ... }
Step3: 配置SpirngBoot连接ES
新建com.salieri.config.RestClientConfig并进行如下配置:
package com.salieri.config; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; @Configuration public class RestClientConfig extends AbstractElasticsearchConfiguration { @Value("${elasticsearch.rest.uri}") private String uri; //RestHighLevelClient dao PeomRepository @Override @Bean public RestHighLevelClient elasticsearchClient() { final ClientConfiguration clientConfiguration = ClientConfiguration.builder() .connectedTo(uri) .build(); return RestClients.create(clientConfiguration).rest(); } }
Step4: 新建com.salieri.elastic.repository.PoemRepository并进行如下配置
package com.salieri.elastic.repository;
import com.salieri.entity.Poem;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface PoemRepository extends ElasticsearchRepository<Poem,String> {
}
<!--segment-->
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>segment</artifactId>
<version>0.1.2</version>
</dependency>
segment的使用如图5所示:
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Step3: 配置文件中配置redis:
spring.redis.host=192.168.94.140
spring.redis.port=7000
Step4: PoemController中的相关代码:
// 放入redis if (!StringUtils.isEmpty(content)) { // 对搜索进行分词处理 List<String> segment = SegmentHelper.segment(content, SegmentResultHandlers.word()); log.info("当前搜索分词结果为:[{}]",segment); segment.forEach(word->{ if (word.length()>1) { stringRedisTemplate.opsForZSet().incrementScore("keywords",word,0.5); } }); } ======================== // 获取redis热词排行榜 @RequestMapping("findRedisKeywords") public Set<ZSetOperations.TypedTuple<String>> findRedisKeywords(){ Set<ZSetOperations.TypedTuple<String>> keywords = stringRedisTemplate.opsForZSet().reverseRangeWithScores("keywords", 0, 20); return keywords; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。