elasticsearch java API ------定义索引字段属性

elasticsearch java API ------定义索引字段属性

 

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 putMapping(){
try {

                      //先创建空索引库
client.admin().indices().prepareCreate("productindex").execute().actionGet();  
XContentBuilder mapping = XContentFactory.jsonBuilder()
        .startObject()
          .startObject("productindex")
          .startObject("properties")       
            .startObject("title").field("type", "string").field("store", "yes").endObject()  
            .startObject("description").field("type", "string").field("index", "not_analyzed").endObject()
            .startObject("price").field("type", "double").endObject()
            .startObject("onSale").field("type", "boolean").endObject()
            .startObject("type").field("type", "integer").endObject()
            .startObject("createDate").field("type", "date").endObject()               
          .endObject()
         .endObject()
       .endObject();
 PutMappingRequest mappingRequest = Requests.putMappingRequest("productindex").type("productindex").source(mapping);
      client.admin().indices().putMapping(mappingRequest).actionGet();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    
}

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

 

你可能感兴趣的:(java,api,elasticsearch,定义索引字段属性)