elasticsearch学习3:elasticsearch7.1.6 java api

package com.thinkgem.jeesite.util.elasticsearch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.modules.lore.entity.Knowledge;
import com.thinkgem.jeesite.modules.sys.utils.HttpClient3;
import com.thinkgem.jeesite.modules.sys.utils.SysUtils;
import com.thinkgem.jeesite.util.elasticsearch.bean.GroupByListItem;
import com.thinkgem.jeesite.util.elasticsearch.jsonFormat.DefaultJsonFormat;
import com.thinkgem.jeesite.util.elasticsearch.jsonFormat.JsonFormatInterface;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
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.*;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilder;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

/**
 * ElasticSearch 操作
 * @author zfq
 *
 */
public class ElasticSearchUtil {

   private RestHighLevelClient restHighLevelClient;
   private RestClient restClient;

   private String hostname = "127.0.0.1";
   private int port = 9200;
   private String scheme = "http";
   private String username = "";  //elasticsearch链接的用户名,如果es本身没设置使用用户名密码的,那这里就不用设置
   private String password = "";  //elasticsearch链接的密码,如果es本身没设置使用用户名密码的,那这里就不用设置
   private JsonFormatInterface jsonFormatInterface; //JSON格式化接口。默认使用 DefaultJsonFormat();
   private HttpHost[] httpHosts;

   /**
    * 缓存。
    * key:  indexName
    * value: 要打包提交的 List
    */
   public Map<String, List<Map<String, Object>>> cacheMap;
   public int cacheMaxNumber = 100; //如果使用缓存,这里是缓存中的最大条数,超过这些条就会自动打包提交

   /**
    * 通过传入自定义 {@link HttpHost} 的方式,创建工具类
    * @param client 传入如:
    *     <pre>
    *   new HttpHost("127.0.0.1", "9200", "http"))
    *     </pre>
    */
   public ElasticSearchUtil(HttpHost... httpHosts) {
      this.httpHosts = httpHosts;
      cacheMap = new HashMap<String, List<Map<String,Object>>>();
      jsonFormatInterface = new DefaultJsonFormat();
   }

   /**
    * 初始化 ElasticSearchUtil
    * @param hostname elasticsearch所在的ip,传入如 127.0.0.1
    */
   public ElasticSearchUtil(String hostname) {
      this.hostname = hostname;
      cacheMap = new HashMap<String, List<Map<String,Object>>>();
      jsonFormatInterface = new DefaultJsonFormat();
   }

   /**
    * 初始化 ElasticSearchUtil
    * @param hostname elasticsearch所在的ip,传入如 127.0.0.1
    * @param port elasticsearch服务的端口号,传入如 9200
    * @param scheme 协议,传入如 http
    */
   public ElasticSearchUtil(String hostname, int port, String scheme) {
      this.hostname = hostname;
      this.port = port;
      this.scheme = scheme;
      cacheMap = new HashMap<String, List<Map<String,Object>>>();
      jsonFormatInterface = new DefaultJsonFormat();
   }

   /**
    * 初始化 ElasticSearchUtil
    * @param hostname elasticsearch所在的ip,传入如 127.0.0.1
    * @param port elasticsearch服务的端口号,传入如 9200
    * @param scheme 协议,传入如 http
    * @param username 如果ElasticSearch 有设置用户名密码登录的方式,这里要传入设置的用户名。如果没有密码,这里传入null或者空字符串都行
    * @param password 如果ElasticSearch 有设置用户名密码登录的方式,这里要传入设置的密码。如果没有密码,这里传入null或者空字符串都行
    */
   public ElasticSearchUtil(String hostname, int port, String scheme, String username, String password) {
      this.hostname = hostname;
      this.port = port;
      this.scheme = scheme;
      if(username != null && username.length() > 0) {
         this.username = username;
      }
      if(password != null && password.length() > 0) {
         this.password = password;
      }
      cacheMap = new HashMap<String, List<Map<String,Object>>>();
      jsonFormatInterface = new DefaultJsonFormat();
   }

   /**
    * 设置elasticsearch的username、password。此行一般是在
    * <pre>
    *     ElasticSearchUtil(String hostname, int port, String scheme)
    * </pre>
    * 这下面就使用,如果elasticsearch有用户名、密码的话。
    * @param username 如果ElasticSearch 有设置用户名密码登录的方式,这里要传入设置的用户名。如果没有密码,这里传入null或者空字符串都行
    * @param password 如果ElasticSearch 有设置用户名密码登录的方式,这里要传入设置的密码。如果没有密码,这里传入null或者空字符串都行
    */
   public void setUsernameAndPassword(String username, String password) {
      if(username != null && username.length() > 0) {
         this.username = username;
      }
      if(password != null && password.length() > 0) {
         this.password = password;
      }
   }

