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

ElasticSearch学习笔记-解析JSON格式的内容

如果需要屏蔽其他项目对Elasticsearch的直接访问操作,统一由一个入口访问操作Elasticsearch,可以考虑直接传入JSON格式语句解析执行。
  • 相关依赖包
<properties><elasticsearch.version>7.9.3</elasticsearch.version><elasticsearch.rest.version>7.9.3</elasticsearch.rest.version>
</properties><!-- Elasticsearch依赖包-->
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elasticsearch.version}</version>
</dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId><version>${elasticsearch.version}</version>
</dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>${elasticsearch.rest.version}</version>
</dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elasticsearch.rest.version}</version>
</dependency>
  • 部分代码
protected static final NamedXContentRegistry namedXContentRegistry;static {SearchModule searchModule = new SearchModule(Settings.builder().build(), false, new ArrayList<>());List<NamedXContentRegistry.Entry> namedXContents = searchModule.getNamedXContents();namedXContents.addAll(getDefaultNamedXContents());namedXContentRegistry = new NamedXContentRegistry(namedXContents);}protected SearchResponse searchResponse(RestHighLevelClient restHighLevelClient, String statement,Map<String, Object> params, String... indices) {SearchResponse searchResponse = null;SearchRequest searchRequest = new SearchRequest(indices);SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();try {searchSourceBuilder.parseXContent(XContentType.JSON.xContent().createParser(namedXContentRegistry,DeprecationHandler.IGNORE_DEPRECATIONS, statement));searchRequest.source(searchSourceBuilder);Object scrollTimeoutObj = params.get(SCROLL_TIMEOUT);if (null != scrollTimeoutObj) {searchRequest.scroll(String.valueOf(scrollTimeoutObj));}long startTimeMillis = System.currentTimeMillis();searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);executionSqlLog(statement, startTimeMillis, indices);} catch (IOException e) {log.error(e.getMessage(), e);}return searchResponse;}protected SearchResponse scrollSearchResponse(RestHighLevelClient restHighLevelClient, String scrollId) {try {return restHighLevelClient.scroll(new SearchScrollRequest(scrollId), RequestOptions.DEFAULT);} catch (IOException e) {log.error(e.getMessage(), e);}return null;}protected CountResponse countResponse(RestHighLevelClient restHighLevelClient, String statement,Map<String, Object> params, String... indices) {CountResponse countResponse = null;CountRequest countRequest = new CountRequest(indices);SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();try {searchSourceBuilder.parseXContent(XContentType.JSON.xContent().createParser(namedXContentRegistry,DeprecationHandler.IGNORE_DEPRECATIONS, statement));countRequest.source(searchSourceBuilder);long startTimeMillis = System.currentTimeMillis();countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);executionSqlLog(statement, startTimeMillis, indices);} catch (IOException e) {log.error(e.getMessage(), e);}return countResponse;}private static List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {Map<String, ContextParser<Object, ? extends Aggregation>> map = new HashMap<>();map.put(TopHitsAggregationBuilder.NAME, (p, c) -> ParsedTopHits.fromXContent(p, (String) c));map.put(DateRangeAggregationBuilder.NAME, (p, c) -> ParsedDateRange.fromXContent(p, (String) c));map.put(FilterAggregationBuilder.NAME, (p, c) -> ParsedFilter.fromXContent(p, (String) c));map.put(SumAggregationBuilder.NAME, (p, c) -> ParsedSum.fromXContent(p, (String) c));map.put(CardinalityAggregationBuilder.NAME, (p, c) -> ParsedCardinality.fromXContent(p, (String) c));map.put(StringTerms.NAME, (p, c) -> ParsedStringTerms.fromXContent(p, (String) c));map.put(LongTerms.NAME, (p, c) -> ParsedLongTerms.fromXContent(p, (String) c));map.put(DoubleTerms.NAME, (p, c) -> ParsedDoubleTerms.fromXContent(p, (String) c));return map.entrySet().stream().map(entry ->new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue())).collect(Collectors.toList());}private void executionSqlLog(String statement, long startTimeMillis, String... indices) {if (!esProperties.isExecutionSqlEnable()) {return;}long spendTime = System.currentTimeMillis() - startTimeMillis;log.info("\n==============  SQL START  ==============" +"\nExecution INX :{} [{} ms]" +"\nExecution SQL :{}" +"\n==============  SQL END    ==============\n", String.join(Constants.SYMBOL_COMMA, indices),spendTime, statement.replaceAll("\\s{2,}", " "));}}
http://www.lryc.cn/news/531891.html

相关文章:

  • 浅谈密码相关原理及代码实现
  • Spring Boot常用注解深度解析:从入门到精通
  • can not add outlook new accounts on the outlook
  • 私有化部署 DeepSeek + Dify,构建你的专属私人 AI 助手
  • 【Elasticsearch】post_filter
  • 验证工具:GVIM和VIM
  • 如何优化垃圾回收机制?
  • beyond the ‘PHYSICAL‘ memory limit.问题处理
  • Day36【AI思考】-表达式知识体系总览
  • 段错误(Segmentation Fault)调试
  • 每日Attention学习19——Convolutional Multi-Focal Attention
  • LeetCode题练习与总结:三个数的最大乘积--628
  • Colorful/七彩虹 隐星P15 TA 24 原厂Win11 家庭版系统 带F9 Colorful一键恢复功能
  • 第二篇:多模态技术突破——DeepSeek如何重构AI的感知与认知边界
  • CTreeCtrl 设置图标
  • 在JAX-RS中获取请求头信息的方法
  • Java 面试之结束问答
  • 柔性数组与c/c++程序中内存区域的划分
  • mini-lsm通关笔记Week2Day7
  • Typora免费使用
  • AI驱动的无线定位:基础、标准、最新进展与挑战
  • 苹果再度砍掉AR眼镜项目?AR真的是伪风口吗?
  • 18 大量数据的异步查询方案
  • DRM系列八:Drm之DRM_IOCTL_MODE_ADDFB2
  • 软件测试用例篇
  • PopupMenuButton组件的功能和用法
  • Python进行模型优化与调参
  • vue2-组件通信
  • 20250205确认荣品RK3566开发板在Android13下可以使用命令行reboot -p关机
  • 设计模式---观察者模式