java 操作elasticsearch6.8.0 简单测试 及springboot 操作elasticsearch6.8.0

java 操作elasticsearch6.8.0 

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.springboot</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>transport-netty4-client</artifactId>
            <version>6.8.0</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

        <!--测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <build>

        <!-- 添加资源 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- src/main/resources下的指定资源放行 -->
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>


        </plugins>
    </build>

</project>

代码 简单测试 

package com.springboot.elasticsearchDemo.controller;


import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
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.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

/**
 * TODO
 *
 * @version: 1.0
 * @author: uniqueliang
 * @date: 2021/1/14 10:11
 * @desc:*/


public class ElasticsearchClient {

    @Test
    public void InitClient() throws UnknownHostException {

        //从创建一个 客户端
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);

        //设置操作es服务主机及端口号
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.201"), 9300));

        //操作 添加索引

//        CreateIndexResponse dangdang = transportClient.admin().indices().prepareCreate("dangdang").get();
//        boolean acknowledged = dangdang.isAcknowledged();
//        System.out.println(acknowledged);

        //通过id获取数据
//        GetResponse documentFields = transportClient.prepareGet("businssstatisticslog", "_doc", "QjJZg3YB5kuPgKTVqzyr").get();
//
//        String sourceAsString = documentFields.getSourceAsString();
//        System.out.println(sourceAsString);


        //各种查询
//        MatchQueryBuilder uuid = QueryBuilders.matchQuery("uuid", "68a4fdeb92f84334823b6d5cc33c32dc");
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();

        //range查询
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gte(0).lte(10);

        //通配符查询
        WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("content", "m*");

        //前缀查询
        PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery("content", "框");

        //ids
        IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery().addIds("QjJZg3YB5kuPgKTVqzyr").addIds("QDJZg3YB5kuPgKTVqzyr");

        //fuzzy 模糊查询 0~2 不允许模糊  3~5 可以出现一个模糊  >5 最多出现两个模糊
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("industry", "开发");

        //bool查询    支持must should mustNot
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("industry", "其他金融业"));

        //创建 highlightBuilder
        HighlightBuilder highlightBuilder = new HighlightBuilder()
                .field("*")
                .requireFieldMatch(false)
                .preTags("<span style='color:red;'>")
                .postTags("</span>");

        SearchResponse searchResponse = transportClient.prepareSearch("businssstatisticslog")
                .setTypes("_doc")//指定类型
                .setQuery(boolQueryBuilder) //条件查询
                .highlighter(highlightBuilder) //高亮处理
                .setPostFilter(QueryBuilders.rangeQuery("handleCreate").gt(1).lte(10))
                .setFrom(0)
                .setSize(20)
                .addSort("consumerTime", SortOrder.DESC)
                //.setSource(SearchSourceBuilder.searchSource().fetchSource("*","handleUpdate"))
                .get();

        long totalHits = searchResponse.getHits().totalHits;
        System.out.println("总条数:"+totalHits);
        SearchHit[] hits = searchResponse.getHits().getHits();

        for (SearchHit hit : hits) {
            String sourceAsString = hit.getSourceAsString();
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            System.out.println(highlightFields);
            System.out.println(sourceAsString);
        }


        //释放资源
        transportClient.close();
    }


//    * 过滤查询  主要是在查询执行之前对大量数据进行筛选
//    *  postFilter  用于过滤
//    *

    @Test
    public void testQuery() throws UnknownHostException {

        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.201"),9300));


        SearchResponse searchResponse = transportClient.prepareSearch("businssstatisticslog")
                .setTypes("_doc")
                .setPostFilter(QueryBuilders.rangeQuery("handleCreate").gt(1).lte(10))  //过滤查询
                .setQuery(QueryBuilders.matchAllQuery())
                .get();


        long totalHits = searchResponse.getHits().totalHits;
        System.out.println("总条数:"+totalHits);


        SearchHit[] hits = searchResponse.getHits().getHits();

        for (SearchHit hit:hits){
            String sourceAsString = hit.getSourceAsString();
            System.out.println(sourceAsString);
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }


        transportClient.close();
    }





}

 

 

