Elasticsearch: Adding manual mapping using Java

后端 未结 1 700
执念已碎
执念已碎 2021-02-06 12:11

I cant change the mapping. Can anybody help me to find the bug in my code?
I have found this standard way to change the mapping according to several tutorials. But when i\'

相关标签:
1条回答
  • 2021-02-06 12:54

    Ok i found the answer by my own. On the type level i had to wrap the "properties" with the type name. E.g:

    "type1" : { "properties" : { ..... } }

    See the following code:

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

    It creates mappings for the attributes ATTR1 - ATTR4. Now it is possible to define mapping for Example a list of different attributes dynamically. Hope it helps someone else.

    0 讨论(0)
提交回复
热议问题