ElasticSearch在Java中简单应用
在Java应用中实现查询
在Java应用中实现查询文档
配置
代码
import org.elasticsearch.action.get.GetResponse;
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.junit.Test;
import java.net.InetAddress;
public class ElasticTest {
//从es中查询数据
@Test
public void test1() throws Exception {
//指定ES集群
Settings settings = Settings.builder().put("cluster.name","my-application").build();
//创建访问es服务器的客户端
TransportClient transportClient = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.200"),9300));
//数据查询
GetResponse getResponse = transportClient.prepareGet("lib3","user","1").execute().actionGet();
System.out.println(getResponse.getSourceAsString());
transportClient.close();
}
}
相关文章