elasticsearch java API ------索引数据

创建索引的第一步工作,是将你要创建索引的对象转化为Json字符串。

        生成Json的方法很多,最直接的是手写,将你的实体转化为Json:

 

[java]  view plain copy
 
 
  1. String json = "{" +  
  2.         ""user":"kimchy"," +  
  3.         ""postDate":"2013-01-30"," +  
  4.         ""message":"trying out Elastic Search"," +  
  5.     "}";  


        第二种,是使用Map方式:

 

 

[java]  view plain copy
 
 
  1. Map<String, Object> json = new HashMap<String, Object>();  
  2. json.put("user","kimchy");  
  3. json.put("postDate",new Date());  
  4. json.put("message","trying out Elastic Search");  


        第三种,是使用Elasticsearch提供的帮助类

[java]  view plain copy
 
 
  1. import static org.elasticsearch.common.xcontent.XContentFactory.*;  
  2.   
  3. XContentBuilder builder = jsonBuilder()  
  4.     .startObject()  
  5.         .field("user""kimchy")  
  6.         .field("postDate"new Date())  
  7.         .field("message""trying out Elastic Search")  
  8.     .endObject();  
  9. String json = builder.string();  


        第四种是使用jackson技术,你可以导入jackson的jar包,如果是maven管理的项目,则直接在pom.xml中添加:

 

 

[java]  view plain copy
 
 
  1. <dependency>  
  2.     <groupId>com.fasterxml.jackson.core</groupId>  
  3.     <artifactId>jackson-databind</artifactId>  
  4.     <version>2.1.3</version>  
  5. </dependency>  


        然后:

 

 

[java]  view plain copy
 
 
  1. import com.fasterxml.jackson.databind.*;  
  2.   
  3. // instance a json mapper  
  4. ObjectMapper mapper = new ObjectMapper(); // create once, reuse  
  5.   
  6. // generate json  
  7. String json = mapper.writeValueAsString(yourbeaninstance);  


        接下来便可以创建索引了:

 

 

[java]  view plain copy
 
 
  1. IndexResponse response = client.prepareIndex("twitter""tweet")  
  2.         .setSource(json)  
  3.         .execute()  
  4.         .actionGet();  

        client.prepareIndex的参数可以是0个(其后必须使用index(String)和type(String)方法),两个(client.prepareIndex(index,type))和三个(client.prepareIndex(index,type,id)),其中,index类似于数据库名,type类似于表名,而id则是每条记录的惟一编码,通常情况下,我们会把实体的惟一编码作为ES的ID,当然,不给的时候,由ES自动生成。

 

        response是Elasticsearch创建完成索引后,回馈给你的实体,你可以从这个实体中获得一些有价值的信息:

 

[java]  view plain copy
 
 
  1. // 索引名称  
  2. String _index = response.index();  
  3. // type名称  
  4. String _type = response.type();  
  5. // 文档ID  
  6. String _id = response.id();  
  7. // 索引版本  
  8. long _version = response.version();  

 

TransportClient client = null;



public ESClientTest() {
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true).build();
client = new TransportClient(settings);
}


public void connect() {
client.addTransportAddress(new InetSocketTransportAddress("localhost", 9300)); 
}

public void close(){
client.close();

}

public void index(){
try {
XContentBuilder doc = XContentFactory.jsonBuilder()
         .startObject()     
             .field("title", "this")
             .field("description", "descript") 
             .field("price", 100)
             .field("onSale", true)
             .field("type", 1)
             .field("createDate", new Date())  
        .endObject();
  client.prepareIndex("productindex","productindex","1").setSource(doc).execute().actionGet();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    
}

public static void main(String[] args) {
ESClientTest es = new ESClientTest();
es.index();
es.close();
}

其中productIndex为索引库名,一个es集群中可以有多个索引库。productType为索引类型,是用来区分同索引库下不同类型的数据的,一个索引库下可以有多个索引类型。

 

 

elasticsearch java API --------删除索引数据

public void delbyquery(){
QueryBuilder query = QueryBuilders.fieldQuery("type", "1");
     client.prepareDeleteByQuery("productindex").setQuery(query).execute().actionGet();


}

public void delbyid(){
DeleteResponse response = client.prepareDelete("productindex", "productindex", "1") 
       .execute() 
       .actionGet();

}

 

猜你喜欢

转载自aoyouzi.iteye.com/blog/2125360