springboot 操作elasticsearch6.8.0 

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.elasticsearch</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--devtools热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--fastjson-->

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.32</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

 

 

配置config 工具类

package com.elasticsearch.springbootdemo.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * TODO
 *
 * @version: 1.0
 * @author: uniqueliang
 * @date: 2021/1/23 18:05
 * @desc:
 */
@Configuration
public class EsConf extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("192.168.1.201:9200")
                .build();

        return RestClients.create(clientConfiguration).rest();
    }
}

controller  层

package com.elasticsearch.springbootdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.elasticsearch.springbootdemo.util.ResultUtils;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @version: 1.0
 * @author: uniqueliang
 * @date: 2021/1/23 18:12
 * @desc:
 */
@RestController
@RequestMapping("/es")
public class SpringbootEs {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    Logger logger = LoggerFactory.getLogger(SpringbootEs.class);

    /*根据id查询数据*/
    @PostMapping("/getDataById")
    public JSONObject getDataById(String info) throws IOException {
        System.out.println("id");

        if (info == null || info.equals("")) {
            return ResultUtils.success("参数不能为null或者是空字符...");
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(info);
        } catch (Exception e) {
            return ResultUtils.fail("参数格式不对...");
        }
        String id = jsonObject.getString("id");

        String index = jsonObject.getString("index");

        GetRequest getRequest = new GetRequest(index, "_doc", id);

        GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(documentFields.getSourceAsMap());
        return ResultUtils.success(documentFields.getSourceAsString());
    }

    /*根据uid查询数据*/
    @PostMapping("/getDataByUid")
    public JSONObject getDataByUid(String info) throws IOException {
        if (info == null) {
            return ResultUtils.success("参数不能为null");
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(info);
        } catch (Exception e) {
            return ResultUtils.fail("参数格式不对...");
        }
        String uid = jsonObject.getString("uid");

        String index = jsonObject.getString("index");

        SearchRequest searchRequest = new SearchRequest(index);
        /*创建searchSourceBuilder*/
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        /*引入term查询*/
        searchSourceBuilder.query(QueryBuilders.termQuery("uuid", uid));

        /*返回参数设置*/
        searchSourceBuilder.fetchSource("", "");

        searchRequest.source(searchSourceBuilder);

        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = search.getHits().getHits();
        String sourceAsString = "";
        for (SearchHit hit : hits) {
            sourceAsString = hit.getSourceAsString();
        }
        System.out.println(sourceAsString);
        return ResultUtils.success(sourceAsString);
    }

