6

我无法更改映射。任何人都可以帮我找到我的代码中的错误吗?
我根据几个教程找到了这种更改映射的标准方法。但是当我尝试调用映射结构时,在手动映射创建后只会出现一个空白映射结构。
但是在插入一些数据之后会出现映射规范,因为 ES 使用的是默认的。要更具体,请参见下面的代码。

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();
}
}

我只想将 ATTR1 字段的属性更改为已分析以确保快速查询。我做错了什么?我还尝试在创建索引后创建映射,但它会产生相同的影响。

4

1 回答 1

5

好的,我自己找到了答案。在类型级别上,我必须用类型名称包装“属性”。例如:

“type1”:{“属性”:{.....}}

请参阅以下代码:

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;
}

它为属性 ATTR1 - ATTR4 创建映射。现在可以为 Example 动态定义不同属性列表的映射。希望它可以帮助别人。

于 2013-06-25T11:17:17.167 回答