对于ElasticSearch的介绍我就不多说了,能看到这篇博客想必你还是了解它是干嘛的,在使用java去连接这个nosql的时候我建议还是事先去学习一下它的语句语法,当你会这些语法的时候用java的api调用它才会更轻松明理,好了话不多说,直接贴代码:
maven
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.2.4</version>
</dependency>
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.4</version>
</dependency>
<!--热启动:每自修改后, 程序自动启动spring Application上下文。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.15</version>
</dependency>
<!--echarts-->
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>echarts</artifactId>
<version>4.1.0-release</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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);
}
其余跟多查询姿势请看注释或者查看源代码或者文档,实例中的基本够用