IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> java操作elasticsearch api实现索引及文档增删改查(非常全面) -> 正文阅读

[大数据]java操作elasticsearch api实现索引及文档增删改查(非常全面)

?

目录

前言

一、创建User实体类

二、索引(index)的增删改查

三、文档(doc)的增删改查

四、文档(doc)的批量新增和删除

五、文档(doc)的高级查询


前言

在实际的项目开发中,我们经常要对大批量的业务数据或者日志数据进行全文检索查询,这个时候我们就可以用elasticsearch作为我们的全文检索引擎,下面我们将使用java elasticsearch 的 api 实现 索引以及文档(doc)的增删改查,包括批量新增和删除,以及组合条件,聚合,分组,高亮等高级查询都会一一以代码示例展示。

一、创建User实体类

用户具有姓名(name),年龄(age),性别(sex)三个字段

package com.example.dzx.elasticsearch;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author 500007
 * @ClassName:
 * @Description:
 * @date 2022年04月29日 09:06:04
 */
@Data
@Accessors(chain = true)
public class User {
    private String name;
    private Integer age;
    private String sex;
}

二、索引(index)的增删改查

package com.example.dzx.elasticsearch;

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.*;
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.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author 500007
 * @ClassName:
 * @Description: index索引的增删改查
 * @date 2022年04月28日 20:03:49
 */
public class ESIndexOperate {
    public static void main(String[] args) throws IOException {
        //创建客户端
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
        //索引客户端
        IndicesClient indices = restHighLevelClient.indices();
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
        //创建索引
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        //响应状态
        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println("创建索引响应:" + acknowledged);

        //获取索引信息
        GetIndexRequest getIndexRequest = new GetIndexRequest("user");
        GetIndexResponse getIndexResponse = indices.get(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(getIndexResponse.getAliases());
        System.out.println(getIndexResponse.getMappings());
        System.out.println(getIndexResponse.getSettings());

        //删除索引信息
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user");
        AcknowledgedResponse acknowledgedResponse = indices.delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(acknowledgedResponse.isAcknowledged());

        //关闭客户端
        restHighLevelClient.close();

    }
}

三、文档(doc)的增删改查

package com.example.dzx.elasticsearch;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
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.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.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author 500007
 * @ClassName:
 * @Description: 索引下doc的增删改查
 * @date 2022年04月29日 09:29:11
 */
public class ESDocOperate {
    public static void main(String[] args) throws Exception {

        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
        //插入数据
        IndexRequest request = new IndexRequest("user").id("1001");
        User user = new User();
        user.setName("zhangsan");
        user.setAge(30);
        user.setSex("男");
        ObjectMapper objectMapper = new ObjectMapper();
        String user_json = objectMapper.writeValueAsString(user);
        request.source(user_json, XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response.status().getStatus());
        System.out.println(response.getResult());

        //修改数据
        UpdateRequest updateRequest = new UpdateRequest().index("user").id("1001");
        updateRequest.doc(XContentType.JSON, "sex", "女");
        UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(update.status());
        System.out.println(update.getResult());

        //查询数据
        GetRequest getRequest = new GetRequest().index("user").id("1001");
        GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(documentFields.getSourceAsString());

        //删除数据
        DeleteRequest deleteRequest = new DeleteRequest().index("user").id("1001");
        DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.status());
        System.out.println(delete.getResult());

        //关闭客户端
        restHighLevelClient.close();
    }
}

四、文档(doc)的批量新增和删除

package com.example.dzx.elasticsearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;

/**
 * @author 500007
 * @ClassName:
 * @Description: doc的批量新增和删除
 * @date 2022年04月29日 09:33:14
 */
public class ESBulkOperate {


    public static void main(String[] args) throws Exception {
        //创建客户端
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
        //批量操作请求体
        BulkRequest bulkRequest = new BulkRequest();

        ObjectMapper objectMapper = new ObjectMapper();
        User user = new User().setName("zhangsan").setAge(30).setSex("男");
        String user_json = objectMapper.writeValueAsString(user);
        //插入数据请求体
        IndexRequest indexRequest = new IndexRequest("user").id("1001").source(user_json, XContentType.JSON);

        User user1 = new User().setName("lisi").setAge(31).setSex("女");
        String user_json1 = objectMapper.writeValueAsString(user);
        //插入数据请求体
        IndexRequest indexRequest1 = new IndexRequest("user").id("1002").source(user_json1, XContentType.JSON);

        //将两个插入数据请求体加入到批量操作请求体中
        bulkRequest.add(indexRequest).add(indexRequest1);
        //批量插入数据
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        //耗时
        System.out.println(bulk.getTook().seconds());
        //打印数据
        System.out.println(bulk.getItems());

        //批量删除操作
        BulkRequest deleteBulkRequest = new BulkRequest();
        deleteBulkRequest.add(new DeleteRequest().index("user").id("1001"));
        deleteBulkRequest.add(new DeleteRequest().index("user").id("1002"));
        BulkResponse bulkDeleteResponse = restHighLevelClient.bulk(deleteBulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkDeleteResponse.getItems());
        System.out.println(bulkDeleteResponse.getTook());
    }
}

五、文档(doc)的高级查询

package com.example.dzx.elasticsearch;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

/**
 * @author 500007
 * @ClassName:
 * @Description: es的高级查询操作
 * @date 2022年04月29日 09:53:31
 */
public class ESSearchOperate {

