elasticSearch 基本操作类

这篇博客介绍了如何使用RestHighLevelClient,一个ElasticSearch的Java高阶REST客户端,进行基本操作。由于官方不再支持TransportClient,作者选择了6.4.2版本的RestHighLevelClient,该客户端支持RESTful风格。博客中提供了官方文档链接及示例代码的GitHub仓库地址。
摘要由CSDN通过智能技术生成

elasticSearch官方文档地址   https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

因为官方后续不会支持transportClient,所以,本篇博客采用的是RestHighLevelClient客户端。版本是6.4.2。

RestHighLevelClient支持Restful风格。

代码地址:https://github.com/yywa/practice.git

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sun.glass.ui.Size;
import com.sun.org.apache.bcel.internal.generic.GETFIELD;
import org.apache.http.HttpHost;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.reflection.MetaObject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
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.ClearScrollRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import sun.rmi.runtime.Log;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author yyw
 */
public class ElasticSearchOperation {
    private static String host = "127.0.0.1";
    private static Integer port = 9200;
    private static final Integer MAX = 10000;

    private static ThreadLocal<Map<String, String>> scrollThreadLocal = new ThreadLocal<Map<String, String>>() {
        @Override
        protected Map<String, String> initialValue() {
            return new HashMap();
        }
    };

    public static RestHighLevelClient client;

    /**
     * 建立客户端链接
     */
    public static void createConnection() {
        try {
            HttpHost[] httpHosts = new HttpHost[1];
            httpHosts[0] = new HttpHost(host, port);
            client = new RestHighLevelClient(RestClient.builder(httpHosts));
            System.out.println("成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败");
        }
    }

    /**
     * 验证是否有客户端链接
     */
    public static void validateClient() {
        if (client == null) {
            createConnection();
        }
    }

    /**
     * 根据条件获取模糊匹配的索引
     *
     * @param condition 条件
     * @return 数组
     */
    public String[] getIndices(String condition) {
        validateClient();
        try {
            GetIndexRequest getIndexRequest = new GetIndexRequest();
            getIndexRequest.indices(condition + "*");
            GetIndexResponse response = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
            return response.getIndices();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建索引
     *
     * @param indexName index
     * @return 是否成功
     */
    public static boolean createIndex(String indexName) {
        validateClient();
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 验证索引是否存在。
     *
     * @param indexName index
     * @return 是否成功
     */
    public static boolean indexExist(String indexName) {
        boolean flag = false;
        validateClient();
        try {
            GetIndexRequest getIndexRequest = new GetIndexRequest();
            getIndexRequest.indices(indexName);
            flag = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除index
     *
     * @param indexName index
     * @return 是否成功
     */
    public static boolean deleteIndex(String indexName) {
        boolean flag = false;
        validat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值