    @PostMapping("/getData")
    public JSONObject getData(String info) throws IOException {

        logger.info("info>>>" + info);
        if (info == null) {
            return ResultUtils.success("参数不能为null");
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(info);
        } catch (Exception e) {
            return ResultUtils.fail("参数格式不对...");
        }

        String index = jsonObject.getString("index");
        Integer pageNo = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String fromTime = jsonObject.getString("start_time");
        String toTime = jsonObject.getString("end_time");
        String operateType = jsonObject.getString("operate_type");


        String time1 = "consumerTime";
        /*根据不同的index 返回的参数不同*/
        String[] strings = null;
        if (index.equals("handlemonitorlog")) {
            strings = new String[]{"uuid", "sysTime"};
        } else if (index.equals("handlesystemlog")) {
            strings = new String[]{"uuid", "createAt", "operateType"};
        }
        /*创建 searchRequest */
        SearchRequest searchRequest = new SearchRequest(index);
        /*创建searchSourceBuilder*/
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        /*引入bool查询*/
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        /*根据前端传值判断时间的范围*/
        if (fromTime != null && !fromTime.isEmpty() && toTime != null && !toTime.isEmpty()) {
            logger.info("fromTime>>>toTime" + fromTime + "__" + toTime);
            /*将range查询放入到bool查询中*/
            boolQueryBuilder.must(QueryBuilders.rangeQuery(time1).gte(fromTime).lte(toTime));
        } else if (fromTime != null && !fromTime.isEmpty() && toTime.isEmpty()) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(time1).gte(fromTime));
        } else if (!toTime.isEmpty()) {
            logger.info("toTime>>>" + toTime);
            boolQueryBuilder.must(QueryBuilders.rangeQuery(time1).lte(toTime));
        }
        /*根据前端传入的operateType值做筛选条件*/
        if (operateType != null && !operateType.isEmpty()) {
            /*创建term查询*/
            TermQueryBuilder operateType1 = QueryBuilders.termQuery("operateType", operateType);
            /*将term查询放入到bool查找中*/
            boolQueryBuilder.must(operateType1);
        }
        /*打印生成的curl查询条件 如:
        {
  "bool": {
    "must": [
      {
        "range": {
          "consumerTime": {
            "from": "0",
            "to": null,
            "include_lower": true,
            "include_upper": true,
            "boost": 1.0
          }
        }
      },
      {
        "term": {
          "operateType": {
            "value": "resolution",
            "boost": 1.0
          }
        }
      }
    ],
    "adjust_pure_negative": true,
    "boost": 1.0
  }
}

         */
        logger.info(boolQueryBuilder.toString());
        /*将bool查询放入到searchSourceBuilder中*/
        searchSourceBuilder.query(boolQueryBuilder);
        /*设置from+size*/
        searchSourceBuilder.from(pageNo - 1);
        searchSourceBuilder.size(pageSize);
        /*设置返回的数据参数*/
        searchSourceBuilder.fetchSource(strings, new String[]{});
        /*将searchSourceBuilder 加入到searchRequest 中*/
        searchRequest.source(searchSourceBuilder);

        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        /*获取符合条件的总数*/
        long totalHits = search.getHits().totalHits;
        System.out.println(totalHits);
        /*获取数据*/
        SearchHit[] hits = search.getHits().getHits();
        List<String> list = new ArrayList<>();
        for (SearchHit hit : hits) {
            list.add(hit.getSourceAsString());
        }
        int pageNum = 0;
        if (totalHits % pageSize == 0) {
            pageNum = (int) totalHits / pageSize;
        } else {
            pageNum = (int) totalHits / pageSize + 1;
        }
        /*返回前端的参数设置*/
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("item", list.toArray());
        jsonObject1.put("pageSize", pageSize);
        jsonObject1.put("currentPage", pageNo);
        jsonObject1.put("totalNum", totalHits);
        jsonObject1.put("totalPage", pageNum);
        return ResultUtils.successObject(jsonObject1);
    }


}

 

ResultUtils工具类

package com.elasticsearch.springbootdemo.util;

import com.alibaba.fastjson.JSONObject;

/**
 * TODO
 *
 * @version: 1.0
 * @author: uniqueliang
 * @date: 2021/1/25 16:35
 * @desc:
 */

public class ResultUtils {


    public static JSONObject success(String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "success");
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功...");
        jsonObject.put("data", message);
        return jsonObject;
    }

    public static JSONObject successObject(JSONObject message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "success");
        jsonObject.put("code", 200);
        jsonObject.put("message", "查询成功...");
        jsonObject.put("data", message);
        return jsonObject;
    }

    public static JSONObject fail(String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "fail");
        jsonObject.put("code", 201);
        jsonObject.put("message", "查询失败...");
        jsonObject.put("data", message);
        return jsonObject;
    }

}

resource 下的application.properties

server.port=8088
server.servlet.context-path=/


#设置开启热部署
spring.devtools.restart.enabled=true

 

启动springboot

通过postmen 测试

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值