使用Java操作ElasticSearch1.7程序

    ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

   最近学习了一下ElasticSearch的Java API使用,简易代码如下:

 

[java] view plain copy
 
  1. package com.learn.test;  
  2.   
  3. import org.elasticsearch.client.Client;  
  4. import org.elasticsearch.client.transport.TransportClient;  
  5. import org.elasticsearch.common.transport.InetSocketTransportAddress;  
  6. import org.elasticsearch.common.settings.ImmutableSettings;  
  7. import org.elasticsearch.common.settings.Settings;  
  8. import org.elasticsearch.action.index.IndexResponse;  
  9.   
  10. import java.io.IOException;  
  11.   
  12. import org.codehaus.jackson.JsonProcessingException;  
  13. import org.codehaus.jackson.map.ObjectMapper;  
  14.   
  15. public class TestElastic   
  16. {  
  17.     public static void main(String[] args)   
  18.     {     
  19.         UserModel user = new UserModel();  
  20.         user.setId(1);  
  21.         user.setName("李四");  
  22.         user.setAge("101");  
  23.         user.setSex("1231");  
  24.         user.setTel("Tel");  
  25.         String jsondata = toJson(user);  
  26.         System.out.println("封装的json:"+jsondata);  
  27.           
  28.         Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "tang-elasticsearch").build();   
  29.         Client client = new TransportClient(settings).addTransportAddress(  
[java] view plain copy
 
  1. new<span style="font-family: Arial, Helvetica, sans-serif;"> InetSocketTransportAddress("192.168.15.1", 9300));</span>  
[java] view plain copy
 
  1. IndexResponse res = client.prepareIndex().setIndex("school").setType("class").setId("10").setSource(  
[java] view plain copy
 
  1.                                         jsondata).execute().actionGet();  
  2.         System.out.println("索引创建成功,版本号:"+res.getVersion());  
  3.           
  4.           
  5.         client.close();  
  6.     }  
  7.       
  8.     public static String toJson(Object o)   
  9.     {  
  10.         try   
  11.         {  
  12.             ObjectMapper objectMapper = new ObjectMapper();  
  13.             return objectMapper.writeValueAsString(o);  
  14.         }   
  15.         catch (JsonProcessingException e)   
  16.         {  
  17.             e.printStackTrace();  
  18.         }   
  19.         catch (IOException e)   
  20.         {  
  21.             e.printStackTrace();  
  22.         }  
  23.           
  24.         return "";  
  25.     }  
  26.   
  27. }  
  28.   
  29. class UserModel   
  30. {  
  31.     private int id;  
  32.     private String name;  
  33.     private String age;  
  34.     private String sex;  
  35.     private String tel;  
  36.   
  37.     public int getId()   
  38.     {  
  39.         return id;  
  40.     }  
  41.   
  42.     public void setId(int id)   
  43.     {  
  44.         this.id = id;  
  45.     }  
  46.   
  47.     public String getName()   
  48.     {  
  49.         return name;  
  50.     }  
  51.   
  52.     public void setName(String name)   
  53.     {  
  54.         this.name = name;  
  55.     }  
  56.   
  57.     public String getAge()   
  58.     {  
  59.         return age;  
  60.     }  
  61.   
  62.     public void setAge(String age)   
  63.     {  
  64.         this.age = age;  
  65.     }  
  66.   
  67.     public String getSex()   
  68.     {  
  69.         return sex;  
  70.     }  
  71.   
  72.     public void setSex(String sex)   
  73.     {  
  74.         this.sex = sex;  
  75.     }  
  76.   
  77.     public String getTel()   
  78.     {  
  79.         return tel;  
  80.     }  
  81.   
  82.     public void setTel(String tel)   
  83.     {  
  84.         this.tel = tel;  
  85.     }  
  86. }  
posted @ 2017-11-28 13:10  Histring  阅读(146)  评论(0)    收藏  举报