   /**
    * 设置缓存中最大缓存的条数。超过这些条就会自动打包提交。 这里自动提交的便是 {@link #cache(Map)} 所缓存的数据
    * @param cacheMaxNumber 缓存的条数。如果不设置,默认是100
    */
   public void setCacheMaxNumber(int cacheMaxNumber) {
      this.cacheMaxNumber = cacheMaxNumber;
   }

   /**
    * JSON格式化接口。如果不设置此处,默认使用 {@link DefaultJsonFormat}
    * @param jsonFormatInterface 设置自定义json序列化方法
    */
   public void setJsonFormatInterface(JsonFormatInterface jsonFormatInterface) {
      this.jsonFormatInterface = jsonFormatInterface;
   }

   /**
    * 获取操作的 {@link RestHighLevelClient} 对象
    * @return {@link RestHighLevelClient}
    */
   public RestHighLevelClient getRestHighLevelClient(){
      if(this.restHighLevelClient == null){
         if(this.httpHosts == null){
            //没有直接传入 httpshosts,那么就是使用单个的
            HttpHost httpHost = new HttpHost(this.hostname, this.port, this.scheme);
            this.httpHosts = new HttpHost[1];
            this.httpHosts[0] = httpHost;
         }
         if(this.username.length() > 0 && this.password.length() > 0) {
            //当前elasticsearch 设置了连接的用户名密码
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));  //es账号密码(默认用户名为elastic)
            this.restHighLevelClient =new RestHighLevelClient(
                  RestClient.builder(this.httpHosts).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                     public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        httpClientBuilder.disableAuthCaching();
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                     }
                  })
            );
         }else{
            this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(this.httpHosts));
         }
      }
      return this.restHighLevelClient;
   }

   /**低级客户端,并不好用,没有密码sql查询还行,加密码后sql查询报出权限错误,其实可以自己封装一个httpClient就能用
    * 获取操作的 {@link RestClient} 对象
    * @return {@link RestClient}
    */
   public RestClient getRestClient(){
      if(this.restClient == null){
         if(this.httpHosts == null){
            //没有直接传入 httpshosts,那么就是使用单个的
            HttpHost httpHost = new HttpHost(this.hostname, this.port, this.scheme);
            this.httpHosts = new HttpHost[1];
            this.httpHosts[0] = httpHost;
         }
         if(this.username.length() > 0 && this.password.length() > 0) {
            //当前elasticsearch 设置了连接的用户名密码
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));  //es账号密码(默认用户名为elastic)
            this.restClient = RestClient.builder(this.httpHosts).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
               public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                  httpClientBuilder.disableAuthCaching();
                  return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
               }
            }).build();

         }
         this.restClient = RestClient.builder(this.httpHosts).build();
      }
      return this.restClient;
   }

   /**
    * 获取操作的 {@link RestHighLevelClient} 对象
    * @return {@link RestHighLevelClient}
    */
   public static RestHighLevelClient getStaticRestHighLevelClient(){
      String hostName = SysUtils.getSysConfigValue("ElasticSearchIp","90");
      RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder("172.0.0.1"));
      return restHighLevelClient;
   }


   /**
    * 将之提交到缓存Cache中。这里不同意put,put是直接提交到ElasticSearch中,而这个只是提交到Java缓存中,等积累到一定条数之后,在一起将Java缓存中的打包一次性提交到 Elasticsearch中
    * <p>默认同一个indexName索引中,缓存最大条数是100条,达到100条会自动提交到 elasticsearch。 这个最大条数,可以通过  {@link #setCacheMaxNumber(int)} 进行设置。建议不要超过4000条 </p>
    * @param params 要增加的数据,key-value形式。 其中map.value 支持的类型有 String、int、long、float、double、boolean
    * @param indexName 索引名字,类似数据库的表,是将数据添加进哪个表
    */
   public synchronized void cache(Map<String, Object> params, String indexName){
      List<Map<String,Object>> list = cacheMap.get(indexName);
      if(list == null){
         list = new LinkedList<Map<String,Object>>();
      }
      list.add(params);

      if(list.size() >= this.cacheMaxNumber){
         //提交
         boolean submit = cacheSubmit(indexName);
         if(submit){
            //提交成功,那么清空indexName的list
            list.clear();
         }
      }

      //重新赋予cacheMap
      cacheMap.put(indexName, list);
   }

   /**
    * 将当前缓存中某个索引中的数据提交到elasticsearch中
    * @param indexName 索引名字,类似数据库的表,是将数据添加进哪个表
    * @return true:成功;  false:提交失败
    */
   public synchronized boolean cacheSubmit(String indexName){
      List<Map<String,Object>> list = cacheMap.get(indexName);
      if(list == null){
         return true;
      }

      BulkResponse res = puts(list, indexName);
      if(res == null || res.hasFailures()){
         //出现错误,那么不清空list
         return false;
      }else{
         //成功,那么清空缓存中这个索引的数据
         list.clear();
         cacheMap.put(indexName, list);
         return true;
      }
   }

   /**
    * 创建索引
    * @param indexName 要创建的索引的名字,传入如: testindex
    * @return 创建索引的响应对象。可以使用 {@link CreateIndexResponse#isAcknowledged()} 来判断是否创建成功。如果为true,则是创建成功
    */
   public CreateIndexResponse createIndex(String indexName)  {
      CreateIndexResponse response=null;
      if(existIndex(indexName)){
         response = new CreateIndexResponse(false, false, indexName);
         return response;
      }

      CreateIndexRequest request = new CreateIndexRequest(indexName);
      try {
         response = getRestHighLevelClient().indices().create(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
      return response;
   }

   public static void log(String text){
      System.out.println(text);
   }

   /**
    * 判断索引是否存在
    * @param indexName 要判断是否存在的索引的名字,传入如: testindex
    * @return 返回是否存在。
    *        <ul>
    *           <li>true:存在</li>
    *           <li>false:不存在</li>
    *        </ul>
    */
   public boolean existIndex(String index){
      GetIndexRequest request = new GetIndexRequest();
      request.indices(index);
      boolean exists;
      try {
         exists = getRestHighLevelClient().indices().exists(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
         return false;
      }
      return exists;
   }

   /**
    * 数据添加,网 elasticsearch 中添加一条数据
    * @param params 要增加的数据,key-value形式。 其中map.value 支持的类型有 String、int、long、float、double、boolean
    * @param indexName 索引名字,类似数据库的表,是添加进那个表
    * @param id 要添加的这条数据的id, 如果传入null,则由es系统自动生成一个唯一ID
    * @return 创建结果。如果 {@link IndexResponse#getId()} 不为null、且id长度大于0,那么就成功了
    */
   public IndexResponse put(String params, String indexName, String id){
      //创建请求
      IndexRequest request = new IndexRequest(indexName);
      if(id != null){
         request.id(id);
      }
      request.timeout(TimeValue.timeValueSeconds(5));

      IndexResponse response = null;
      try {
         response = getRestHighLevelClient().index(request.source(params, XContentType.JSON), RequestOptions.DEFAULT);

      } catch (IOException e) {
         e.printStackTrace();
      }
      return response;
   }

   /**
    * 数据更新
    * @param params 要更新 的数据,key-value形式。 其中map.value 支持的类型有 String、int、long、float、double、boolean
    * @param indexName 索引名字,类似数据库的表,是添加进那个表
    * @param id 要添加的这条数据的id, 如果传入null,则由es系统自动生成一个唯一ID
    * @return 创建结果。如果 {@link IndexResponse#getId()} 不为null、且id长度大于0,那么就成功了
    */
   public UpdateResponse update(String params,String indexName, String id){
      //创建请求
      UpdateRequest request = new UpdateRequest(indexName,id);
      request = request.doc(params, XContentType.JSON);
      request.timeout(TimeValue.timeValueSeconds(5));
      UpdateResponse response = null;
      try {
         response = getRestHighLevelClient().update(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
      return  response;
   }





   /**
    * 数据添加,网 elasticsearch 中添加一条数据
    * @param params 要增加的数据,key-value形式。 其中map.value 支持的类型有 String、int、long、float、double、boolean
    * @param indexName 索引名字,类似数据库的表,是添加进那个表
    * @param id 要添加的这条数据的id, 如果传入null,则由es系统自动生成一个唯一ID
    * @return 创建结果。如果 {@link IndexResponse#getId()} 不为null、且id长度大于0,那么就成功了
    */
   public IndexResponse put(String params, String indexName){
      return put(params, indexName, null);
   }

   /**
    * 批量添加数据
    * @param list 批量添加的数据的List
    * @param indexName 索引名字,类似数据库的表,是添加进那个表
    * @return {@link BulkResponse} ,如果没提交,或者提交的是空,或者出错,那么会返回null。判断其有没有提交成功可以使用  (res != null && !res.hasFailures())
    */
   public BulkResponse puts(List<Map<String, Object>> list, String indexName){
      if(list.size() < 1){
         return null;
      }

      //批量增加
      BulkRequest bulkAddRequest = new BulkRequest();
      IndexRequest indexRequest;
      for (int i = 0; i < list.size(); i++) {
         indexRequest = new IndexRequest(indexName);
         indexRequest.source(jsonFormatInterface.mapToJsonString(list.get(i)), XContentType.JSON);
         bulkAddRequest.add(indexRequest);
      }

      BulkResponse bulkAddResponse = null;
      try {
         bulkAddResponse = getRestHighLevelClient().bulk(bulkAddRequest, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
      return bulkAddResponse;
   }

   public BulkResponse putListObject(List<Knowledge> list, String indexName){
      if(list.size() < 1){
         return null;
      }
      //批量增加
      BulkRequest bulkAddRequest = new BulkRequest();
      IndexRequest indexRequest;
      for (int i = 0; i < list.size(); i++) {
         indexRequest = new IndexRequest(indexName);
         String esJson = JsonMapper.toJsonString(list.get(i));
         indexRequest.source(esJson, XContentType.JSON);
         bulkAddRequest.add(indexRequest);
      }

      BulkResponse bulkAddResponse = null;
      try {
         bulkAddResponse = getRestHighLevelClient().bulk(bulkAddRequest, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
      return bulkAddResponse;
   }


   /**
    * 查询并分页
    * @param indexName 索引名字
    * @param query 查询条件, {@link SearchSourceBuilder}
    * @param from 从第几条开始查询,相当于 limit a,b 中的a ,比如要从最开始第一条查,可传入: 0
    * @param size 本次查询最大查询出多少条数据 ,相当于 limit a,b 中的b
    * @return {@link SearchResponse} 结果,可以通过 response.status().getStatus() == 200 来判断是否执行成功
    *获取总条数的方法
    * TotalHits totalHits = searchResponse.getHits().getTotalHits();
    *
    */
   public SearchResponse search(String indexName, SearchSourceBuilder searchSourceBuilder, Integer from, Integer size){
      SearchRequest request = new SearchRequest(indexName);
      searchSourceBuilder.from(from);
      searchSourceBuilder.size(size);
      request.source(searchSourceBuilder);
      SearchResponse response = null;
      try {
         response = getRestHighLevelClient().search(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
      return response;
   }
   public SearchResponse searchCount(SearchRequest request ){
      SearchResponse response = null;
      try {
         response = getRestHighLevelClient().search(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
      return response;
   }

   /**
    * 查询数据
    * @param indexName 索引名字
    * @param queryString 查询条件,传入如: name:guanleiming AND age:123
    * @param from 从第几条开始查询,相当于 limit a,b 中的a ,比如要从最开始第一条查,可传入: 0
    * @param size 本次查询最大查询出多少条数据 ,相当于 limit a,b 中的b
    * @param sort 排序方式。如果不需要排序,传入null即可。 比如要根据加入时间time由大到小,传入的便是: SortBuilders.fieldSort("time").order(SortOrder.DESC)
    * @return 查询的结果,封装成list返回。list中的每条都是一条结果。如果链接es出错或者查询异常又或者什么都没查出,那么都是返回一个 new ArrayList<Map<String,Object>>(); ,任何情况返回值不会为null
    *        <p>返回的结果集中,每条会自动加入一项 esid ,这个是在es中本条记录的唯一id编号,es自动赋予的。</p>
    */
   public List<Map<String,Object>> search(String indexName, String queryString, Integer from, Integer size, SortBuilder sort){
      List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      if(queryString != null && queryString.length() > 0){
         //有查询条件,才会进行查询,否则会查出所有
         QueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString);
         searchSourceBuilder.query(queryBuilder);
      }
      //此处可封装获取高亮

      /*// 2.2、指定高亮
      HighlightBuilder highlightBuilder = new HighlightBuilder();
      highlightBuilder.field("username")
            .preTags("<font color='red'>")
            .postTags("</font>");

      searchSourceBuilder.highlighter(highlightBuilder);*/


      //判断是否使用排序
      if(sort != null){
         searchSourceBuilder.sort(sort);
      }
      SearchResponse response = search(indexName, searchSourceBuilder, from, size);
      if(response.status().getStatus() == 200){
         SearchHit shs[] = response.getHits().getHits();
         for (int i = 0; i < shs.length; i++) {
            Map<String, Object> map = shs[i].getSourceAsMap();
            map.put("esid", shs[i].getId());
            list.add(map);
         }
      }else{
         //异常
      }

      return list;
   }


   /**
    * 查询数据
    * <p>如果数据超过100条,那么只会返回前100条数据。<p>
    * @param indexName 索引名字
    * @param queryString 查询条件,传入如: name:guanleiming AND age:123
    * @return 查询的结果,封装成list返回。list中的每条都是一条结果。如果链接es出错或者查询异常又或者什么都没查出,那么都是返回一个 new ArrayList<Map<String,Object>>(); ,任何情况返回值不会为null
    *        <p>返回的结果集中,每条会自动加入一项 esid ,这个是在es中本条记录的唯一id编号,es自动赋予的。</p>
    */
   public List<Map<String,Object>> search(String indexName, String queryString){
      return search(indexName, queryString, 0, 20, null);
   }





   /**
    * 通过elasticsearch数据的id,获取这条数据
    * @param indexName 索引名字
    * @param id elasticsearch数据的id
    * @return 这条数据的内容。 如果返回null,则是没有找到这条数据,或者执行过程出错。
    */
   public  Map<String,Object> searchById (String indexName, String id){
      GetRequest request = new GetRequest(indexName, id);
      GetResponse response = null;
      try {
         response = getRestHighLevelClient().get(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
         return null;
      }
      if(response.isSourceEmpty()){
         //没有这条数据
         return null;
      }

      Map<String, Object> map = response.getSource();
      //为返回的数据添加id
      map.put("esid",response.getId());
      return map;
   }

   public  static Map<String,Object> getById(String indexName, String id){
      GetRequest request = new GetRequest(indexName, id);
      GetResponse response = null;
      try {
         response = getStaticRestHighLevelClient().get(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
         return null;
      }
      if(response.isSourceEmpty()){
         //没有这条数据
         return null;
      }

      Map<String, Object> map = response.getSource();
      //为返回的数据添加id
      map.put("esid",response.getId());
      return map;
   }

   /**
    * 删除索引
    * @param indexName
    * @throws IOException
    */

   public  AcknowledgedResponse deleteIndex(String indexName) throws IOException {
      DeleteIndexRequest request = new DeleteIndexRequest(indexName);
      AcknowledgedResponse response = getRestHighLevelClient().indices().delete(request, RequestOptions.DEFAULT);
      System.out.println(response.isAcknowledged());
      return response;
   }



   /**
    * 通过elasticsearch数据的id,来删除这条数据
    * @param indexName 索引名字
    * @param id 要删除的elasticsearch这行数据的id
    */
   public boolean deleteById(String indexName, String id) {
      DeleteRequest request = new DeleteRequest(indexName, id);
      DeleteResponse delete = null;
      try {
         delete = getRestHighLevelClient().delete(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
         //删除失败
         return false;
      }

      if(delete == null){
         //这种情况应该不存在
         return false;
      }
      if(delete.getResult().equals(Result.DELETED)){
         return true;
      }else{
         return false;
      }
   }

   /**
    * 以 sql查询语句的形式,搜索 elasticsearch
    *
    * @param sqlQuery sql查询语句,传入如: select * from user WHERE username = 'guanleiming'
    * @return List结果
    * 注意sql查询并不会分词查询,使用起来效果并不理想
    */
   public List<Map<String, Object>> searchBySqlQuery(String sqlQuery){
      List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();

      String method = "POST";
      //String endPoint = "/_sql";
      String endPoint = "/_xpack/sql";
      Request request = new Request(method, endPoint);
      request.addParameter("Content-Type","application/json");

      request.addParameter("format", "text");
      //request.addParameter("header", "{\"WWW-Authenticate\":\"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\"}");

      request.setJsonEntity("{\"query\":\""+sqlQuery+"\"}");
      try {
         Response response = getRestClient().performRequest(request);
         String text = EntityUtils.toString(response.getEntity());

         JSONObject json = JSONObject.parseObject(text);
         JSONArray columnsJsonArray = json.getJSONArray("columns");
         String columns[] = new String[columnsJsonArray.size()];
         //遍历columns
         for (int i = 0; i < columnsJsonArray.size(); i++) {
            JSONObject columnJsonObject = columnsJsonArray.getJSONObject(i);
            columns[i] = columnJsonObject.getString("name");
         }

         //遍历数据
         JSONArray rowsJsonArray = json.getJSONArray("rows");
         for (int i = 0; i < rowsJsonArray.size(); i++) {
            JSONArray row = rowsJsonArray.getJSONArray(i);

            Map<String, Object> map = new HashMap<String, Object>();
            for (int j = 0; j < row.size(); j++) {
               Object obj = row.get(j);
               if(obj != null){
                  //如果此项不为null,那么加入 map
                  map.put(columns[j], obj);
               }
            }
            list.add(map);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }

      return list;
   }

   /**
    * 注意sql查询并不会分词查询,使用起来效果并不理想
    * @param url
    * @param sqlQuery
    * @param username
    * @param password
    * @return
    */

   public List<Map<String, Object>> searchByHttpClientSqlQuery(String url,String sqlQuery,String username,String password){

      List<Map<String, Object>> list = new ArrayList<>();
      try {
         String text = HttpClient3.doPostHeaderAndSendJson("http://"+url+"/_sql", "{\"query\":\"" + sqlQuery + "\"}",username,password);

         JSONObject json = JSONObject.parseObject(text);
         JSONArray columnsJsonArray = json.getJSONArray("columns");
         String columns[] = new String[columnsJsonArray.size()];
         //遍历columns
         for (int i = 0; i < columnsJsonArray.size(); i++) {
            JSONObject columnJsonObject = columnsJsonArray.getJSONObject(i);
            columns[i] = columnJsonObject.getString("name");
         }

         //遍历数据
         JSONArray rowsJsonArray = json.getJSONArray("rows");
         for (int i = 0; i < rowsJsonArray.size(); i++) {
            JSONArray row = rowsJsonArray.getJSONArray(i);

            Map<String, Object> map = new HashMap<String, Object>();
            for (int j = 0; j < row.size(); j++) {
               Object obj = row.get(j);
               if(obj != null){
                  //如果此项不为null,那么加入 map
                  map.put(columns[j], obj);
               }
            }
            list.add(map);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }

      return list;
   }

   /**
    * group by 统计
    * @param indexName 要统计的是哪个索引(数据库表)
    * @param field 针对的是哪个字段,也就是 group by field ,如传入 username
    * @param queryBuilder 查询条件,传入如
    * <pre>
    * //查询time > 1 AND time < 10
    * QueryBuilders.rangeQuery("time").gt(1).lt(10);
    * </pre>
    *        其中:
    *           <ul>
    *              <li>gt : 大于</li>
    *              <li>gte : 大于等于</li>
    *              <li>lt : 小于</li>
    *              <li>lte : 小于等于</li>
    *           </ul>
    * @return 结果,按照统计条数有大到小排序。如果失败,那么返回的 list.size() 为0
    */
   public List<GroupByListItem> groupBy(String indexName, String field, QueryBuilder queryBuilder){
      SearchRequest searchRequest = new SearchRequest();
      searchRequest.indices(indexName);
      TermsAggregationBuilder aggregation = AggregationBuilders.terms("termsname").field(field+".keyword").order(BucketOrder.count(false)).size(100);
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      searchSourceBuilder.aggregation(aggregation);
      if(queryBuilder != null){
         searchSourceBuilder.query(queryBuilder);
      }
      searchRequest.source(searchSourceBuilder);

      List<GroupByListItem> list = new ArrayList<GroupByListItem>();
      SearchResponse response;
      try {
         response = getRestHighLevelClient().search(searchRequest, RequestOptions.DEFAULT);
         Terms byAgeAggregation = response.getAggregations().get("termsname");
         for (Terms.Bucket buck : byAgeAggregation.getBuckets()) {
            GroupByListItem item = new GroupByListItem();
            item.setName(buck.getKeyAsString());
            item.setCount(buck.getDocCount());
            list.add(item);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }

      return list;
   }

   /**
    * @param countSql 统计的sql语句,传入格式如:
    *     <pre>
    * select count(*) from useraction WHERE action='我是字符串类型' AND time > 1624001953
    *     </pre>
    * @return 执行统计语句所获取到的结果,返回值为int类型
    */
   public int count(String url,String countSql,String username,String password){
      //List<Map<String, Object>> list = searchBySqlQuery(countSql);
      List<Map<String, Object>> list =searchByHttpClientSqlQuery(url,countSql,username,password);
      if(list!=null&&list.size()>0){
         int count = (Integer) list.get(0).values().stream().findAny().get();
         return count;
      }else{
         return 0;
      }

   }


   @Test
   public void testAdd(){
      String indexName = "testind";
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("username", "赵富强的");
      map.put("age", 17);
      map.put("price", 12.6f);
      map.put("a", true);


      /*Map<String, Object> map2 = new HashMap<String, Object>();
      map2.put("username", "zfq");
      map2.put("age", 17);
      map2.put("price", 12.6f);
      map2.put("a", true);

      Map<String, Object> map3 = new HashMap<String, Object>();
      map3.put("username", "msl");
      map3.put("age", 17);
      map3.put("price", 12.6f);
      map3.put("a", true);*/
      List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
      list.add(map);
      map.put("age", 14);
      list.add(map);
      map.put("age", 15);
      list.add(map);
      map.put("username", "zfq");
      list.add(map);
      list.add(map);

      ElasticSearchUtil es = new ElasticSearchUtil("127.0.0.1");
      if(!es.existIndex(indexName)){
         es.createIndex(indexName);
      }

      BulkResponse ir = es.puts(list, indexName);
      System.out.println(ir);
   }

   public static void main(String[] args) {

      String indexName = "testind";
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("username", "赵的");
      map.put("age", 17);
      map.put("price", 12.6f);
      map.put("a", true);


      /*Map<String, Object> map2 = new HashMap<String, Object>();
      map2.put("username", "zfq");
      map2.put("age", 17);
      map2.put("price", 12.6f);
      map2.put("a", true);

      Map<String, Object> map3 = new HashMap<String, Object>();
      map3.put("username", "msl");
      map3.put("age", 17);
      map3.put("price", 12.6f);
      map3.put("a", true);*/
      List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
      list.add(map);

      ElasticSearchUtil es = new ElasticSearchUtil("172.16.10.90000",9200,"http","elastic","123");
      //ElasticSearchUtil es = new ElasticSearchUtil("localhost");
      if(!es.existIndex(indexName)){
         es.createIndex(indexName);
      }
      final int count = es.count("http://172.16.10.90:9200","select count(1)  from knowledge where state= 3 ","elastic","mycomm123");
      System.out.println(count);
      BulkResponse ir = es.puts(list, indexName);

      System.out.println(ir);


//     QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("age:12 AND a:false");
//     SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.query(queryBuilder);
//    System.out.println(es.searchListData(indexName, searchSourceBuilder, 0, 10).toString());

       /*es.cache(map, indexName);
       es.cache(map, indexName);
       es.cache(map, indexName);
       es.cache(map, indexName);
       es.cache(map, indexName);
       es.cache(map, indexName);
       es.cache(map, indexName);
       es.cache(map, indexName);*/
//     System.out.println(es.cacheSubmit(indexName));


//     String method = "GET";
//        String endPoint = "/_sql";
//        Request request = new Request(method, endPoint);
//        request.addParameter("format", "json");
//
//        String sql = "update testind set age = 18 WHERE username = 'zhangqun222'";
//        //String sql = "select * from testind WHERE username = 'zhangqun222'";
//
//        request.setJsonEntity("{\"query\":\""+sql+"\"}");
//
//        Response response = es.getClient().p
              .performRequest(request);
//

//     RestHighLevelClient
//     RestClient restClient = RestClient.builder(
//                new HttpHost("192.168.31.24", 9200, "http")
//         ).build();
//     try {
//       Response response = restClient.performRequest(request);
//       String text = EntityUtils.toString(response.getEntity());
//       System.out.println(text);
//
//       JSONObject json = JSONObject.parseObject(text);
               parseObject(EntityUtils.toString(response.getEntity()));
//       System.out.println(json);
//       JSONArray columnsJsonArray = json.getJSONArray("columns");
//       String columns[] = new String[columnsJsonArray.size()];
//       //遍历columns
//       for (int i = 0; i < columnsJsonArray.size(); i++) {
//          JSONObject columnJsonObject = columnsJsonArray.getJSONObject(i);
//          columns[i] = columnJsonObject.getString("name");
//          System.out.println(columns[i]);
//       }
//       System.out.println(columns);
//
//       //遍历数据
//       JSONArray rowsJsonArray = json.getJSONArray("rows");
//       for (int i = 0; i < rowsJsonArray.size(); i++) {
//          JSONArray row = rowsJsonArray.getJSONArray(i);
//          System.out.println(row);
//       }
//
//    } catch (IOException e) {
//       e.printStackTrace();
//    }


//     List<Map<String, Object>> lists = es.search("useraction", "", 0, 100, null);
//     List<Map<String, Object>> lists = es.search("useraction", "SELECT username, COUNT(*) as number GROUP BY username");
//     List<Map<String, Object>> lists = es.search(indexName, "SELECT * FROM testind WHERE username='zhangqun222'");
//     for (int i = 0; i < lists.size(); i++) {
//       System.out.println(lists.get(i));
//    }
//     System.out.println(lists.size());
//

    /* Map<String, Object> m = es.searchById(indexName, "GxwL5X0Bs1jifER7dBxR");
       System.out.println(m);*/
      boolean b = es.deleteById(indexName, "e7lL5X0BS02zE3K8C11i");
      System.out.println(b);


          /*List<Map<String, Object>> lists = es.searchBySqlQuery("SELECT * FROM knowledge WHERE  type = '1' AND state = '3' AND lore_type = 'sys_basic_yuan_gong'");
       for (int i = 0; i < lists.size(); i++) {
         System.out.println(lists.get(i));
      }*/


      //List<Map<String,Object>> list = es.search("testind", "sum(*)");
      List<Map<String,Object>> listFind = es.search("testind", "username:赵富强'");

      for (int i = 0; i < listFind.size(); i++) {
         System.out.println(listFind.get(i));
      }

      List<Map<String,Object>> listResult = new ArrayList<Map<String,Object>>();

      // 2、指定查询条件
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      // 2.1、查询条件
      searchSourceBuilder.query(QueryBuilders.matchQuery("contentText", "工号"));
      // 2.2、指定高亮
      HighlightBuilder highlightBuilder = new HighlightBuilder();
      highlightBuilder.field("contentText")
            .preTags("<font color='red'>")
            .postTags("</font>");

      searchSourceBuilder.highlighter(highlightBuilder);

      SearchResponse testind = es.search("knowledge", searchSourceBuilder, 0, 10);
      SearchHit[] hits = testind.getHits().getHits();
      // 4、打印
      for (SearchHit hit : testind.getHits().getHits()) {
         HighlightField username = hit.getHighlightFields().get("contentText");
         System.out.println(username);
         Text[] texts=username.getFragments();
         System.out.println(texts[0].toString());
         Map<String, Object>sourceAsMap =hit.getSourceAsMap();
         sourceAsMap.put("id", hit.getId());
         sourceAsMap.put("contentText",texts[0].toString());
         listResult.add(sourceAsMap);
      }
      for (int i = 0; i < listResult.size(); i++) {
         System.out.println(listResult.get(i));
      }
   /* if(testind.status().getStatus() == 200){
         SearchHit shs[] = testind.getHits().getHits();
         for (int i = 0; i < shs.length; i++) {
            Map<String, Object> map = shs[i].getSourceAsMap();
            map.put("esid", shs[i].getId());
            listResult.add(map);
         }
      }else{
         //异常
      }*/



      /*String hostname = "127.0.0.1";
      int port = 9200;
      String scheme = "http";
      HttpHost httpHost = new HttpHost(hostname, port,scheme);
      RestHighLevelClient restHighLevelClient2 = new RestHighLevelClient(RestClient.builder(httpHost));

      // 1、SearchRequest
      SearchRequest searchRequest = new SearchRequest(indexName);

      // 2、指定查询条件
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      // 2.1、查询条件
      searchSourceBuilder.query(QueryBuilders.matchQuery("username", "zfq"));
      // 2.2、指定高亮
      HighlightBuilder highlightBuilder = new HighlightBuilder();
      highlightBuilder.field("username")
            .preTags("<font color='red'>")
            .postTags("</font>");

      searchSourceBuilder.highlighter(highlightBuilder);
      searchRequest.source(searchSourceBuilder);

      // 3、执行
      SearchResponse resp = null;
      try {
         resp = restHighLevelClient2.search(searchRequest, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }

      // 4、打印
      for (SearchHit hit : resp.getHits().getHits()) {
         System.out.println(hit.getHighlightFields());
         System.out.println(hit.getHighlightFields().get("username"));
      }*/

   }
   @Test
   public  void height() throws IOException {
      //没有直接传入 httpshosts,那么就是使用单个的

      String hostname = "127.0.0.1";
      int port = 9200;
      String scheme = "http";
      HttpHost httpHost = new HttpHost(hostname, port,scheme);
      RestHighLevelClient restHighLevelClient2 = new RestHighLevelClient(RestClient.builder(httpHost));

      String indexName="testind";
      // 1、SearchRequest
      SearchRequest searchRequest = new SearchRequest(indexName);

      // 2、指定查询条件
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      // 2.1、查询条件
      searchSourceBuilder.query(QueryBuilders.matchQuery("age", "15"));
      // 2.2、指定高亮
      HighlightBuilder highlightBuilder = new HighlightBuilder();
      highlightBuilder.field("age", 10)
            .preTags("<font color='red'>")
            .postTags("</font>");

      searchSourceBuilder.highlighter(highlightBuilder);
      searchRequest.source(searchSourceBuilder);

      // 3、执行
      SearchResponse resp = restHighLevelClient2.search(searchRequest, RequestOptions.DEFAULT);

      // 4、打印
      for (SearchHit hit : resp.getHits().getHits()) {
         System.out.println(hit.getHighlightFields().get("age"));
      }
   }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要对百万数据的 Elasticsearch 索引进行重命名,可以尝试以下性能优化方法: 1. 执行重命名操作时,尽量避免在同一节点上同时执行其他繁重的操作,以减少节点的负载和竞争。 2. 可以考虑将索引分成多个分片,然后在多个节点上执行并行的重命名操作。这可以通过设置索引的分片数来实现,例如: ``` PUT /my_index/_settings { "index": { "number_of_shards": 5 } } ``` 这将将索引分成 5 个分片,每个分片都可以在不同的节点上处理。 3. 使用 Elasticsearch Bulk API 执行批量操作。Bulk API 可以一次性处理多个操作,从而提高索引重命名的性能,例如: ``` POST /_bulk { "update": { "_id": "1", "_index": "my_index", "_type": "_doc" } } { "doc": { "name": "new_name" } } { "update": { "_id": "2", "_index": "my_index", "_type": "_doc" } } { "doc": { "name": "new_name" } } ... ``` 这将在一次 API 调用中更新多个文档的名称,而不是逐个更新。 4. 在执行重命名操作之前,可以考虑关闭索引的刷新机制。刷新操作会将新数据写入磁盘,从而增加索引重命名的时间和开销。可以使用以下命令关闭索引的刷新机制: ``` POST /my_index/_settings { "index": { "refresh_interval": "-1" } } ``` 这将关闭索引的刷新机制。在执行完索引重命名操作后,可以使用以下命令重新启用刷新机制: ``` POST /my_index/_settings { "index": { "refresh_interval": "1s" } } ``` 这将每秒钟执行一次索引刷新操作。请注意,关闭刷新机制可能会导致某些查询结果不准确,因为查询可能会返回尚未刷新的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值