原创

ElasticSerch学习之路(2)---测试代码篇

由于国庆去嗨了,博主没有更新文章

package elasticsearch;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import fetter.three.bear.bean.FileCenter;

public class TestEs {
    private static String esHost = "xxx.xxx.xxx.xx";
    private static int esPort = 9200;

    public static RestHighLevelClient getClient() {
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost(esHost, esPort, "http")));
        return restHighLevelClient;
    }

    public static void start() throws IOException {
        List<FileCenter> list2 = testCun();
        for (FileCenter fileCenter : list2) {
            System.out.println(fileCenter.getFileAddress());
        try (RestHighLevelClient re = getClient()) {// JDK1.8新写法,执行完以后自动释放资源
            List<String> list = new ArrayList<>();
        Map<String, Object> jsonMap = new HashMap<>();
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jsonMap.put("user", fileCenter.getFileAddress());
        jsonMap.put("postDate", df2.format(new Date()));
        jsonMap.put("message", fileCenter.getFileAddress());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String sf = "hhs_" + df.format(new Date().getTime());
        IndexRequest indexRequest = new IndexRequest("test-"+sf);// 设置索引 如果相同索引存在 测添加新文档,如果不存在则创建新索引
        indexRequest.timeout(TimeValue.timeValueMinutes(30));// 超时,等待所有节点被确认
        // indexRequest.id("2");//手动设置ID号 不设置时为自动生成20位字母随机大小写组成的字符串ID
        indexRequest.source(jsonMap);// 以MAP形式传入文档
            // 设置传入的请求,分片规则等采用默认方式==1分片1副本
            IndexResponse response = re.index(indexRequest, RequestOptions.DEFAULT);
            jsonMap.clear();
            System.out.println("==================================" + response.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
        // restHighLevelClient.close();//关闭服务 释放资源
    }

    //该索引下所有文档
    public static void getDoc() throws IOException {
        try (RestHighLevelClient re = getClient()) {// JDK1.8新写法,执行完以后自动释放资源
            long start = System.currentTimeMillis();
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); 
//            sourceBuilder.query(QueryBuilders.termsQuery("message", "哦哦哦99991").boost(1)); //模糊查询
            sourceBuilder.query(QueryBuilders.rangeQuery("postDate").gt("2019-06-26 12:46:11").lt("2019-06-26 15:45:11")); //根据时间筛选查询  gt:大于  gte:大于等于 lt:小于 lte:小于等于
//            sourceBuilder.query(QueryBuilders.matchAllQuery()); //查询所有
//            sourceBuilder.query(QueryBuilders.matchQuery("message", "哦")); //查询所有
            sourceBuilder.from(0); 
            sourceBuilder.size(2000); 
            sourceBuilder.timeout(new TimeValue(30, TimeUnit.SECONDS));
            SearchRequest searchRequest = new SearchRequest("test-hy").source(sourceBuilder);
//            searchRequest.indices("hhs_2019-06");
//            searchRequest.source(sourceBuilder);
            SearchResponse response = re.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits  s = response.getHits();
            int a = s.getHits().length;
            System.out.println(a);
            for (SearchHit searchHit : s) {
                System.out.println(searchHit.getSourceAsString().toCharArray());
            }
            long end = System.currentTimeMillis();
            System.out.println("耗时"+(end-start)+"毫秒");
        }


    }

    public static List<FileCenter> testCun(){
        List<FileCenter> list = new ArrayList<>();
        for(int i =0 ; i<10; i++) {
            FileCenter f = new FileCenter();
            f.setFileAddress("啊啊啊"+i);
            f.setFileName("哦哦哦"+i);
            f.setId("哈哈哈"+i);
//            System.out.println(i);
            list.add(f);
        }

        return  list;
    }


    public static void main(String[] args) throws IOException, InterruptedException {
        start();
//        getDoc();
//        List<FileCenter> list2 = testCun();
//        for (FileCenter fileCenter : list2) {
//            Map<String,String> map1 = new HashMap<>();
//            map1.put("zhangsan", "a");
//            map1.put("name", "b");
//            map1.put("id", "c");
//            System.out.println(map1);
//            Thread.sleep(5000);
//            map1.put("zhangsan3", "d");
//            map1.put("name3", "e");
//            map1.put("id3", "f");
//            System.out.println(map1);
        }
//    }
}
该测试可以复制下来填上IP以后直接运行。基本都是按照官网推荐的方式进行测试,留着以后需要的时候方便继续测试,因为后面ES 这块有大佬已经完成了,所以博主就在慢慢学习大佬写的代码,
以后需要的时候再进行汇总,写自己的方法。
用到的依赖
<dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.1.1</version>
        </dependency>
        <!-- <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> 
            <version>7.1.1</version> </dependency> -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.1.1</version>
        </dependency>
正文到此结束
本文目录