一、 依赖包
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
com.qiang
Elasticsearch
1.0.0-SNAPSHOT
junit
junit
4.11
test
org.elasticsearch
elasticsearch
6.4.3
org.elasticsearch.client
transport
6.4.3
org.apache.logging.log4j
log4j-core
2.10.0
org.apache.logging.log4j
log4j-api
2.10.0
二、 连接到Elasticsearch
public class TransportClientUtils {
public TransportClient transportClient() {
TransportClient transportClient = null;
try {
Settings settings = Settings.builder()
//自动发现新加入集群的机器
.put("client.transport.sniff", true)
.put("cluster.name", "elasticsearch").build();
transportClient = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(new InetSocketAddress("192.168.17.222", 9300)));
} catch (Exception e) {
e.printStackTrace();
}
return transportClient;
}
}
三、 操作Elasticsearch
数据

public class ElasticsearchBuilder {
TransportClient transportClient = new TransportClientUtils().transportClient();
@Test
public void test1() throws Exception {
//判断索引是否存在
boolean exists = transportClient.admin().indices().prepareExists("animals").execute().actionGet().isExists();
System.out.println(exists);
}
@Test
public void test2() throws Exception {
//添加
for (int i = 1; i <= 3; i++) {
long s = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
IndexResponse indexResponse = transportClient.prepareIndex("animals", "dogs")
.setSource(jsonBuilder()
.startObject()
.field("名字", "傻狗" + i)
.field("颜色", "白色" + i)
.field("时间", s)
.field("@timestamp", new Date())
.endObject())
.get();
Thread.sleep(100);
System.out.println(indexResponse);
}
}
@Test
public void test3() throws Exception {
//删除
DeleteResponse deleteResponse = transportClient.prepareDelete("animals", "dogs", "xVA65G4BCACHllmZ5ygu").get();
System.out.println(deleteResponse);
}
@Test
public void test4() throws Exception {
//修改
UpdateResponse updateResponse = transportClient.prepareUpdate("animals", "dogs", "xFA65G4BCACHllmZ5ygi").setDoc(jsonBuilder()
.startObject()
.field("颜色", "黑色9")
.endObject())
.get();
System.out.println(updateResponse);
}
@Test
public void test5() throws Exception {
//根据ID查询
GetResponse getResponse = transportClient.prepareGet("animals", "dogs", "xFA65G4BCACHllmZ5ygi").get();
String sourceAsString = getResponse.getSourceAsString();
System.out.println(sourceAsString);
}
@Test
public void test6() throws Exception {
//根据索引查询,默认10条,setSize()设置大小
String index = "animals";
QueryBuilder query = QueryBuilders.matchAllQuery();
SearchResponse response = transportClient.prepareSearch(index).setQuery(query).setSize(100).execute().actionGet();
for (SearchHit searchHit : response.getHits()) {
String sourceAsString = searchHit.getSourceAsString();
System.out.println(sourceAsString);
}
}
@Test
public void test7() throws Exception {
//根据类型查询,默认10条,setSize()设置大小
SearchResponse response = transportClient.prepareSearch("animals").setTypes("dogs").setSize(100).execute().actionGet();
for (SearchHit searchHit : response.getHits()) {
String sourceAsString = searchHit.getSourceAsString();
System.out.println(sourceAsString);
}
}
@Test
public void test8() throws Exception {
//单条件查询
QueryBuilder queryBuilder = QueryBuilders.termQuery("名字.keyword", "傻狗3");
SearchResponse response = transportClient.prepareSearch("animals").setTypes("dogs")
.setQuery(queryBuilder)
.setFrom(0).setSize(1000).setExplain(true)
.execute()
.actionGet();
//直接遍历
for (SearchHit searchHit : response.getHits()) {
String sourceAsString = searchHit.getSourceAsString();
System.out.println(sourceAsString);
}
//存入集合后再遍历
SearchHits hits = response.getHits();
List
四、 常见错误
错误
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
解决
org.apache.logging.log4j
log4j-core
2.10.0
org.apache.logging.log4j
log4j-api
2.10.0
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
作者(Author):小强崽
来源(Source):https://www.wuduoqiang.com/archives/SpringBoot整合Elasticsearch
协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
版权(Copyright):商业转载请联系作者获得授权,非商业转载请注明出处。 For commercial use, please contact the author for authorization. For non-commercial use, please indicate the source.