XContentBuilder Can not write a field name, expecting a value - what value?

I'm trying to set up the following mapping structure in Java High-Lever Rest API:

{
    "mappings": {
        "dynamic_templates": [{
            "articleNumber": {
                {
                    "match_mapping_type": "*"
                },
                {
                    "match": "articleNumber*"
                },
                {
                    "mapping": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword"
                            }
                        }

                    }
                }

            }
        }]
    }
}

I decided to use XContentBuilder for that:

XContentBuilder mapping = XContentFactory.jsonBuilder();

mapping.startObject()
    .startObject("mappings")
        .startArray("dynamic_templates")
            .startObject("articleNumber")
                .field("match_mapping_type", "*")
                .field("match", "articleNumber*")
                .startObject("mapping")
                    .field("type", "text")
                    .startObject("fields")
                        .startObject("keyword")
                            .field("type", "keyword")
                        .endObject()
                    .endObject()
                .endObject()
            .endObject()
        .endArray()
    .endObject();
    
return mapping;

Everything due to the instructions, but the following error was received:

com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value, and it points out to this line: .startObject("articleNumber").

Am I creating the JSON object wrong? Or the mapping structure is invalid?

I think you need to squeeze another startObject after .startArray("dynamic_templates") and .startObject("articleNumber") in order to resemble the {after the[array starts.

Now, I'm receiving the different error:

Invocation of i nit method failed; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [mappings : {dynamic_templates=[{articleNumber={mapping={type=text, fields={keyword={type=keyword}}}, match_mapping_type=string, match=articleNumber*}}]}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Root mapping definition has unsupported parameters: [mappings : {dynamic_templates=[{articleNumber={mapping={type=text, fields={keyword={type=keyword}}}, match_mapping_type=string, match=articleNumber*}}]}]]];

Is it something with object parser?

what endpoint are you calling? can you provide a full reproduction instead of just snippets?

The problem was fixed by removing mappings object.
This is the correct structure of a mapping body:

{
    "dynamic_templates": [
        {
            "articleNumber": {
                "match_mapping_type": "string",
                "match": "articleNumber*",
                "mapping": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword"
                        }
                    }
                    
                }
            }
        }
    ] 
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.