当前位置: 首页 > news >正文

Java ‘Elasticsearch‘ 操作

依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><version>2.3.12.RELEASE</version>
</dependency>

配置

spring:elasticsearch:uris: http://192.168.0.226:9200

对象

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

添加索引操作

elasticsearchRestTemplate.indexOps(IndexCoordinates.of("indexname")).create();

IndexCoordinates可以给多个索引

删除索引

elasticsearchRestTemplate.indexOps(IndexCoordinates.of("indexname")).delete();

添加操作

IndexQuery indexQuery = new IndexQueryBuilder().withId(productId).withObject(product4ES).build();
elasticsearchRestTemplate.index(indexQuery, IndexCoordinates.of("products"));

查询操作

这个类是封装查询数据的

NativeSearchQuery query = new NativeSearchQueryBuilder()

要查询的字段

.withQuery(multiMatchQuery(keyword, "productName", "productSkuName"))

分页由于里面比尔帮你做好了就不要套用公式了

.withPageable(PageRequest.of(pageNum - 1, limit))

这里是高亮显示,用的是可变参数

.withHighlightFields(new HighlightBuilder.Field("productName").preTags("<label style='color: red'>").postTags("</label>"), new HighlightBuilder.Field("productSkuName").preTags("<label style='color: red'>").postTags("</label>"))

这里是构建

.build();

elasticsearchRestTemplate.search调用这个方法
search(封装的查询调教, 里面装的类型自动的, IndexCoordinates.of("要差的索引"))
SearchHits得到这个
search.getTotalHits();获取查询到的count

product4ES是我自己创建的然后装得到index

获取搜索的结果

List<SearchHit<Product4ES>> searchHits = search.getSearchHits();
创建一个等等用
List<Product4ES> list = new ArrayList<>();
for (SearchHit<Product4ES> hit : searchHits) {
获取返回的对象用什么对象装的什么对象拿自动的Product4ES product4ES = hit.getContent();
获取高亮map对象多个高亮map就有多个值Map<String, List<String>> highlightFields = hit.getHighlightFields();
获取叫下面这个名字的字段的高亮文本这里可能一段文字多个高亮List<String> productName = highlightFields.get("productName");if (!productName.isEmpty()) {
拿到第一个高亮,并设置product4ES.setProductName(productName.get(0));}
设置到对象list.add(product4ES);
}

计算总页数

int pageCount = ((int) (count % limit == 0 ? count / limit : count / limit + 1));
http://www.lryc.cn/news/299988.html

相关文章:

  • 【AI视野·今日NLP 自然语言处理论文速览 第七十八期】Wed, 17 Jan 2024
  • 实验5-4 使用函数计算两点间的距离
  • 【JavaEE】_JavaScript(Web API)
  • ARM交叉编译搭建SSH
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • 回归预测模型:MATLAB多项式回归
  • 「计算机网络」数据链路层
  • 【Linux】Ubuntu 22.04 升级 nodejs 到 v18
  • 当go get获取不到软件包时
  • 全网最详细解法|同济大学|高等数学|第八版|习题1-5
  • 可视化工具:将多种数据格式转化为交互式图形展示的利器
  • [嵌入式AI从0开始到入土]14_orangepi_aipro小修补含yolov7多线程案例
  • 机器学习、深度学习、强化学习、迁移学习的关联与区别
  • 苹果为什么需要台积电3nm工艺芯片?
  • 力扣:53. 最大子数组和
  • 幻兽帕鲁Palworld专用服务器CPU内存配置怎么选择?
  • 学习总结11
  • Hadoop运行环境搭建
  • CTFshow web(php命令执行59-67)
  • 03、全文检索 -- Solr -- Solr 身份验证配置(给 Solr 启动身份验证、添加用户、删除用户)
  • 怎么使用ChatGPT提高工作效率?
  • 【微服务】skywalking自定义告警规则使用详解
  • BUGKU-WEB 矛盾
  • 2024-02-11 Unity 编辑器开发之编辑器拓展2 —— 自定义窗口
  • Python 读取pdf文件
  • 人究其一生只是在通用智能模型基础上作微调和对齐
  • DS:二叉树的链式结构及实现
  • PhP+vue企业原材料采购系统_cxg0o
  • C++线程池
  • SpringCloud-Hystrix:服务熔断与服务降级