elasticsearch-rest-high-level-client api

elasticsearch-rest-high-level-client api

文章目录

    • elasticsearch-rest-high-level-client api
      • 编写代码测试
        • count 统计
        • 对查询进行分组
        • 普通查询
        • 新增数据 如果id一致就会触发修改操作
        • 批量新增
        • 根据id删除数据
        • 根据其他字段来批量删除数据
        • Request 对象
      • 将毫秒值在查询出来时转换为 Date
        • yaml 中配置依赖
      • 在 springboot + reids + elasticsearch 整合是会出现 es 与 redis 中的 netty 包冲突
        • 在springboot启动程序前添加
        • 在配置类中添加配置, 加两个的原因是因为如果运行测试方法项目加载顺序的问题

  • 官方给的5.5.3的API并不支持高级API, 发现只有5.6以上的版本支持
  • 官方文档位置

导入依赖

<properties>
    
    <elasticsearch.version>6.2.2elasticsearch.version>
properties>

<dependency>
    <groupId>org.elasticsearch.clientgroupId>
    <artifactId>elasticsearch-rest-high-level-clientartifactId>
    <version>${elasticsearch.version}version>
dependency>

在配置文件中添加es配置

# 设置超时时间默认1s
#spring.elasticsearch.rest.connection-timeout
# 设置连接用户
#spring.elasticsearch.rest.username
# 设置连接密码
#spring.elasticsearch.rest.password
# 设置es读取数据的超时时间默认30s
#spring.elasticsearch.rest.read-timeout
# 设置es地址多个以,分隔
spring.elasticsearch.rest.uris=127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203

编写代码测试

count 统计

// 初始化查询器 并制定 es 索引
SearchRequest request = new SearchRequest()
    .indices(EsConsUtils.INDEX).types(EsConsUtils.INDEX_DOC);

// 初始化条件构造器
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 指定返回 0 条记录
sourceBuilder.size(0);
// 定义普通查询
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("prId", 1688));

// 定义聚合查询
ValueCountAggregationBuilder terms_city = AggregationBuilders.count("count_city").field("id");

// 添加查询条件
sourceBuilder.query(boolQuery);
sourceBuilder.aggregation(terms_city);
request.source(sourceBuilder);

// 发起请求并获取返回值
try {
    SearchResponse response = restHighLevelClient.search(request);
    ValueCount count_city = response.getAggregations().get("count_city");
    return count_city.getValue();
} catch (IOException e) {
    e.printStackTrace();
}

对查询进行分组

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

sourceBuilder.size(0);

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

boolQuery.must(QueryBuilders.termQuery("proId", "520000"));
sourceBuilder.query(boolQuery);

TermsAggregationBuilder termsCity = AggregationBuilders.terms("terms_city").field("city.keyword");

sourceBuilder.aggregation(termsCity);

SearchRequest request = new SearchRequest().indices(EsConsUtils.INDEX).types(EsConsUtils.INDEX_DOC);
request.source(sourceBuilder);

SearchResponse response = restHighLevelClient.search(request);
ParsedStringTerms terms = response.getAggregations().get("terms_city");

JSONObject result = new JSONObject();
for (Terms.Bucket bucket : terms.getBuckets()) {
    result.put(bucket.getKeyAsString(),bucket.getDocCount());
}

普通查询

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("provinceId", 410000));

sourceBuilder.query(boolQuery);
request.source(sourceBuilder);
List<SysLogES> esList = new ArrayList();
SearchResponse response = restHighLevelClient.search(request);

Arrays.stream(response.getHits().getHits())
    .forEach(i -> {
        String sourceAsString = i.getSourceAsString();
        JSONObject resJson = (JSONObject) JSONObject.parse(sourceAsString);
        SysLogES logES = resJson.toJavaObject(SysLogES.class);
        esList.add(logES);
    });

System.out.println(JSON.toJSONString(esList));

新增数据 如果id一致就会触发修改操作

IndexRequest request = new IndexRequest(EsConsUtils.INDEX, EsConsUtils.INDEX_DOC, "10010");

String jsonStr = "{id:10010,name:zs}";
request.source(jsonStr, XContentType.JSON);

IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);

String resIndex = indexResponse.getIndex();
String resType = indexResponse.getType();
String resId = indexResponse.getId();
long resVersion = indexResponse.getVersion();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
    System.out.println("新增文档成功!" + resIndex + resType + resId + resVersion);
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
    System.out.println("修改文档成功!");
}

批量新增

BulkRequest request = new BulkRequest();

List<RestClientTest> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    list.add(new RestClientTest(i, "hashVal00" + i, "zs" + i));
}

for (RestClientTest rest : list) {
    request.add(new IndexRequest(EsConsUtils.INDEX, 
                                 EsConsUtils.INDEX_DOC, 
                                 String.valueOf(rest.getId()))
                .source(JSON.toJSONString(rest), XContentType.JSON));
}

BulkResponse bulkResponse = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
for (BulkItemResponse bulkItemResponse : bulkResponse) {
    DocWriteResponse itemResponse = bulkItemResponse.getResponse();
    if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
        || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
        IndexResponse indexResponse = (IndexResponse) itemResponse;
        //TODO 新增成功的处理
        System.out.println("新增成功,{}" + indexResponse.toString());
    } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
        UpdateResponse updateResponse = (UpdateResponse) itemResponse;
        //TODO 修改成功的处理
        System.out.println("修改成功,{}" + updateResponse.toString());
    } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
        DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
        //TODO 删除成功的处理
        System.out.println("删除成功,{}" + deleteResponse.toString());
    }
}

根据id删除数据

DeleteRequest deleteRequest = new DeleteRequest(EsConsUtils.INDEX, EsConsUtils.INDEX_DOC,  "10086");
DeleteResponse response = null;
try {
response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(response);

根据其他字段来批量删除数据

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name.keyword", "zhangshan");
sourceBuilder.query(termQueryBuilder);
SearchRequest searchRequest = new SearchRequest(EsConsUtils.INDEX);
searchRequest.types(EsConsUtils.INDEX_DOC);
searchRequest.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

SearchHits hits = response.getHits();
List<String> docIds = new ArrayList<>(hits.getHits().length);
for (SearchHit hit : hits) {
    docIds.add(hit.getId());
}

BulkRequest bulkRequest = new BulkRequest();
for (String id : docIds) {
    DeleteRequest deleteRequest = new DeleteRequest(EsConsUtils.INDEX, EsConsUtils.INDEX_DOC, id);
    bulkRequest.add(deleteRequest);
}
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);

Request 对象

对象 操作
IndexRequest 新增
DeleteRequest 删除
SearchRequest 查询
UpdateRequest 修改

将毫秒值在查询出来时转换为 Date

@Data
public class ChineseDateFormat extends SimpleDateFormat {

    private static final long serialVersionUID = 6276491293753307748L;

    public ChineseDateFormat() throws Exception{
        super("y-MM-dd HH:mm:ss", Locale.CHINESE);
        setTimeZone(TimeZone.getTimeZone("GMT+8"));
    }
    
}

yaml 中配置依赖

spring:
  jackson:
    date-format: com.config.ChineseDateFormat

在 springboot + reids + elasticsearch 整合是会出现 es 与 redis 中的 netty 包冲突

在springboot启动程序前添加

System.setProperty("es.set.netty.runtime.available.processors", "false");

在配置类中添加配置, 加两个的原因是因为如果运行测试方法项目加载顺序的问题

@Configuration
public class ElasticsearchConfig {

    @PostConstruct
    void init() {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
    }

}

你可能感兴趣的:(ElasticSearch)