ElasticSearch系列四:JAVA-API调用ES-API

1.Elasticsearch6中RestHighLevelClient和RestLowLevelClient

ElasticSearch系列四:JAVA-API调用ES-API_第1张图片
图片.png

1.1pom.xml



    4.0.0

    com.zy
    spring-elasticsearch-demo
    1.0-SNAPSHOT
    war

    
        5.1.3.RELEASE
        2.9.8
        6.5.4
        1.2.54
        1.16.20
    

    
        
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-expression
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
        
        
            org.elasticsearch
            elasticsearch
            ${elasticsearch.version}
        
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            ${elasticsearch.version}
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.core.version}
        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.core.version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.core.version}
        
        
        
            com.alibaba
            fastjson
            ${fastjson.version}
        
        
            org.projectlombok
            lombok
            ${lombok.version}
        

        
        
            org.slf4j
            slf4j-api
            1.7.25
        
        
        
            org.apache.logging.log4j
            log4j-slf4j-impl
            2.11.1
        
        
        
            org.apache.logging.log4j
            log4j-core
            2.11.1
        
        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
        
        
            org.apache.commons
            commons-lang3
            3.8.1
        

    

    
        spring-springmvc
        
            
                
                    maven-clean-plugin
                    3.0.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.7.0
                
                
                    maven-surefire-plugin
                    2.20.1
                
                
                    maven-war-plugin
                    3.2.0
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    

1.2web.xml




  spring-elasticsearch-demo
  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:springmvc.xml
    
    
    1
  
  
    DispatcherServlet
    /
  

1.3spring.xml




    
    

    
    


1.4applicationContext.xml




    


1.5配置类

package com.zy.config;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class ESConfigBean {
    private String host = "127.0.0.1";
    private int port = 9200;
    private String schema = "http";
    private int connectTimeOut = 1000;
    private int socketTimeOut = 30000;
    private int connectionRequestTimeOut = 500;
    private int maxConnectNum = 100;
    private int maxConnectPerRoute = 100;
    private HttpHost httpHost;
    private boolean uniqueConnectTimeConfig = true;
    private boolean uniqueConnectNumConfig = true;
    private RestClientBuilder builder;
    private RestHighLevelClient client;

    /**
     * Bean name default  函数名字
     *
     * @return
     */
    @Bean(name = "restHighLevelClient")
    public RestHighLevelClient restHighLevelClient() {
        httpHost= new HttpHost(host, port, schema);
        builder = RestClient.builder(httpHost);
        if (uniqueConnectTimeConfig) {
            setConnectTimeOutConfig();
        }
        if (uniqueConnectNumConfig) {
            setMutiConnectConfig();
        }
        client = new RestHighLevelClient(builder);
        return client;
    }

    /**
     * 异步httpclient的连接延时配置
     */
    public void setConnectTimeOutConfig() {
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeOut);
                requestConfigBuilder.setSocketTimeout(socketTimeOut);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
                return requestConfigBuilder;
            }
        });
    }

    /**
     * 异步httpclient的连接数配置
     */
    public void setMutiConnectConfig() {
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });
    }

    /**
     * 关闭连接
     */
    public void close() {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

1.6工具类

package com.zy.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.common.xcontent.XContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
@Component
public class ElasticSearchUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchUtil.class);

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    private static RestHighLevelClient client;

    @PostConstruct
    public void init() {
        client = this.restHighLevelClient;
    }

    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * 创建索引
     * @param index
     * @param requestOptions
     * @return
     */
    public static boolean createIndex(String index, RequestOptions requestOptions) {
        CreateIndexRequest request = new CreateIndexRequest(index);
        boolean flag;
        try {
            CreateIndexResponse indexResponse = client.indices().create(request, requestOptions);
            flag = indexResponse.isAcknowledged();
            if (flag) {
                LOGGER.info("创建索引成功!");
            }
            else {
                LOGGER.info("创建索引失败!");
            }
        } catch (IOException e) {
            LOGGER.error("failed to create index: {}.", index, e);
            flag = false;
        }
        return flag;
    }

    /**
     * 新增索引
     * @param index
     * @param document
     * @param id
     * @param object
     * @param requestOptions
     * @return
     */
    public static String add(String index, String document, String id, JSONObject object, RequestOptions requestOptions) {
        IndexRequest indexRequest = new IndexRequest(index, document, id);
        try {
            indexRequest.source(mapper.writeValueAsString(object), XContentType.JSON);
            IndexResponse indexResponse = client.index(indexRequest, requestOptions);
            return indexResponse.getId();
        } catch (IOException e) {
            LOGGER.error("failed to add index: {}, document: {}, id: {}.", index, document, id, e);
        }
        return null;
    }

    /**
     * 检查某索引是否存在:同步方法
     * @param request
     * @return
     */
    public static boolean checkIndexExist(Request request) {
        try {
            Response response = client.getLowLevelClient().performRequest(request);
            return response.getStatusLine().getReasonPhrase().equals("OK");
        } catch (IOException e) {
            LOGGER.error("index is not exist, request is {}.", request, e);
        }
        return false;
    }

    /**
     * 获取低水平客户端
     * @return
     */
    public static RestClient getLowLevelClient() {
        return client.getLowLevelClient();
    }

}

