| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- //import java.net.InetAddress;
- //import java.net.UnknownHostException;
- //import java.util.Date;
- //import java.util.HashMap;
- //import java.util.Map;
- //import java.util.concurrent.ExecutionException;
- //
- //import org.elasticsearch.action.get.GetResponse;
- //import org.elasticsearch.action.index.IndexResponse;
- //import org.elasticsearch.action.search.SearchResponse;
- //import org.elasticsearch.client.transport.TransportClient;
- //import org.elasticsearch.common.settings.Settings;
- //import org.elasticsearch.common.transport.InetSocketTransportAddress;
- //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.search.sort.SortOrder;
- //import org.elasticsearch.transport.client.PreBuiltTransportClient;
- //import org.junit.Before;
- //import org.junit.Test;
- //
- //public class ElasticsearchTest1 {
- // TransportClient client;
- // @Before
- // @SuppressWarnings({ "unchecked" })
- // public void before() throws UnknownHostException, InterruptedException, ExecutionException {
- // Settings esSettings = Settings.builder()
- // .put("cluster.name", "my-application") //设置ES实例的名称
- // .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
- // .build();
- // client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。
- // //此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
- // client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.124.237"), 9300));
- // }
- //
- // @Test
- // public void index() throws Exception {
- // Map<String,Object> infoMap = new HashMap<String, Object>();
- // infoMap.put("name", "广告信息11");
- // infoMap.put("title", "我的广告22");
- // infoMap.put("createTime", new Date());
- // infoMap.put("count", 1022);
- // infoMap.put("count22", 1022222);
- // IndexResponse indexResponse = client.prepareIndex("chanpinbiao", "info","100").setSource(infoMap).execute().actionGet();
- // System.out.println("id:"+indexResponse.getId());
- // }
- //
- // @Test
- // public void get() throws Exception {
- // GetResponse response = client.prepareGet("test", "info", "100")
- // .execute().actionGet();
- // System.out.println("response.getId():"+response.getId());
- // System.out.println("response.getSourceAsString():"+response.getSourceAsString());
- // }
- //
- // @Test
- // public void query() throws Exception {
- // //term查询
- //// QueryBuilder queryBuilder = QueryBuilders.termQuery("age", 50) ;
- // //range查询
- // QueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gt(50);
- // SearchResponse searchResponse = client.prepareSearch("sxq")
- // .setTypes("user")
- // .setQuery(rangeQueryBuilder)
- // .addSort("age", SortOrder.DESC)
- // .setSize(20)
- // .execute()
- // .actionGet();
- // SearchHits hits = searchResponse.getHits();
- // System.out.println("查到记录数:"+hits.getTotalHits());
- // SearchHit[] searchHists = hits.getHits();
- // if(searchHists.length>0){
- // for(SearchHit hit:searchHists){
- // String name = (String) hit.getSource().get("name");
- // Integer age = (Integer)hit.getSource().get("age");
- // System.out.format("name:%s ,age :%d \n",name ,age);
- // }
- // }
- // }
- //
- //}
|