ElasticsearchTest1.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //import java.net.InetAddress;
  2. //import java.net.UnknownHostException;
  3. //import java.util.Date;
  4. //import java.util.HashMap;
  5. //import java.util.Map;
  6. //import java.util.concurrent.ExecutionException;
  7. //
  8. //import org.elasticsearch.action.get.GetResponse;
  9. //import org.elasticsearch.action.index.IndexResponse;
  10. //import org.elasticsearch.action.search.SearchResponse;
  11. //import org.elasticsearch.client.transport.TransportClient;
  12. //import org.elasticsearch.common.settings.Settings;
  13. //import org.elasticsearch.common.transport.InetSocketTransportAddress;
  14. //import org.elasticsearch.index.query.QueryBuilder;
  15. //import org.elasticsearch.index.query.QueryBuilders;
  16. //import org.elasticsearch.search.SearchHit;
  17. //import org.elasticsearch.search.SearchHits;
  18. //import org.elasticsearch.search.sort.SortOrder;
  19. //import org.elasticsearch.transport.client.PreBuiltTransportClient;
  20. //import org.junit.Before;
  21. //import org.junit.Test;
  22. //
  23. //public class ElasticsearchTest1 {
  24. // TransportClient client;
  25. // @Before
  26. // @SuppressWarnings({ "unchecked" })
  27. // public void before() throws UnknownHostException, InterruptedException, ExecutionException {
  28. // Settings esSettings = Settings.builder()
  29. // .put("cluster.name", "my-application") //设置ES实例的名称
  30. // .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
  31. // .build();
  32. // client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。
  33. // //此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
  34. // client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.124.237"), 9300));
  35. // }
  36. //
  37. // @Test
  38. // public void index() throws Exception {
  39. // Map<String,Object> infoMap = new HashMap<String, Object>();
  40. // infoMap.put("name", "广告信息11");
  41. // infoMap.put("title", "我的广告22");
  42. // infoMap.put("createTime", new Date());
  43. // infoMap.put("count", 1022);
  44. // infoMap.put("count22", 1022222);
  45. // IndexResponse indexResponse = client.prepareIndex("chanpinbiao", "info","100").setSource(infoMap).execute().actionGet();
  46. // System.out.println("id:"+indexResponse.getId());
  47. // }
  48. //
  49. // @Test
  50. // public void get() throws Exception {
  51. // GetResponse response = client.prepareGet("test", "info", "100")
  52. // .execute().actionGet();
  53. // System.out.println("response.getId():"+response.getId());
  54. // System.out.println("response.getSourceAsString():"+response.getSourceAsString());
  55. // }
  56. //
  57. // @Test
  58. // public void query() throws Exception {
  59. // //term查询
  60. //// QueryBuilder queryBuilder = QueryBuilders.termQuery("age", 50) ;
  61. // //range查询
  62. // QueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gt(50);
  63. // SearchResponse searchResponse = client.prepareSearch("sxq")
  64. // .setTypes("user")
  65. // .setQuery(rangeQueryBuilder)
  66. // .addSort("age", SortOrder.DESC)
  67. // .setSize(20)
  68. // .execute()
  69. // .actionGet();
  70. // SearchHits hits = searchResponse.getHits();
  71. // System.out.println("查到记录数:"+hits.getTotalHits());
  72. // SearchHit[] searchHists = hits.getHits();
  73. // if(searchHists.length>0){
  74. // for(SearchHit hit:searchHists){
  75. // String name = (String) hit.getSource().get("name");
  76. // Integer age = (Integer)hit.getSource().get("age");
  77. // System.out.format("name:%s ,age :%d \n",name ,age);
  78. // }
  79. // }
  80. // }
  81. //
  82. //}