ElasicSearch(4) 与java结合

1.pom

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

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

 2. 查询数据

 // 从es中查询数据
    @Test
    public void test1() throws UnknownHostException {
        //指定es集群
        Settings Settings = org.elasticsearch.common.settings.Settings.builder().put("cluster.name","my-application").build();
        //创建访问es服务器的客户端
        TransportAddress address = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
        TransportClient client = new PreBuiltTransportClient(Settings)
                .addTransportAddress(address);
        //数据查询
        // index/type/id
        GetResponse response = client.prepareGet(null,"_search",null).execute().actionGet();
        System.out.println(response.getSourceAsString());

        client.close();
    }

猜你喜欢

转载自www.cnblogs.com/mm163/p/10735161.html