专栏首页扎心了老铁java优雅的使用elasticsearch api

java优雅的使用elasticsearch api

本文给出一种优雅的拼装elasticsearch查询的方式,可能会使得使用elasticsearch的方式变得优雅起来,使得代码结构很清晰易读。

建立elasticsearch连接部分请参看另一篇博客:http://www.cnblogs.com/kangoroo/p/7127003.html

1、ESDao -- 类似DB中的dao层

封装了增删改查ES的各种方法

package com.dqa.sentinel.client.es;

import com.xiaoju.dqa.sentinel.client.es.entity.ESDocument;
import com.xiaoju.dqa.sentinel.client.es.entity.SearchDocument;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetRequestBuilder;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;


@Component
public class ESDao {

    @Autowired
    private ESClient esClient;

    public void createCache(ESDocument doc) throws Exception {
        IndexResponse response = esCache.getClient().prepareIndex(doc.getIndexName(), doc.getTypeName(), doc.getId())
                .setSource(doc.getSource().getBytes(Charset.forName("UTF-8"))).setRefresh(true).get();
        if (!response.isCreated()) {
            throw new Exception();
        }
    }

    public Map<String, Object> get(ESDocument doc) throws Exception {
        try {
            GetResponse response = esClient.getClient().prepareGet(doc.getIndexName(), doc.getTypeName(), doc.getId()).get();
            return response.getSourceAsMap();
        } catch (Exception ex) {
            throw new Exception();
        }
    }

    public void create(ESDocument doc) throws Exception {
        IndexResponse response = esClient.getClient().prepareIndex(doc.getIndexName(), doc.getTypeName(), doc.getId())
                .setSource(doc.getSource().getBytes(Charset.forName("UTF-8"))).setRefresh(true).get();
        if (!response.isCreated()) {
            throw new Exception();
        }
    }

    public void mutiGet(List<ESDocument> docList) throws Exception {
        MultiGetRequestBuilder multiGetRequestBuilder = esClient.getClient().prepareMultiGet();

        for (ESDocument doc : docList) {
            multiGetRequestBuilder.add(doc.getIndexName(), doc.getTypeName(), doc.getId());
        }
        MultiGetResponse multiGetItemResponses = multiGetRequestBuilder.get();

        for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
            GetResponse response = itemResponse.getResponse();
            if (response.isExists()) {
                String json = response.getSourceAsString();
                System.out.println(json);
            }
        }
    }

    public void update(ESDocument doc) throws Exception {
        try {
            UpdateRequest request = new UpdateRequest().index(doc.getIndexName())
                    .type(doc.getTypeName()).id(doc.getId())
                    .doc(doc.getSource().getBytes(Charset.forName("UTF-8"))).refresh(true);
            UpdateResponse response = esClient.getClient().update(request).get();
            long docVersion = response.getVersion();
            if (!response.isCreated()) {
                throw new Exception();
            }
        } catch (Exception e) {
            throw new Exception();
        }
    }

    public void deleteById(ESDocument doc) throws Exception{
        try {
            DeleteResponse response = esClient.getClient().prepareDelete(doc.getIndexName(),
                    doc.getTypeName(), doc.getId())
                    .execute()
                    .actionGet();
            long docVersion = response.getVersion();
            if (!response.isFound()) {
                throw new Exception();
            }
        } catch (Exception e) {
            throw new Exception();
        }

    }

    public SearchHit[] searchClient(SearchDocument searchDocument) {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        // term
        for (Map<String, String> termMap : searchDocument.getTermList()) {
            TermQueryBuilder termQueryBuilder = termQuery(termMap.get("field"), termMap.get("value"));
            query.must(termQueryBuilder);
        }
        // terms
        for (Map<String, Object> termsMap : searchDocument.getTermsList()) {
            if (termsMap.size() != 0) {
                TermsQueryBuilder termsQueryBuilder = termsQuery(termsMap.get("field").toString(), (Collection<?>) termsMap.get("terms"));
                query.must(termsQueryBuilder);
            }
        }
        // range
        for (Map rangeMap : searchDocument.getRangeList()) {
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(rangeMap.get("field").toString()).from(rangeMap.get("from")).to(rangeMap.get("to")).includeLower(true).includeUpper(false);
            query.must(rangeQueryBuilder);
        }
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(query);
        SearchRequestBuilder searchRequestBuilder = esClient.getClient()
                .prepareSearch(searchDocument.getIndexName()).setTypes(searchDocument.getTypeName());
        SearchResponse response = searchRequestBuilder
                .setQuery(query)
                .setFrom(0).setSize(10000)
                .setExplain(true).execute().actionGet();
        SearchHits hits = response.getHits();
        return hits.getHits();
    }

    

2、查询字段的传入我们是通过Document类完成的