    public static void main(String[] args) throws Exception {
        //创建客户端
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

        SearchRequest request = new SearchRequest();
        request.indices("user");
        /**
         * ****************************全量查询******************************
         */
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .query(QueryBuilders.matchAllQuery());
        request.source(searchSourceBuilder);

        SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        //命中数据
        SearchHits hits = search.getHits();
        SearchHit[] hits1 = hits.getHits();
        for (SearchHit searchHit : hits1) {
            System.out.println(searchHit.getSourceAsString());
        }
        //命中条数
        System.out.println(hits.getTotalHits());
        //耗时
        System.out.println(search.getTook().seconds());

        /**
         * *****************************条件查询******************************
         */
        SearchRequest termSearchRequest = new SearchRequest("user")
                .source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30)));
        SearchResponse termSearchResponse = restHighLevelClient.search(termSearchRequest, RequestOptions.DEFAULT);
        System.out.println(termSearchResponse.getHits());
        System.out.println(termSearchResponse.getTook());


        /**
         * *****************************分页并按照年龄排序查询******************************
         */
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30));
        //(当前页码-1)*页面容量
        builder.from(0)
                .size(2)
                .sort("age", SortOrder.DESC);
        SearchRequest termPageSearchRequest = new SearchRequest("user")
                .source(builder);
        SearchResponse termPageSearchResponse = restHighLevelClient.search(termPageSearchRequest, RequestOptions.DEFAULT);
        System.out.println(termSearchResponse.getHits());
        System.out.println(termSearchResponse.getTook());


        /**
         * *****************************排除字段和包含字段查询******************************
         */
        SearchSourceBuilder cludesBuilder = new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30));
        //查询返回的结果中,包含user的age字段,排除name字段
        String[] includes = {"age"};
        String[] excludes = {"name"};
        cludesBuilder.fetchSource(includes, excludes);
        SearchRequest cludesSearchRequest = new SearchRequest("user")
                .source(builder);
        SearchResponse cludesSearchResponse = restHighLevelClient.search(cludesSearchRequest, RequestOptions.DEFAULT);
        System.out.println(cludesSearchResponse.getHits());
        System.out.println(cludesSearchResponse.getTook());


        /**
         * *****************************组合查询******************************
         */
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //姓名必须等于30,
        boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
        //性别必须是男
        boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "男"));
        //性别必须不是男
//        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "男"));

        //年龄可以是30或者40
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 40));

        SearchRequest boolQuerySearchRequest = new SearchRequest("user")
                .source(new SearchSourceBuilder().query(boolQueryBuilder));
        restHighLevelClient.search(boolQuerySearchRequest, RequestOptions.DEFAULT);


        /**
         * *****************************范围查询*****************************
         */
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
        rangeQueryBuilder.gte(30).lte(50);
        SearchRequest rangeQuerySearchRequest = new SearchRequest("user")
                .source(new SearchSourceBuilder().query(rangeQueryBuilder));
        restHighLevelClient.search(rangeQuerySearchRequest, RequestOptions.DEFAULT);


        /**
         * *****************************模糊查询*****************************
         */
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "wangwu");
        //设置偏差字符长度为1,例如wangwu1,wangwu2,都可以查询出来
        fuzzyQueryBuilder.fuzziness(Fuzziness.ONE);
        //设置偏差字符长度为2,例如wangwu11,wangwu22,都可以查询出来
        fuzzyQueryBuilder.fuzziness(Fuzziness.TWO);

        SearchRequest fuzzyQuerySearchRequest = new SearchRequest("user")
                .source(new SearchSourceBuilder().query(fuzzyQueryBuilder));
        restHighLevelClient.search(fuzzyQuerySearchRequest, RequestOptions.DEFAULT);


        /**
         * *****************************高亮查询*****************************
         */
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "张三");
        SearchSourceBuilder highlightSourceBuilder = new SearchSourceBuilder();
        //设置高亮器,文本显示为红色
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.preTags("</font>");
        highlightBuilder.field("name");
        highlightSourceBuilder.highlighter(highlightBuilder);

        highlightSourceBuilder.query(termQueryBuilder);

        SearchRequest highlightQuerySearchRequest = new SearchRequest("user")
                .source(highlightSourceBuilder);
        restHighLevelClient.search(highlightQuerySearchRequest, RequestOptions.DEFAULT);


        /**
         * *****************************聚合查询*****************************
         */
        SearchSourceBuilder aggSourceBuilder = new SearchSourceBuilder();
        //查询
        AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        aggSourceBuilder.aggregation(aggregationBuilder);
        SearchRequest aggQuerySearchRequest = new SearchRequest("user")
                .source(aggSourceBuilder);
        restHighLevelClient.search(highlightQuerySearchRequest, RequestOptions.DEFAULT);


        /**
         * *****************************分组查询*****************************
         */
        SearchSourceBuilder groupSourceBuilder = new SearchSourceBuilder();
        AggregationBuilder groupAggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        groupSourceBuilder.aggregation(groupAggregationBuilder);
        SearchRequest groupQuerySearchRequest = new SearchRequest("user")
                .source(groupSourceBuilder);
        restHighLevelClient.search(groupQuerySearchRequest, RequestOptions.DEFAULT);
    }
}

至此代码演示结束,大家觉得赞的话,支持一下博主呢,记得一键三连哦!

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-05-01 15:49:32  更:2022-05-01 15:52:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 1:11:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码