乐趣区

关于后端:RestHighLevelClient简单使用

本文转自 SDK 社区:sdk.cn

一、创立连贯
!ps:RestHighLevelClient 是官网指定的连贯 API。另外一个是 TransportClient,然而 TransportClient 这个是曾经废除不必的,所以会在 ES8.0 之后齐全移除,也就是说 8.0 之后就无奈应用了
RestHighLevelClient 的 maven 依赖为(应用前必须增加相应依赖)

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.2</version>
</dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.9.2</version>
</dependency>

留神:以上的依赖版本能够依据你应用的 ES 的版本来定,向下兼容,然而无奈向上兼容

创立连贯

static String ip = "localhost";
static int port = 9200;
static RestHighLevelClient restHighLevelClient = null;
static TransportClient client = null;

static  RestHighLevelClient initClient(){// 这里的 RestClient.builder(new HttpHost(ip,port),.........)反对多个 httphost 连贯,也就是反对连贯多个 elasticsearch
    restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost(ip,port)));
}

static Connection getInstance(){synchronized (ElasticSearchFactory.class){if (restHighLevelClient == null){restHighLevelClient  =   initClient();
        }
    }
    return connection;
}

RestHighLevelClient 反对对 ES 的增删改查、批量操作
新增:

    public static  void creatIndex(String json){IndexRequest indexRequest = new IndexRequest("test_index","String");
    indexRequest.source(json, XContentType.JSON);// 这里的 json 为存入 ES 的文档内容
    try {restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);//RequestOptions 在新办法中必须抉择
    } catch (IOException e) {e.printStackTrace();
    }
}

查问:
查问反对间接查问和条件查问,因为 ES 是依据索引、文档类型和文档 ID 确定一个惟一的文档的,所以间接查问必须要有索引和类型还有 ID

public static Map<String, Object> getIndex(String indexName,String type,String id){GetRequest request = new GetRequest(indexName);
    //SearchRequest searchRequest = new SearchRequest(indexName);
    try {GetResponse search = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        Map<String, Object> source = search.getSource();
        return source;
    } catch (IOException e) {e.printStackTrace();
    }
    return new HashMap<>();}

条件查问:
条件查问反对所有的 ES 的查问规定

public static Map<String, Object> searchIndex(String indexName){//GetRequest request = new GetRequest(indexName);
    //SearchRequest searchRequest = new SearchRequest(indexName);
    QueryBuilder queryBuilders = QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("queryKey","吱吱吱 ZZZ")
    );
    SearchRequest searchRequest = new SearchRequest();
    //searchRequest.indices("posts"); 这里能够设计 GEET 或者 POST
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); 反对按评分排序,评分是指分词之后获得的文档类似度评分
    searchSourceBuilder.query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("queryKey.keyword","吱吱吱 ZZZ")));
    searchRequest.source(searchSourceBuilder);
    try {SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = search.getHits().getHits();
        for (SearchHit searchHit : hits){Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
            System.out.println("source : {}" + sourceAsMap.toString());
        }
    } catch (IOException e) {e.printStackTrace();
    }
    return new HashMap<>();}

批改:
ps:ES 中其实并不存在真正意义上的批改,其实是进行的文档笼罩,先将原文档查出来标记删除,而后在原文档的根底上增加上批改造成一份新文档,存入 ES 中,之后再将打上删除标记的文档删除。

public static void updateIndex(String indexName, String type, String id, String json){UpdateRequest request = new UpdateRequest(indexName, type, id);

    request.doc(json, XContentType.JSON);
    try {UpdateResponse updateResponse = restHighLevelClient.update(request,RequestOptions.DEFAULT);
    } catch (IOException e) {e.printStackTrace();
    }
}

删除:

public static void deleteIndex(String indexName, String type, String id){DeleteRequest request = new DeleteRequest(indexName,type,id);
    try {restHighLevelClient.delete(request,RequestOptions.DEFAULT);
    } catch (IOException e) {e.printStackTrace();
    }
}

bulk 操作,bulk 操作相当于批处理,可能在一次操作中解决一批数据,同时可能同时解决,增删改操作

  public static void bulkIndex(){BulkRequest request = new BulkRequest();
        String jsonString="{\n" +
                "\t\"databaseId\": \" 謝謝謝謝謝謝 \",\n" +
                "\t\"databaseType\": \" 嘻嘻嘻 \",\n" +
                "\t\"queryKey\":\" 吱吱吱 ZZZ\"\n" +
                "}\n";
    UUID uuid = UUID.randomUUID();
    request.add(new IndexRequest("test_index", "String", uuid.toString())
                .source(jsonString,XContentType.JSON));
        String updateJson="{\n" +
                "\"queryKey\":\" 吱吱吱 ZZZ\"\n" +
                "}";
        request.add(new UpdateRequest("test_index", "String", uuid.toString())
                .doc(updateJson,XContentType.JSON));
        request.add(new DeleteRequest("test_index", "String", uuid.toString()));

    try {restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
    } catch (IOException e) {e.printStackTrace();
    }
}

执行 ES-DSL

String json = "{\"bool\":{\"filter\":[{\"term\":{\"queryKey.keyword\":\" 吱吱吱 ZZZ\"}}]}}";
String index= "test_index";
public static void wapQuerty(String json,String index){WrapperQueryBuilder queryBuilder = new WrapperQueryBuilder(json);
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(queryBuilder);
    searchRequest.source(searchSourceBuilder).indices(index);
    try {SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = search.getHits().getHits();
        for (SearchHit searchHit : hits){Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
            String id = searchHit.getId();
            System.out.println("id:"+id);
        }
    } catch (IOException e) {e.printStackTrace();
    }
}
退出移动版