5ol.cc 发表于 2017-5-20 13:36:17

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

创建索引的第一步工作,是将你要创建索引的对象转化为Json字符串。
        生成Json的方法很多,最直接的是手写,将你的实体转化为Json:
 
 view plaincopy 
 



[*]String json = "{" +  
[*]        ""user":"kimchy"," +  
[*]        ""postDate":"2013-01-30"," +  
[*]        ""message":"trying out Elastic Search"," +  
[*]    "}";  

  
        第二种,是使用Map方式:
 
 
 view plaincopy 
 



[*]Map<String, Object> json = new HashMap<String, Object>();  
[*]json.put("user","kimchy");  
[*]json.put("postDate",new Date());  
[*]json.put("message","trying out Elastic Search");  

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

 view plaincopy 
 



[*]import static org.elasticsearch.common.xcontent.XContentFactory.*;  
[*]  
[*]XContentBuilder builder = jsonBuilder()  
[*]    .startObject()  
[*]        .field("user", "kimchy")  
[*]        .field("postDate", new Date())  
[*]        .field("message", "trying out Elastic Search")  
[*]    .endObject();  
[*]String json = builder.string();  

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



[*]<dependency>  
[*]    <groupId>com.fasterxml.jackson.core</groupId>  
[*]    <artifactId>jackson-databind</artifactId>  
[*]    <version>2.1.3</version>  
[*]</dependency>  

  
        然后:
 
 
 view plaincopy 
 



[*]import com.fasterxml.jackson.databind.*;  
[*]  
[*]// instance a json mapper  
[*]ObjectMapper mapper = new ObjectMapper(); // create once, reuse  
[*]  
[*]// generate json  
[*]String json = mapper.writeValueAsString(yourbeaninstance);  

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



[*]IndexResponse response = client.prepareIndex("twitter", "tweet")  
[*]        .setSource(json)  
[*]        .execute()  
[*]        .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创建完成索引后,回馈给你的实体,你可以从这个实体中获得一些有价值的信息:
 
 view plaincopy 
 



[*]// 索引名称  
[*]String _index = response.index();  
[*]// type名称  
[*]String _type = response.type();  
[*]// 文档ID  
[*]String _id = response.id();  
[*]// 索引版本  
[*]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();
}
 
页: [1]
查看完整版本: elasticsearch java API ------索引数据