有三个Document,BaseDocument,ESDocument,SearchDocument,后两个继承前一个。

BaseDocument:

public class BaseDocument {
    private String indexName;
    private String typeName;

    public String getIndexName() {
        return this.indexName;
    }
    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }
    public String getTypeName() {
        return this.typeName;
    }
    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

}

ESDocument:

public class ESDocument extends BaseDocument {
    private String id;
    private String source;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getSource() {
        return source;
    }
    public void setSource(String source) {
        this.source = source;
    }

}

SearchDocument

public class SearchDocument extends BaseDocument {
    private List<Map<String, String>> termList;
    private List<Map<String, String>> rangeList;
    private List<Map<String, Object>> termsList;
    public SearchDocument() {
        super();
        termList = new ArrayList<>();
        rangeList = new ArrayList<>();
        termsList = new ArrayList<>();
    }
    public List<Map<String, String>> getTermList() {
        return this.termList;
    }
    public void setTermList(List<Map<String, String>> termList) {
        this.termList = termList;
    }
    public List<Map<String, String>> getRangeList() {
        return this.rangeList;
    }
    public void setRangeList(List<Map<String, String>> rangeList) {
        this.rangeList = rangeList;
    }

    public void setTermsList(List<Map<String, Object>> termsList) {
        this.termsList = termsList;
    }
    public List<Map<String, Object>> getTermsList() {
        return termsList;
    }
}

3、使用的时候,拼装Document,并通过ESDao查询即可

例如:

     SearchDocument searchDocument = new SearchDocument();
        // term查询, field:topicName
        Map<String, String> topicMap = new HashMap<>();
        topicMap.put("field", "topicName");
        topicMap.put("value", topicName.toLowerCase());
        List<Map<String, String>> termList = new ArrayList<>();
        termList.add(topicMap);

        // range查询, field:timestamp
        Map<String, String> timeRange =  new HashMap<>();
        timeRange.put("field", "timestamp");
        timeRange.put("from", from);
        timeRange.put("to", to);
        List<Map<String, String>> rangeList = new ArrayList<>();
        rangeList.add(timeRange);

        searchDocument.setTermList(termList);
        searchDocument.setRangeList(rangeList);
        searchDocument.setIndexName("audit");
        searchDocument.setTypeName("kafka");
        SearchHit[] searchHits = esDao.searchCache(searchDocument);
        String hourStatus = "0";
        for (SearchHit hit : searchHits) {
            Map<String, Object> eachMap = hit.getSource();
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • springboot mybatis优雅的添加多数据源

    springboot的原则是简化配置,本文试图不通过xml配置,使用configuration配置数据源,并进行简单的数据访问。 并且配置了多数据源,在开发过程...

    用户1225216
  • springboot使用zookeeper(curator)实现注册发现与负载均衡

    最简单的实现服务高可用的方法就是集群化,也就是分布式部署,但是分布式部署会带来一些问题。比如: 1、各个实例之间的协同(锁) 2、负载均衡 3、热删除 这里通过...

    用户1225216
  • springboot mybatis redis 二级缓存

    前言 什么是mybatis二级缓存? 二级缓存是多个sqlsession共享的,其作用域是mapper的同一个namespace。 即,在不同的sqlsessi...

    用户1225216
  • Retrofit初探和简单使用

    ? Retrofit简单介绍 Retrofit是Square提供的开源产品,为Android平台的应用提供一个类型安全的REST客户端。它是基于注解,提供 J...

    非著名程序员
  • 《Spring实战》摘录 - 13

    问题: #7.2.1-4 | 与StandardServletMultipartResolver有所不同,CommonsMultipart-Resolver会不...

    用户1335799
  • 一起来学SpringBoot | 第二十一篇:轻松搞定数据验证(三)

    前面两章中详细介绍了数据有效性校验的重要性、自定有数据有效性校验注解 本章也是 轻松搞定数据验证的最后一篇, 一起来揭开神秘的分组验证

    battcn
  • Consul-Proxy:使用netty实现快速服务注册(一)注册服务并提供服务

    Springcloud+consul作为微服务的注册已经见怪不怪了,试下也很流行,在我个人云服务器上,我也是这样做的。

    品茗IT
  • 第80节:Java中的MVC设计模式

    事务,设置自动连接提交关闭. setAutoCommit(false); conn.commit(); conn.rollBack

    达达前端
  • 《挑战30天C++入门极限》C++类的继承与多重继承的访问控制

    landv
  • 宜信开源|(功能上新)UAVStack服务治理之流量控制

    应用微服务化场景下,随着服务个数的增加,服务之间的相互调用变得更加复杂,服务治理需求愈加突出,其中服务流量控制是服务治理中的重要一环。

    宜信技术学院

扫码关注云+社区

领取腾讯云代金券