ELASTICSEARCH的java客户端和springboot整合
目录
ELASTICSEARCH的java客户端和springboot整合
1.通过client对象连接集群,获取索引的操作权限,并且执行新增索引删除索引
一.java客户端代码
1.1添加依赖
ES的代码操作,千万不能lucene在一起,会有jar包,class类的冲突
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.5.2</version>
</dependency>
<!-- es的java客户端 TransportClient -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.2</version>
</dependency>
1.2 Java代码客户端测试
1.TransportTest测试类
transportclient连接负载均衡器节点
代码9300,地址9200
//创建一个连接对象,连接es集群
private TransportClient client;
@Before //后面直接使用
public void conn() throws Exception{
//新建一个client对象,setting对象(配置集群名称)
client=
new PreBuiltTransportClient(Settings.EMPTY);
//添加负载均衡器的结点信息,如果是集群,可以多次添加多个节点信息
client.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("10.42.60.249"), 9300)); //直接连接是9200
client.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("10.42.65.114"), 9300));
client.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("10.42.65.135"), 9300));
}
2.新增数据
1.通过client对象连接集群,获取索引的操作权限,并且执行新增索引删除索引
@Test
public void indexTest01(){
//获取索引的管理对象indexAdmin
IndicesAdminClient indexAdmin = client.admin().indices();
//prepare方法
CreateIndexRequestBuilder request
= indexAdmin.prepareCreate("exindex01");//提前
CreateIndexResponse response=request.get();
System.out.println(response.isAcknowledged());
System.out.println(response.remoteAddress().toString());
}
2.测试结果
![](https://i-blog.csdnimg.cn/blog_migrate/4f2bab40cb983ce518b52f09d8e32b23.png)
3.删除索引
@Test
public void indexTest01(){
//获取索引的管理对象indexAdmin
IndicesAdminClient indexAdmin = client.admin().indices();
DeleteIndexRequestBuilder request = indexAdmin.prepareDelete("exindex01");
//执行方法需要从代码发送这个请求并且接受响应
DeleteIndexResponse response = request.get();
System.out.println(response.isAcknowledged());
System.out.println(response.remoteAddress().toString());
}
1.测试结果
4.查看索引是否存在
public void indexTest01(){
//获取索引的管理对象indexAdmin
IndicesAdminClient indexAdmin = client.admin().indices();
IndicesExistsResponse response = indexAdmin.prepareExists("index06").get();
System.out.println(response.isExists());//true false
}
1.测试结果
5.新增文档
1.操作索引新增文档数据到索引中
@Test
public void docTest01() throws JsonProcessingException{
//{"id":"1","age":"dafa"}
IndexRequestBuilder docManage =
client.prepareIndex("index06", "article","1");//url:localhost/index06/article/1
//将数据对象先转化成json字符串,通过String类型的数据添加到索引中
Article at=new Article();
at.setId(100);
at.setTitle("java编程思想");
at.setContent("这就是一个工具书");
ObjectMapper mapper=new ObjectMapper();
String dataJson=mapper.writeValueAsString(at);//Java对象转换json字符串
System.out.println(dataJson);
//{"id":"","title":"","content":""}
IndexResponse response = docManage.setSource(dataJson).get();
response.getIndex();
response.getVersion();
response.getId();
}
2.测试结果
6.获取文档
@Test
public void docTest02(){
GetResponse response = client.prepareGet("index06", "article", "1").get();
//response封装的是获取doc返回的所有字符串内容
response.getIndex();
response.getId();
response.getType();
response.getVersion();
Map<String, Object> map = response.getSource();//拿到了一个封装document中数据的map
String dataJson=response.getSourceAsString();//把source全部拿到
System.out.println(map.get("title"));
System.out.println(dataJson);
}
2.测试结果
7.删除文档
@Test
public void docTest02(){
DeleteResponse response = client.prepareDelete("index06", "article", "1").get();
//response封装的是获取doc返回的所有字符串内容
System.out.println(response);
}
2.测试结果
8.搜索
1.termQuery(Query实现类)
@Test
public void searchTest01(){
//query实现类
//org.elasticsearch.index.query.QueryBuilders
//QueryBuilders.fuzzyQuery(name, value);//获取一个模糊查询的query
//QueryBuilders.wildcardQuery(name, query)//通配符查询
TermQueryBuilder query1 = QueryBuilders.termQuery("title", "java");
TermQueryBuilder query2 = QueryBuilders.termQuery("title", "编程");
//MatchQueryBuilder query1=QueryBuilders.matchQuery("title", "java编程思想");//进行默认的分词器解析
//java 编,程,思,想
//搜索api获取搜索结果
SearchResponse response1 = client.prepareSearch("index06")
.setFrom(0).setSize(10).setQuery(query1).get(); //查询前十条 get 请求发送过去
long total = response1.getHits().totalHits;
System.out.println(total);
SearchHit[] hits = response1.getHits().getHits();
for (SearchHit hit : hits) {
String dataJson = hit.getSourceAsString();
System.out.println(dataJson);
}
}
2.测试结果
9.封装一个JSON字符串
代码设置某个索引中某个类的mapping
@Test
public void mappingTest() throws Exception{
//构造一个可以封装{"properties":{"title":{"type":"text"}}}
XContentBuilder mappingBuilder=XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("bookName").field("type", "text")
.endObject()
.startObject("neirong").field("type", "text")
.endObject()
.startObject("bookid").field("type","integer")
.endObject()
.endObject()
.endObject();
//绑定mappingBuidler到index 到type
PutMappingRequest request = Requests.putMappingRequest("index06")
.type("book").source(mappingBuilder);
client.admin().indices().putMapping(request).get();
}
2.测试结果
{
"index06": {
"mappings": {
"article": {
"properties": {
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "long"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"book": {
"properties": {
"bookName": {
"type": "text"
},
"bookid": {
"type": "integer"
},
"neirong": {
"type": "text"
}
}
}
}
}
}
测试代码结束,