page contents

Java教程——ElasticSearch Java API操作

本文讲述了Java教程——JElasticSearch Java API操作!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2023-06-PhYqQkaM64979addb8d83.png本文讲述了Java教程——JElasticSearch Java API操作!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。

一、索引操作

pom.xml

 <dependencies>

         <dependency>

             <groupId>org.elasticsearch</groupId>

             <artifactId>elasticsearch</artifactId>

             <version>7.8.0</version>

         </dependency>

         <!-- elasticsearch 的客户端 -->

         <dependency>

             <groupId>org.elasticsearch.client</groupId>

             <artifactId>elasticsearch-rest-high-level-client</artifactId>

             <version>7.8.0</version>

         </dependency>

         <!-- elasticsearch 依赖 2.x 的 log4j -->

         <dependency>

             <groupId>org.apache.logging.log4j</groupId>

             <artifactId>log4j-api</artifactId>

             <version>2.8.2</version>

         </dependency>

         <dependency>

             <groupId>org.apache.logging.log4j</groupId>

             <artifactId>log4j-core</artifactId>

             <version>2.8.2</version>

         </dependency>

         <dependency>

             <groupId>com.fasterxml.jackson.core</groupId>

             <artifactId>jackson-databind</artifactId>

             <version>2.9.9</version>

         </dependency>

         <!-- junit 单元测试 -->

         <dependency>

             <groupId>junit</groupId>

             <artifactId>junit</artifactId>

             <version>4.12</version>

         </dependency>

     </dependencies>

1、创建索引

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

 

         CreateIndexResponse resp = esClient.indices().create(new CreateIndexRequest("user"), RequestOptions.DEFAULT);

         boolean acknowledged = resp.isAcknowledged();

         System.out.println(acknowledged);

         esClient.close();

2、查看索引

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

 

         GetIndexResponse resp = esClient.indices().get(new GetIndexRequest("user"), RequestOptions.DEFAULT);

         System.out.println(resp.getAliases());

         System.out.println(resp.getMappings());

         System.out.println(resp.getSettings());

         esClient.close();

3、删除索引

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

         AcknowledgedResponse resp = esClient.indices().delete(new DeleteIndexRequest("user"), RequestOptions.DEFAULT);

         System.out.println(resp.isAcknowledged());

         esClient.close();

二、文档操作

User.java

 public class User {

 

     private String name;

     private String sex;

     private Integer age;

 

     public String getName() {

         return name;

    }

 

     public void setName(String name) {

         this.name = name;

    }

 

     public String getSex() {

         return sex;

    }

 

     public void setSex(String sex) {

         this.sex = sex;

    }

 

     public Integer getAge() {

         return age;

    }

 

     public void setAge(Integer age) {

         this.age = age;

    }

 }

1、插入文档

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

 

         IndexRequest request=new IndexRequest();

         request.index("user").id("1001");

 

         User user=new User();

         user.setName("张三");

         user.setSex("男");

         user.setAge(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()); 

         esClient.close();

2、批量插入文档

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

         BulkRequest request = new BulkRequest("user");

        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","张三","age",36,"sex","男"));

         request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","李四","age",35,"sex","女"));

         request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","王五1","age",34,"sex","男"));

         request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","王五2","age",33,"sex","女"));

         request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","王五3","age",32,"sex","男"));

         request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON,"name","王五4","age",31,"sex","女"));

 

         esClient.bulk(request,RequestOptions.DEFAULT);

 

         esClient.close();

3、修改文档

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

 

         UpdateRequest request=new UpdateRequest();

         request.index("user").id("1001");

         request.doc(XContentType.JSON,"sex","女");

 

         UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);

 

         System.out.println(response.getResult());

         esClient.close();

4、删除文档

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http"))); 

         DeleteRequest request=new DeleteRequest();

         request.index("user").id("1001");

 

         DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);

 

         System.out.println(response.toString());

         esClient.close();

5、批量删除文档

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

         BulkRequest request = new BulkRequest("user");

 

         request.add(new DeleteRequest().index("user").id("1001"));

         request.add(new DeleteRequest().index("user").id("1002"));

         request.add(new DeleteRequest().index("user").id("1003"));

 

         esClient.bulk(request,RequestOptions.DEFAULT);

 

         esClient.close();



6、查询指定文档

 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("master", 9200, "http")));

 

 

         GetRequest request=new GetRequest();

         request.index("user").id("1001");

         GetResponse response = esClient.get(request, RequestOptions.DEFAULT);

 

         System.out.println(response.getSourceAsString());

 

         esClient.close();

7、查询

全量查询

 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());

        }

条件查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("sex","男")));

 

         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());

        }

分页查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

         builder.from(0);

         builder.size(3);

 

         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());

        }

查询排序

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

         builder.sort("age", SortOrder.DESC);

 

         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());

        }

过滤字段

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

         String[] excludes={};

         String[] includes={"name","age"};

         builder.fetchSource(includes,excludes);

 

         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());

        }

组合查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder();

         BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

 

 //       boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));

 //       boolQueryBuilder.mustNot(QueryBuilders.matchQuery("age",31));

         boolQueryBuilder.should(QueryBuilders.matchQuery("age",32));

         boolQueryBuilder.should(QueryBuilders.matchQuery("age",31));

 

 

         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());

        }

范围查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder();

         RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");

 

         //大于等于

         rangeQuery.gte(33);

         //小于等于

         rangeQuery.lte(36);

 

         builder.query(rangeQuery);

 

         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());

        }

模糊查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder();

         FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("name", "王五").fuzziness(Fuzziness.ONE);

 

 

         builder.query(fuzzyQuery);

 

         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());

        }

高亮查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder();

         TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("sex", "男");

         builder.query(termQueryBuilder);

 

         HighlightBuilder highlightBuilder=new HighlightBuilder();

         highlightBuilder.preTags("<font color='red'>");

         highlightBuilder.postTags("</font>");

         highlightBuilder.field("sex");

 

         builder.highlighter(highlightBuilder);

 

         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());

        }

聚合查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder();

 

         AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").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());

        }

分组查询

 SearchRequest request=new SearchRequest();

         request.indices("user");

 

         SearchSourceBuilder builder = new SearchSourceBuilder();

 

         AggregationBuilder 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());

        }

 

 

         esClient.close();

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Java编程语言,推荐大家关注一个微信公众号:Java圈子。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Java入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2023-03-2AoKIjPQ64014b4ad30a3.jpg

  • 发表于 2023-06-25 09:40
  • 阅读 ( 168 )
  • 分类:Java开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1470 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章