1.7controller层

package com.zy.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.utils.ElasticSearchUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/es/")
public class ESController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ESController.class);

    /**
     * 参考链接
     * https://blog.csdn.net/chy2z/article/details/81703012
     */

    /**
     *
     * @return
     */
    @GetMapping("index")
    @ResponseBody
    public Object indexPage() {
        return "hello world";
    }

    /**
     * http://localhost:8080/es/createIndex
     * 创建索引
     *
     * @param request
     * @param response
     * @param index
     * @return
     */
    @RequestMapping("/createIndex")
    @ResponseBody
    public String createIndex(HttpServletRequest request, HttpServletResponse response, String index) {
        Request request1 = new Request("HEAD", index);
        if (!ElasticSearchUtil.checkIndexExist(request1)) {
            if (ElasticSearchUtil.createIndex(request1.getEndpoint(), RequestOptions.DEFAULT)) {
                return "索引创建成功";
            } else {
                return "索引已经失败";
            }
        } else {
            return "索引已经存在";
        }
    }

    /**
     * 插入记录
     * http://localhost:8080/es/add
     *
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String addData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDateTime.now()));
        jsonObject.put("age", 30);
        jsonObject.put("name", "tom");
        jsonObject.put("date", LocalDateTime.now());
        String id = ElasticSearchUtil.add("hello",
                "hello_type",
                jsonObject.getString("id"),
                jsonObject,
                RequestOptions.DEFAULT);
        if (StringUtils.isNotBlank(id)) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("queryAll")
    @ResponseBody
    public String queryAll() {
        HttpEntity entity = new NStringEntity(
                "{ \"query\": { \"match_all\": {}}}",
                ContentType.APPLICATION_JSON);
        String endPoint = "/hello/hello_type/_search";
        try {
            Request request = new Request("POST", endPoint);
            request.setEntity(entity);
            Response response = ElasticSearchUtil.getLowLevelClient().performRequest(request);
            return EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            LOGGER.error("failed to query", e);
        }
        return "failed to queryAll";
    }

    /**
     * 根据条件查询
     *
     * @return
     */
    @GetMapping("/queryByMatch")
    @ResponseBody
    public String queryByMatch() {
        try {
            String endPoint = "/hello/hello_type/_search";
            IndexRequest indexRequest = new IndexRequest();
            XContentBuilder builder;
            try {
                builder = JsonXContent.contentBuilder().
                        startObject().
                        startObject("query").
                        startObject("match").
                        field("name.keyword", "jerry").
                        // field("name", "jerry").
                                endObject().
                                endObject().
                                endObject();
                indexRequest.source(builder);
            } catch (IOException e) {
                LOGGER.error("failed to query,", e);
            }
            String source = indexRequest.source().utf8ToString();
            LOGGER.info("source---->" + source);
            HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
            Request request = new Request("post", endPoint);
            request.setEntity(entity);
            Response response = ElasticSearchUtil.getLowLevelClient().performRequest(request);
            return EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            LOGGER.error("failed to query, name: {}.", "jerry", e);
        }
        return "failed to query";
    }

    /**
     * 复合查询
     *
     * @return
     */
    @RequestMapping("/queryByCompound")
    @ResponseBody
    public String queryByCompound() {
        try {
            String endPoint = "/hello/hello_type/_search";
            IndexRequest indexRequest = new IndexRequest();
            XContentBuilder builder;
            try {
                builder = JsonXContent.contentBuilder().
                        startObject().
                            startObject("query").
                                startObject("bool").
                                    startObject("must").
                                        startObject("match").
                                            field("name.keyword", "tom").
                                        endObject().
                                    endObject().
                                    startObject("filter").
                                        startObject("range").
                                            startObject("age").
                                                field("gte", "10").
                                                field("lte", "50").
                                            endObject().
                                        endObject().
                                    endObject().
                                endObject().
                            endObject().
                        endObject();
                indexRequest.source(builder);
            } catch (IOException e) {
                LOGGER.error("failed to query", e);
            }
            String source = indexRequest.source().utf8ToString();
            LOGGER.info("source---->" + source);
            HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
            Request request = new Request("post", endPoint);
            request.setEntity(entity);
            Response response = ElasticSearchUtil.getLowLevelClient().performRequest(request);
            return EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            LOGGER.error("failed to query", e);
        }
        return "failed to query";
    }
}

你可能感兴趣的:(ElasticSearch系列四:JAVA-API调用ES-API)