package com.jt;
import java.net.InetAddress;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EsTest {
public static TransportClient getClient() throws Exception{
//java TransportClient client连接es
//Settings.EMPTY表示连接过程使用默认配置
//例如集群名称默认是elasticsearch
TransportClient client=new PreBuiltTransportClient(Settings.EMPTY).
addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("10.9.21.220"),9300));
return client;
}
/*
* 客户连接es,执行一个方法判断连接是否成功
*/
@Test
public void test01() throws Exception{
TransportClient client=EsTest.getClient();
//利用client的方法,获取一个索引jtdb tb_item 下id 为830972的doc
//参数问题:
//第一个参数:代表索引
//第二个参数:代表类型,也就是数据库的表名称
//第三个参数:代表在表中的id
GetResponse response=client.prepareGet("jtdb_item","tb_item","536563").get();
//从response,获取doc数据
String result=response.getSourceAsString();
System.out.println(result);
}
/*
* 判断索引是否存在
*/
@Test
public void test02() throws Exception{
TransportClient client=EsTest.getClient();
//获取管理对象
IndicesAdminClient indicesAdminClient=client.admin().indices();
//获取索引的doc
IndicesExistsResponse response1=indicesAdminClient.prepareExists("index01").get();
IndicesExistsResponse response2=indicesAdminClient.prepareExists("jtdb_item").get();
//判断索引是否存在
System.out.println(response1.isExists());
System.out.println(response2.isExists());
}
/*
* 创建索引
*/
@Test
public void test03() throws Exception{
TransportClient client=EsTest.getClient();
//获取管理对象
IndicesAdminClient indicesAdminClient=client.admin().indices();
CreateIndexResponse response=indicesAdminClient.prepareCreate("index03").get();
System.out.println(response);
}
/*
* 删除索引
*/
@Test
public void test04() throws Exception{
TransportClient client=EsTest.getClient();
//获取管理对象
IndicesAdminClient indicesAdminClient=client.admin().indices();
DeleteIndexResponse response=indicesAdminClient.prepareDelete("index03").get();
System.out.println(response);//可以不打印
}
/*
* 新建文档
* 文档是对象,底层http协议如何把对象转换为json字符串
* 引入一个第三方工具jacksonJson:将对象转化为json
*/
private static final ObjectMapper mapper=new ObjectMapper();
@Test
public void test05() throws Exception{
Item item=new Item();
item.setId(10L);
item.setBarcode("sdfas");
item.setTitle("三星大地利尔");
item.setSellPoint("灭口神奇");
String docStr=new ObjectMapper().writeValueAsString(item);
//client调用添加方法将字符串通过http发送到es存储
//如果不拿结果,最后的方法可以不加
IndexResponse reponse=EsTest.getClient().prepareIndex("index01","item","1").
setSource(docStr).execute().actionGet();
System.out.println(reponse);
}
/*
* machQuery查询数据
*/
@Test
public void test06() throws Exception{
//连接对象
TransportClient client=EsTest.getClient();
//构造查询条件对象
//org.elasticsearch.index.query
QueryBuilder query=QueryBuilders.matchQuery("title","苹果").
operator(Operator.AND);
//将对象封装数据
//获取返回结构,d打印部分字段
//searche指向一个索引文件名
SearchResponse reponse=client.prepareSearch("jtdb_item").setQuery(query).setSize(10).get();
//reponse里包含着50条查询结果
SearchHits hits=reponse.getHits();//查询条数
for(SearchHit hit:hits){
System.out.println("文档id"+hit.getId());
System.out.println("商品id"+hit.getSource().get("id"));
System.out.println("商品标题"+hit.getSource().get("title"));
}
}
}