SpringBoot整合ElasticSearch执行查询操作

对于ElasticSearch的介绍我就不多说了,能看到这篇博客想必你还是了解它是干嘛的,在使用java去连接这个nosql的时候我建议还是事先去学习一下它的语句语法,当你会这些语法的时候用java的api调用它才会更轻松明理,好了话不多说,直接贴代码:

maven

<!--springdata对es-->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client-sniffer</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>

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

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

        <dependency>
            <groupId>com.bbossgroups.plugins</groupId>
            <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
            <version>5.2.5</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

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

配置类

package com.hw.config;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * @program: springboot-03-elasticsearch
 * @description:
 * @author: hw
 **/

@Component
public class TransportClientUitl {
    private TransportClient transportClient;
    //transportClient的加载配置
    public TransportClient getClient() throws Exception{
            //连接服务器                                                             //my-application是es的集群名,在配置文件中有
            transportClient  = new PreBuiltTransportClient(Settings.builder().put("cluster.name", "my-application").build())
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.217.155"), 9300));//服务器地址和端口号
        return transportClient;
    }
}

测试方法

//自动注入
    @Autowired
    private TransportClientUitl clientUitl;
    @Test
    public void test2() throws Exception {
        SearchRequestBuilder searchBuilder = clientUitl.getClient().prepareSearch("blog");
        searchBuilder.setTypes("log");
        //设置每批读取的的数据量
        searchBuilder.setSize(10000);
        //默认是查询所有
        //searchBuilder.setQuery(QueryBuilders.queryStringQuery("*:*"));
        //设置 search context 维护1分钟的有效期
        searchBuilder.setScroll(TimeValue.timeValueMillis(3));
        //查询时间范围内的数据
        //RangeQueryBuilder builder=QueryBuilders.rangeQuery("crawlerdate").from("2019-02-21").to("2019-03-01");
        //根据title来精确查询
        MatchPhraseQueryBuilder builder = QueryBuilders.matchPhraseQuery("title", "jsonp解决跨域问题");
        //根据title来查询
//        MatchQueryBuilder builder = QueryBuilders.matchQuery("title", "jsonp解决跨域问题");

        //获得首次查询结果
        SearchResponse searchResponse = searchBuilder.setQuery(builder).setPostFilter(builder).get();
        System.out.println("命中总数量:" + searchResponse.getHits().getTotalHits());
        //打印计数
        int count = 1;

        do {
            System.out.println("第" + count + "次打印数据:");
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                String sourceAsString = hit.getSourceAsString();
                System.err.println(sourceAsString);
            }
            count++;
            //将scorllId循环传递
            searchResponse=clientUitl.getClient().prepareSearchScroll(searchResponse.getScrollId())
                    .setScroll(TimeValue.timeValueMillis(1)).execute().actionGet();
            //当searchHits的数组为空的时候结束循环,至此数据全部读取完毕
        } while (searchResponse.getHits().getHits().length != 0);



    }

其余跟多查询姿势请看注释或者查看源代码或者文档,实例中的基本够用

 

点赞或者评论是我最大的动力,有问题欢迎留言或者联系q:1559810637  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章