客户端打开。
代码如下:
User
package com.zhiyou.model;
public class User {
private Long id;
private String username;
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
'}';
}
}
ElasticTest
package com.zhiyou.test;
import com.alibaba.fastjson.JSON;
import com.zhiyou.model.User;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import java.net.InetAddress;
public class ElasticTest {
@Test
public void test01() throws Exception{
// 启动客户端连接
TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
// 关闭客户端连接
client.close();
}
// 添加索引
@Test
public void test02() throws Exception{
TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
User user=new User();
user.setId(3L);
user.setAge(77);
user.setUsername("王五");
IndexResponse response=client.prepareIndex("user","userinfo",user.getId()+"")
.setSource(JSON.toJSONString(user), XContentType.JSON)
.get();
System.out.println(response.toString());
//IndexResponse[index=user,type=userinfo,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
client.close();
}
//获取索引
@Test
public void test03() throws Exception{
TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
GetResponse response = client.prepareGet("user", "userinfo", "1").execute().actionGet();
String json=response.getSourceAsString();
System.out.println(json);
//{"age":30,"id":1,"username":"张三"}
User user = JSON.parseObject(json, User.class);
System.out.println(user);
//User{id=1, username='张三', age=30}
client.close();
}
//删除索引
@Test
public void test04() throws Exception{
TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
DeleteResponse response = client.prepareDelete("user", "userinfo", "1").execute().actionGet();
String result=response.getResult().name();
System.out.println(result);
//DELETED
client.close();
}
//根据条件搜索
@Test
public void test05() throws Exception{
TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
SearchResponse response=client.prepareSearch("user")
.setTypes("userinfo")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
//.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
.setFrom(0).setSize(20).setExplain(true)//这个分页,默认10条
.get();
System.out.println(response.getHits().getAt(1).getSourceAsString());
//{"age":30,"id":1,"username":"张三"}
// 数据不是根据id的顺序保存
client.close();
}
}