package cn.crxy.elasticsearch_11;
import static org.junit.Assert.*;
import java.util.HashMap;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.ImmutableList;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.crxy.elasticsearch_08.Person;
public class EsTest {
TransportClient transportClient;
@Before
public void test0() throws Exception {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch")//指定集群名称
.put("client.transport.sniff", true)//开启自动嗅探功能,可以把集群内部的所有节点都添加到客户端连接中
.build();
transportClient = new TransportClient(settings);
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
transportClient.addTransportAddresses(transportAddress);
}
/**
* 创建es客户端连接对象
* @throws Exception
*/
@Test
public void test1() throws Exception {
TransportClient transportClient = new TransportClient();
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
transportClient.addTransportAddress(transportAddress);
ImmutableList<DiscoveryNode> connectedNodes = transportClient.connectedNodes();
for (DiscoveryNode discoveryNode : connectedNodes) {
System.out.println(discoveryNode.getHostAddress());
}
}
String index = "crxy";
String type = "emp";
/**
* 如果集群的名称被修改了,需要显式指定集群的名称
* @throws Exception
*/
@Test
public void test2() throws Exception {
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build();
TransportClient transportClient = new TransportClient(settings);
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
transportClient.addTransportAddress(transportAddress);
//表示查询指定索引库中指定类类型下的指定id的数据
GetResponse getResponse = transportClient.prepareGet(index, type, "1").get();
}
/**
* 工作中建议使用这种
* @throws Exception
*/
@Test
public void test3() throws Exception {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch")//指定集群名称
.put("client.transport.sniff", true)//开启自动嗅探功能,可以把集群内部的所有节点都添加到客户端连接中
.build();
TransportClient transportClient = new TransportClient(settings);
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
//TransportAddress transportAddress1 = new InetSocketTransportAddress("192.168.1.171", 9300);
transportClient.addTransportAddresses(transportAddress);
ImmutableList<DiscoveryNode> connectedNodes = transportClient.connectedNodes();
for (DiscoveryNode discoveryNode : connectedNodes) {
System.out.println(discoveryNode.getHostAddress());
}
}
/**
* index -1 json字符串
* @throws Exception
*/
@Test
public void test4() throws Exception {
String jsonStr = "{\"name\":\"hehe\",\"age\":18}";
IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//指定索引库,类型,id
.setSource(jsonStr)//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}
/**
* index -2 hash类型
* @throws Exception
*/
@Test
public void test5() throws Exception {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("name", "zs");
hashMap.put("age", 20);
IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3")//指定索引库,类型,id
.setSource(hashMap)//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}
/**
* 工作中最常用
* index -3 bean对象
* @throws Exception
*/
@Test
public void test6() throws Exception {
Person person = new Person();
person.setName("haha");
person.setAge(12);
ObjectMapper objectMapper = new ObjectMapper();
IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4")//指定索引库,类型,id
.setSource(objectMapper.writeValueAsString(person))//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}
/**
* index -4 es工具类
* @throws Exception
*/
@Test
public void test7() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()//{
.field("name", "ww")//"name":"ww"
.field("age", 30)//"age":30
.endObject();//}
ObjectMapper objectMapper = new ObjectMapper();
IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4")//指定索引库,类型,id
.setSource(builder)//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}
/**
* 查询
* @throws Exception
*/
@Test
public void test8() throws Exception {
GetResponse getResponse = transportClient.prepareGet(index, type, "1").get();
System.out.println(getResponse.getSourceAsString());
}
/**
* 更新
* @throws Exception
*/
@Test
public void test9() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("age", 11).endObject();
UpdateResponse updateResponse = transportClient.prepareUpdate(index, type, "100").setDoc(builder).get();
System.out.println(updateResponse.getVersion());
}
/**
* 删除
* @throws Exception
*/
@Test
public void test10() throws Exception {
DeleteResponse deleteResponse = transportClient.prepareDelete(index, type, "1").get();
}
/**
* 求count值
* @throws Exception
*/
@Test
public void test11() throws Exception {
CountResponse countResponse = transportClient.prepareCount(index).get();
System.out.println(countResponse.getCount());
}
}