ElasticSearch - JAVAAPI练习

索引

package com.chun.estest.demo;import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;/*** @author Chun* @create 2021-06-09 14:35**/
public class EsTest_Client {RestHighLevelClient esClient = null;@Beforepublic void after(){//创建ES客户端esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));}//索引@Testpublic void index_create() throws IOException {//创建索引CreateIndexRequest request = new CreateIndexRequest("user");CreateIndexResponse response = esClient.indices().create(request, RequestOptions.DEFAULT);//响应状态boolean acknowledged = response.isAcknowledged();System.out.println("索引操作:"+acknowledged);//关闭esClient.close();}@Testpublic void index_search() throws IOException {GetIndexRequest user = new GetIndexRequest("user");GetIndexResponse response = esClient.indices().get(user, RequestOptions.DEFAULT);System.out.println(response.getAliases());System.out.println(response.getMappings());System.out.println(""+response.getSettings());}@Testpublic void index_delete() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("user");AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);System.out.println("是否删除成功:"+response.isAcknowledged());}//数据@Testpublic void doc_create() throws IOException {IndexRequest request = new IndexRequest();request.index("user").id("1001");User user = new User("张三","男",30);ObjectMapper mapper = new ObjectMapper();String userJson = mapper.writeValueAsString(user);request.source(userJson, XContentType.JSON);IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);System.out.println(response.getResult());}@Testpublic void doc_update() throws IOException {UpdateRequest request = new UpdateRequest();request.index("user").id("1001");request.doc(XContentType.JSON,"sex","女");
//        User user = new User("张三","男",30);
//        ObjectMapper mapper = new ObjectMapper();
//        String userJson = mapper.writeValueAsString(user);
//        request.source(userJson, XContentType.JSON);UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);System.out.println(response.getResult());}@Testpublic void doc_search() throws IOException {GetRequest request = new GetRequest();request.index("user").id("1001");
//        User user = new User("张三","男",30);
//        ObjectMapper mapper = new ObjectMapper();
//        String userJson = mapper.writeValueAsString(user);
//        request.source(userJson, XContentType.JSON);GetResponse response = esClient.get(request, RequestOptions.DEFAULT);System.out.println(response.getSourceAsString());}@Testpublic void doc_delete() throws IOException {DeleteRequest request = new DeleteRequest();request.index("user").id("1001");
//        User user = new User("张三","男",30);
//        ObjectMapper mapper = new ObjectMapper();
//        String userJson = mapper.writeValueAsString(user);
//        request.source(userJson, XContentType.JSON);DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);System.out.println(response.getResult());}@Afterpublic void After() throws IOException {esClient.close();}}

数据

package com.chun.estest.demo;import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.util.Arrays;/*** @author Chun* @create 2021-06-09 14:35**///批量操作
public class EsTest_Client_Batch {RestHighLevelClient esClient = null;@Beforepublic void after(){//创建ES客户端esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));}@Testpublic void doc_Insert_Batch() throws IOException {BulkRequest request = new BulkRequest();request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","zhangsan","age",30));request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","lisi","age",40));request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","wangwu","age",20));BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);System.out.println(response.getTook());System.out.println(Arrays.toString(response.getItems()));}@Testpublic void doc_Delete_Batch() throws IOException {BulkRequest request = new BulkRequest();request.add(new DeleteRequest().index("user").id("1001"));request.add(new DeleteRequest().index("user").id("1002"));request.add(new DeleteRequest().index("user").id("1003"));BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);System.out.println(response.getTook());System.out.println(Arrays.toString(response.getItems()));}@Testpublic void doc_Search_all_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//条件查询@Testpublic void doc_Search_Where_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("name","zhangsan")));SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//条件查询@Testpublic void doc_Search_Page_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());builder.from(2).size(2);    //起始位置 - 第几页request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//排序查询@Testpublic void doc_Search_OrderBy_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());builder.sort("age", SortOrder.ASC);
//        builder.from(2).size(2);    //起始位置 - 第几页request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//过滤列@Testpublic void doc_Search_Filter_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());String [] excludes = {"age"};   //排除哪些字段String [] includes = {};        //包含哪些字段builder.fetchSource(includes,excludes);
//        builder.from(2).size(2);    //起始位置 - 第几页request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//组合查询@Testpublic void doc_Search_Bool_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//and
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age",30));
//        boolQueryBuilder.must(QueryBuilders.matchQuery("name","zhangsan"));
//        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","lisi"));//or
//        boolQueryBuilder.should(QueryBuilders.matchQuery("age",30));
//        boolQueryBuilder.should(QueryBuilders.matchQuery("age",40));builder.query(boolQueryBuilder);request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//范围查询@Testpublic void doc_Search_Range_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");rangeQueryBuilder.gte(30);  //大于等于
//        rangeQueryBuilder.gt(30);  //大于
//        rangeQueryBuilder.lte(40);  //小于等于rangeQueryBuilder.lt(40);   //小于builder.query(rangeQueryBuilder);request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//模糊查询@Testpublic void doc_Search_Fuzzy_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "li1si").fuzziness(Fuzziness.ONE); //Fuzziness.ONE偏差的个数builder.query(fuzziness);request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//高亮显示@Testpublic void doc_Search_Highlight_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "li1si").fuzziness(Fuzziness.ONE); //Fuzziness.ONE偏差的个数HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.preTags("<font color='red'>");highlightBuilder.postTags("</font>");highlightBuilder.field("name");builder.highlighter(highlightBuilder);builder.query(fuzziness);request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}//分组查询@Testpublic void doc_Search_Group_Batch() throws IOException {SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");builder.aggregation(aggregationBuilder);request.source(builder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for (SearchHit hit : hits) {System.out.println(hit.getSourceAsString());}}@Afterpublic void After() throws IOException {esClient.close();}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/437373.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

matplotlib的优点_超详细matplotlib基础介绍!!!

(给Python开发者加星标&#xff0c;提升Python技能)来源&#xff1a;逐梦erhttps://zhumenger.blog.csdn.net/article/details/106530281【导语】&#xff1a;出色的数据可视化&#xff0c;会让你的数据分析等工作锦上添花&#xff0c;让人印(升)象(职)深(加)刻(薪)。matplotli…

【转】WPF PRISM开发入门一( 初始化PRISM WPF程序)

这篇博客将介绍在WPF项目中引入PRISM框架进行开发的一些基础知识。目前最新的PRISM的版本是Prism 6.1.0&#xff0c;可以在Github上获取PRISM的源码。这个系列的博客将选择PRISM 4.1版本来讲解。可以从微软官网上下载到PRISM 4.1相关内容。将下载下来的文件解压开&#xff1a; …

JS 给某个对象添加专属方法

function A() {this.name "A"; } a new A(); a.func function() {alert(this.name); } a.func(); // func只有a对象能够使用。 function A() {this.name "A";this.func function(){ale…

截屏悬浮软件_功能强大,却小巧的录屏软件,不在错过你的王者时刻

看看录屏是一款操作简单。功能强大的录屏软件。他可以设置你录制视频的一个分辨率&#xff0c;帧率以及录制屏幕方向&#xff0c;非常方便&#xff0c;用户将手机摇一摇就可以控制开启和停止录屏&#xff0c;高效录制精彩瞬间&#xff0c;在录制游戏视频的时候也可以做到不掉帧…

【转】对ASP.NET程序员非常有用的85个工具

介绍 这篇文章列出了针对 ASP.NET 开发人员的有用工具。 工具 1. Visual Studio Visual Studio Productivity Power tool&#xff1a;Visual Studio 专业版&#xff08;及以上&#xff09;的扩展&#xff0c;具有丰富的功能&#xff0c;如快速查找&#xff0c;导航解决方案&am…

JAVA 捕获异常所遇BUG

排查程序BUG时发现&#xff0c;在线程池的定时器内并未进行异常捕获&#xff0c;而是在定时器外try catch的&#xff0c;内部实现中从HashMap中Get数据&#xff0c;没有此KEY所有返回了NULL&#xff0c;后面没对NULL做处理&#xff0c;导致程序出现异常&#xff0c;然后上层又没…

公司用的非标普通自动化用单片机还是plc_PLC的介绍

PLC又叫可编程控制器&#xff0c;一开始是替代传统接触器的一个东西。随着人工价格不断的上涨&#xff0c;自动化的设备会越来越普及。自动化不再是大企业才用的起的东西 &#xff0c;各种多元化小型自动化设备进入了普通小企业甚至家庭作坊。PLC其实是单片机开发出来的一种工业…

【转】!C# 基础至集合-数组、List<T>、ArrayList、LinkedList、HashMap的一些区别

在C#中&#xff0c;数组由于是固定长度的&#xff0c;所以常常不能满足我们开发的需求。 由于这种限制不方便&#xff0c;所以出现了ArrayList。 ArrayList、List<T> ArrayList是可变长数组&#xff0c;你可以将任意多的数据Add到ArrayList里面。其内部维护的数组&…

JS字面量对象

<pre name"code" class"javascript">var A {name : "A",age : 18,sayNameAge : function(){ alert(A.name);alert(A.age);} }A.sayNameAge我搜到的资料上面都叫这种产生对象的方式叫 “对象字面量”&#xff0c;而我认为叫做 "字面量…

Hbase2修复 - HBCK2

第一次尝试修复HBase&#xff1a;https://blog.csdn.net/weixin_43736084/article/details/121336868 第二次尝试修复HBasehttps://blog.csdn.net/weixin_43736084/article/details/121336326

比较文本差异的工具_Linux 开发的五大必备工具 | Linux 中国

Linux 已经成为工作、娱乐和个人生活等多个领域的支柱&#xff0c;人们已经越来越离不开它。在 Linux 的帮助下&#xff0c;技术的变革速度超出了人们的想象&#xff0c;Linux 开发的速度也以指数规模增长。因此&#xff0c;越来越多的开发者也不断地加入开源和学习 Linux 开发…

【转】C# 动态对象(dynamic)的用法

说到正确用法&#xff0c;那么首先应该指出一个错误用法&#xff1a; 常有人会拿var这个关键字来和dynamic做比较。实际上&#xff0c;var和dynamic完全是两个概念&#xff0c;根本不应该放在一起做比较。var实际上是编译期抛给我们的“语法糖”&#xff0c;一旦被编译&#x…

关于prototype使用位置问题的讨论

问题贴&#xff1a;http://bbs.csdn.net/topics/390446362 new四部曲&#xff1a; &#xff08;1&#xff09;创建一个新的对象&#xff0c;并让函数的 this 指针指向它&#xff1b; &#xff08;2&#xff09;将函数的 prototype 对象的所有成员都赋给这个新对象&#xff0c…

第二次尝试修复Hbase2出现Region不一致,使用 HBCK2 - 2021.11.15

spark任务中入hbase任务全部失败了&#xff0c;查看日志发现hbase出现问题 报错日志&#xff1a; 在hbase的log中看到报错 Call queue is full on xxxx,16000,1611197476326, too many items queued 修改了配置文件&#xff0c;增加了队列数量 <property><name>h…

@query传参_vue-router中params传参和query传参的区别及处理方法

在 Vue 实例内部&#xff0c;你可以通过 $router 访问路由实例。因此你可以调用 this.$router.push想要导航到不同的 URL&#xff0c;则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录&#xff0c;所以&#xff0c;当用户点击浏览器后退按钮时&#xff0c;则…

JS成员函数声明位置优化

上代码 function A() {this.a function(){}; } a1 new A(); a2 new A(); alert( a1.aa2.a);输出 说明了a1.a&#xff0c;a2.a指向的内存不是同一个&#xff0c;也就是每个对象都有一份自己的函数&#xff0c;只不过一个类的所有实例之间的函数长得是一样的&#xff01; 所以…

【转】什么是用例

用例是什么? 其原始英文是usecase&#xff0c;直译过来就成了用例。这也是一个比较贴切的叫法了。 从字面的直接理解就是使用的例子。 另一种比较流行的定义是用例就是与使用者(actor)交互的&#xff0c;并且给使用者提供可观测的有意义的结果的一系列活动的集合。 这个定义…

第一次尝试修复Hbase2出现Region不一致,HBCK2

出现问题的原因 Hadoop中报错&#xff0c;集群中某节点的一块磁盘损坏了&#xff0c;运维修复后&#xff0c;hbase出现了region不一致的情况。 修复HBCK2 首先查看web ui中被lock的producer&#xff0c;先释放父region使用bypass -or pid&#xff0c;再释放子producer使用byp…

mybatis plus 事务管理器_SpringBoot第七篇:springboot开启声明式事务

springboot开启事务很简单&#xff0c;只需要一个注解Transactional 就可以了。因为在springboot中已经默认对jpa、jdbc、mybatis开启了事事务&#xff0c;引入它们依赖的时候&#xff0c;事物就默认开启。当然&#xff0c;如果你需要用其他的orm&#xff0c;比如beatlsql&…