2013-06-25 21 views
6

Tôi không thể thay đổi ánh xạ. Ai có thể giúp tôi để tìm lỗi trong mã của tôi?
Tôi đã tìm thấy cách tiêu chuẩn này để thay đổi ánh xạ theo một số hướng dẫn. Nhưng khi tôi cố gắng gọi cấu trúc ánh xạ, chỉ xuất hiện một cấu trúc ánh xạ trống sau khi tạo bản đồ manuall.
Nhưng sau khi chèn một số dữ liệu, sẽ xuất hiện đặc tả bản đồ vì ES đang sử dụng khóa mặc định. Để cụ thể hơn, hãy xem mã bên dưới.Elasticsearch: Thêm ánh xạ thủ công bằng cách sử dụng Java

public class ElasticTest { 
private String dbname = "ElasticSearch"; 
private String index = "indextest"; 
private String type = "table"; 
private Client client = null; 
private Node node = null; 

public ElasticTest(){ 
    this.node = nodeBuilder().local(true).node(); 
    this.client = node.client(); 

    if(isIndexExist(index)){ 
     deleteIndex(this.client, index); 
     createIndex(index); 
    } 
    else{ 
     createIndex(index); 
    } 

    System.out.println("mapping structure before data insertion"); 
    getMappings(); 
    System.out.println("----------------------------------------"); 
    createData(); 
    System.out.println("mapping structure after data insertion"); 
    getMappings(); 



} 

public void getMappings() { 
    ClusterState clusterState = client.admin().cluster().prepareState() 
      .setFilterIndices(index).execute().actionGet().getState(); 
    IndexMetaData inMetaData = clusterState.getMetaData().index(index); 
    MappingMetaData metad = inMetaData.mapping(type); 

    if (metad != null) { 
     try { 
      String structure = metad.getSourceAsMap().toString(); 
      System.out.println(structure); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

private void createIndex(String index) { 
    XContentBuilder typemapping = buildJsonMappings(); 
    String mappingstring = null; 
    try { 
     mappingstring = buildJsonMappings().string(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 

    client.admin().indices().create(new CreateIndexRequest(index) 
        .mapping(type, typemapping)).actionGet(); 

    //try put mapping after index creation 
    /* 
    * PutMappingResponse response = null; try { response = 
    * client.admin().indices() .preparePutMapping(index) .setType(type) 
    * .setSource(typemapping.string()) .execute().actionGet(); } catch 
    * (ElasticSearchException e) { e.printStackTrace(); } catch 
    * (IOException e) { e.printStackTrace(); } 
    */ 

} 

private void deleteIndex(Client client, String index) { 
    try { 
     DeleteIndexResponse delete = client.admin().indices() 
       .delete(new DeleteIndexRequest(index)).actionGet(); 
     if (!delete.isAcknowledged()) { 
     } else { 
     } 
    } catch (Exception e) { 
    } 
} 

private XContentBuilder buildJsonMappings(){ 
    XContentBuilder builder = null; 
    try { 
     builder = XContentFactory.jsonBuilder(); 
     builder.startObject() 
     .startObject("properties") 
      .startObject("ATTR1") 
       .field("type", "string") 
       .field("store", "yes") 
       .field("index", "analyzed") 
      .endObject() 
      .endObject() 
     .endObject();   
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return builder; 
} 

private boolean isIndexExist(String index) { 
    ActionFuture<IndicesExistsResponse> exists = client.admin().indices() 
      .exists(new IndicesExistsRequest(index)); 
    IndicesExistsResponse actionGet = exists.actionGet(); 

    return actionGet.isExists(); 
} 

private void createData(){ 
    System.out.println("Data creation"); 
    IndexResponse response=null; 
    for (int i=0;i<10;i++){ 
     Map<String, Object> json = new HashMap<String, Object>(); 
     json.put("ATTR1", "new value" + i); 
     response = this.client.prepareIndex(index, type) 
       .setSource(json) 
       .setOperationThreaded(false) 
       .execute() 
       .actionGet(); 
    } 
    String _index = response.getIndex(); 
    String _type = response.getType(); 
    long _version = response.getVersion(); 
    System.out.println("Index : "+_index+" Type : "+_type+" Version : "+_version); 
    System.out.println("----------------------------------"); 
} 

public static void main(String[] args) 
{ 
    new ElasticTest(); 
} 
} 

Tôi chỉ muốn thay đổi thuộc tính của trường ATTR1 để phân tích để đảm bảo truy vấn nhanh. Tôi đang làm gì sai? Tôi cũng đã cố gắng để tạo ra các bản đồ sau khi tạo chỉ mục nhưng nó dẫn đến cùng một ảnh hưởng.

Trả lời

5

Ok tôi đã tự tìm được câu trả lời. Ở cấp độ loại tôi đã bao bọc "thuộc tính" với tên kiểu. Ví dụ:

"type1": { "tài sản": { ..... }}

Xem đoạn mã sau:

private XContentBuilder getMappingsByJson(){ 
    XContentBuilder builder = null; 
    try { 
     builder = XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties"); 
     for(int i = 1; i<5; i++){ 
      builder.startObject("ATTR" + i) 
        .field("type", "integer") 
        .field("store", "yes") 
        .field("index", "analyzed") 
        .endObject(); 
      } 
      builder.endObject().endObject().endObject(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    return builder; 
} 

Nó tạo ra các ánh xạ cho các thuộc tính ATTR1 - ATTR4. Bây giờ có thể xác định ánh xạ cho Ví dụ một danh sách các thuộc tính khác nhau động. Hy vọng nó sẽ giúp người khác.

Các vấn đề liên quan