JAVA全系列 教程
3762个小节阅读:7090.7k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在向ES添加数据时,我们需要将数据库的一些字段进行分词作为商品的关键词,方便编写补齐关键词功能,在搜索服务接口实现类编写分词方法:
xxxxxxxxxx
@DubboService
public class GoodsESServiceImpl implements GoodsESService {
@Autowired
private GoodsESRepository goodsESRepository;
@Autowired
private ElasticsearchRestTemplate template;
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 分词
* @param text 被分词的文本
* @param analyzer 分词器
* @return 分词结果
*/
@SneakyThrows // 抛出已检查异常
public List<String> analyze(String text, String analyzer){
// 分词请求
AnalyzeRequest request = AnalyzeRequest.withIndexAnalyzer("goods",analyzer, text);
// 分词响应
AnalyzeResponse response = restHighLevelClient.indices().analyze(request, RequestOptions.DEFAULT);
// 分词结果集合
List<String> words = new ArrayList<>();
// 处理响应
List<AnalyzeResponse.AnalyzeToken> tokens = response.getTokens();
for (AnalyzeResponse.AnalyzeToken token : tokens) {
String term = token.getTerm(); // 分出的词
words.add(term);
}
return words;
}
}
在测试时,发现JAVA项目无法连接ES,这是由于ES默认不允许远程访问。kibana由于和ES在同一台服务器下所以可以访问,JAVA程序在开发电脑中所以无法访问。我们修改es的配置文件,开启远程访问功能:
打开ES配置文件
xxxxxxxxxx
vim /usr/local/elasticsearch/config/elasticsearch.yml
添加如下内容
xxxxxxxxxx
# 单体ES环境
discovery.type: single-node
# 允许所有路径访问
network.host: 0.0.0.0
重启ES和kibana。