美文网首页
elk 2:springboot 集成 es highlevel

elk 2:springboot 集成 es highlevel

作者: _Rondo | 来源:发表于2020-10-21 17:25 被阅读0次

    一、前言

    上篇是springdata es 提供方法的记录,这篇记录下highlevel cli的使用

    二、示例

    pom

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.73</version>
            </dependency>
    
            <dependency>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
            </dependency>
    
    

    修改配置文件

    spring:
      elasticsearch:
        rest:
          uris: http://127.0.0.1:9200
    

    用到一个工具类

    package com.example.highlevel.util;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    
    import com.alibaba.fastjson.JSONObject;
    import lombok.AllArgsConstructor;
    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.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.assertj.core.util.Lists;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.action.delete.DeleteRequest;
    import org.elasticsearch.action.get.GetRequest;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.action.update.UpdateRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestClientBuilder;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.client.indices.CreateIndexRequest;
    import org.elasticsearch.client.indices.CreateIndexResponse;
    import org.elasticsearch.client.indices.GetIndexRequest;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.index.query.BoolQueryBuilder;
    import org.elasticsearch.index.query.MultiMatchQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.index.query.TermQueryBuilder;
    import org.elasticsearch.rest.RestStatus;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.elasticsearch.search.sort.ScoreSortBuilder;
    import org.elasticsearch.search.sort.SortOrder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ElasticUtils {
    
      private static final Logger LOGGER = LogManager.getLogger(ElasticUtils.class);
    
      @Autowired
      RestHighLevelClient client;
      public List<JSONObject> searchLike(String indexName, Map<String, Object> condition, int page,
                                         int pageSize) throws IOException {
        if (!checkExistIndex(indexName)) {
          return Lists.newArrayList();
        }
        SearchRequest searchRequest = new SearchRequest(indexName);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    
        if (condition != null) {
          for (String k : condition.keySet()) {
            TermQueryBuilder termQuery = QueryBuilders.termQuery(k, condition.get(k));
            boolQueryBuilder.must(termQuery);
          }
        }
    
        sourceBuilder.query(boolQueryBuilder);
        // 分页
        sourceBuilder.from((page - 1) * pageSize);
        sourceBuilder.size(pageSize);
        //匹配度从高到低
        sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        //sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC)); //根据自己的需求排序
    
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    
        RestStatus restStatus = searchResponse.status();
        if (restStatus != RestStatus.OK) {
          LOGGER.error(restStatus);
          return null;
        }
        SearchHits hits = searchResponse.getHits();
        List<JSONObject> matchRsult = Lists.newArrayList();
        for (SearchHit hit : hits.getHits()) {
          matchRsult.add(JSONObject.parseObject(hit.getSourceAsString()));
        }
        return matchRsult;
      }
    
      public List<JSONObject> searchFullIndex(String indexName, int page, int pageSize, Object content,
          String... fields) throws IOException {
        if (!checkExistIndex(indexName)) {
          return Lists.newArrayList();
        }
        SearchRequest searchRequest = new SearchRequest(indexName);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    
    //        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(fieldName, keyword).fuzziness(Fuzziness.AUTO); //模糊匹配
        MultiMatchQueryBuilder matchQueryBuilder = new MultiMatchQueryBuilder(content, fields);
    
        sourceBuilder.query(matchQueryBuilder);
        // 分页
        sourceBuilder.from((page - 1) * pageSize);
        sourceBuilder.size(pageSize);
        //匹配度从高到低
        sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        //sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC)); //根据自己的需求排序
    
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    
        RestStatus restStatus = searchResponse.status();
        if (restStatus != RestStatus.OK) {
          LOGGER.error(restStatus);
          return null;
        }
        SearchHits hits = searchResponse.getHits();
        List<JSONObject> matchRsult = Lists.newArrayList();
        for (SearchHit hit : hits.getHits()) {
          matchRsult.add(JSONObject.parseObject(hit.getSourceAsString()));
        }
        return matchRsult;
      }
    
      public List<JSONObject> searchFullIndexFilterUserId(String indexName, int userId, int page,
          int pageSize, Object content, String... fields) throws IOException {
        if (!checkExistIndex(indexName)) {
          return Lists.newArrayList();
        }
        SearchRequest searchRequest = new SearchRequest(indexName);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    
        TermQueryBuilder termQuery = QueryBuilders.termQuery("userId", userId);
        boolQueryBuilder.must(termQuery);
    
    //        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(fieldName, keyword).fuzziness(Fuzziness.AUTO); //模糊匹配
        MultiMatchQueryBuilder matchQueryBuilder = new MultiMatchQueryBuilder(content, fields);
        boolQueryBuilder.must(matchQueryBuilder);
    
        sourceBuilder.query(boolQueryBuilder);
        // 分页
        sourceBuilder.from((page - 1) * pageSize);
        sourceBuilder.size(pageSize);
        //匹配度从高到低
        sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        //sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC)); //根据自己的需求排序
    
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    
        RestStatus restStatus = searchResponse.status();
        if (restStatus != RestStatus.OK) {
          LOGGER.error(restStatus);
          return null;
        }
        SearchHits hits = searchResponse.getHits();
        List<JSONObject> matchRsult = Lists.newArrayList();
        for (SearchHit hit : hits.getHits()) {
          matchRsult.add(JSONObject.parseObject(hit.getSourceAsString()));
        }
        return matchRsult;
      }
    
      public void deleteDocument(String indexName, String id) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
        client.delete(deleteRequest, RequestOptions.DEFAULT);
      }
    
      public JSONObject getDocument(String indexName, String id) throws IOException {
        GetRequest request = new GetRequest(indexName, id);
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        if (!response.isExists()) {
          return null;
        } else {
          return JSONObject.parseObject(response.getSourceAsString());
        }
      }
    
      public void addDocument(String indexName, String id, String jsonStr) throws IOException {
        IndexRequest indexRequest = new IndexRequest(indexName).id(id)
            .source(jsonStr, XContentType.JSON);
        client.index(indexRequest, RequestOptions.DEFAULT);
      }
    
      public void updateDocument(String indexName, String id, String jsonStr) throws IOException {
        UpdateRequest updateRequest = new UpdateRequest(indexName, id);
        updateRequest.doc(jsonStr, XContentType.JSON);
        client.update(updateRequest, RequestOptions.DEFAULT);
      }
    
      public void updateDocument(String indexName, String id, JSONObject updatedFields)
          throws IOException {
        UpdateRequest updateRequest = new UpdateRequest(indexName, id);
        for (String k : updatedFields.keySet()) {
          updateRequest.doc(k, updatedFields.get(k));
        }
        client.update(updateRequest, RequestOptions.DEFAULT);
      }
    
      public boolean checkExistIndex(String indexName) throws IOException {
        GetIndexRequest request = new GetIndexRequest(indexName);
        return client.indices().exists(request, RequestOptions.DEFAULT);
      }
    
      public void deleteIndex(String indexName) throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
        client.indices().delete(request, RequestOptions.DEFAULT);
      }
    
      public boolean createIndex(String indexName) throws IOException {
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        JSONObject setting = new JSONObject();
        setting.put("index.analysis.analyzer.default.type", "ik_smart");
        request.settings(setting.toString(), XContentType.JSON);
        CreateIndexResponse createIndexResponse = client.indices()
            .create(request, RequestOptions.DEFAULT);
        if (!createIndexResponse.isAcknowledged()) {
          return false;
        }
        return true;
      }
    
    }
    

    实体

    package com.example.highlevel.bean;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import org.springframework.data.elasticsearch.annotations.Document;
    
    @Document(indexName = "at")
    @Data
    @AllArgsConstructor
    public class Book {
        private Integer id;
        private String bookname;
        private String author;
    
    }
    

    接口

    package com.example.highlevel.dao;
    
    import com.example.highlevel.bean.Book;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public interface Bookrepository extends ElasticsearchRepository<Book,Integer> {
        //ElasticsearchCrudRepository 已经过时
        List<Book> findBookById(int i);
    }
    

    测试也包含了util

    package com.example.highlevel;
    
    import com.alibaba.fastjson.JSONObject;
    import com.example.highlevel.bean.Book;
    import com.example.highlevel.dao.Bookrepository;
    import com.example.highlevel.util.ElasticUtils;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @SpringBootTest
    class HighLevelApplicationTests {
    
        @Autowired
        Bookrepository bookrepository;
    
        @Autowired
        ElasticUtils elasticUtils;
    
        @Test
        void contextLoads() {
            Book book=new Book(1,"西游记","吴承恩");
            bookrepository.save(book);
        }
    
        @Test
        void testRepositories(){
            //查询
            //Elasticsearch Repositories提供and,by等一大堆关键字来连接JAVABEAN属性,我们写接口,他自动变成为实现类。
            List<Book> bookById = bookrepository.findBookById(1);
            System.out.println(bookById.get(0));
        }
    
        @Test
        void testUtil() throws IOException {
            String index = "test-index";
            elasticUtils.createIndex(index);
    
        }
    
        @Test
        void testUtilAdd() throws IOException {
            String index = "test-index";
            elasticUtils.addDocument(index,"001","{\"name\":\"阿萨的那家店哈登记户口是的金卡贷记卡大家看到\"}");
            elasticUtils.addDocument(index,"002","{\"name\":\"呵呵泥潭阿玛大数据库打死你健康大数据南科大\"}");
        }
    
        @Test
        void testUtilGet() throws IOException {
            String index = "test-index";
            JSONObject jsonObject = elasticUtils.getDocument(index,"001");
            System.out.println(jsonObject.toJSONString());
        }
    
        @Test
        void testUtilSearch() throws IOException {
            String index = "test-index";
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("name","阿");
            List<JSONObject> jsonObjects = elasticUtils.searchLike(index,objectMap,1,10);
            jsonObjects.forEach(jsonObject -> System.out.println(jsonObject.toJSONString()));
    
        }
    
    }
    
    

    -end-

    相关文章

      网友评论

          本文标题:elk 2:springboot 集成 es highlevel

          本文链接:https://www.haomeiwen.com/subject/kzsemktx.html