iam using elastic search 7.0. I have a model which has to be saved to elastic.
When the index did not exist, then i try to save the documents directly to elastic:
final IndexRequest indexRequest = new IndexRequest("seminar_map", "seminar", id)
.source(new Gson().toJson(object), XContentType.JSON);
indexRequest.id(id);
final IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
Everything works fine and the documents will be saved to elastic.
But i want to have a custom analyzer and i need to change the mapping types.
So i tried to set up the index and the mapping before saving any documents with:
try {
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder()
.loadFromSource(Strings.toString(jsonBuilder()
.startObject()
.startObject("analysis")
.startObject("analyzer")
.startObject("case_insensitive_analyzer")
.field("tokenizer", "keyword")
.field("type", "custom")
.field("filter", new String[]{"lowercase"})
.endObject()
.endObject()
.endObject()
.endObject()), XContentType.JSON)
);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("seminar_nummer");
{
builder.field("analyzer", "case_insensitive_analyzer");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
request.mapping("_doc",builder);
return client.indices().create(request, RequestOptions.DEFAULT);
But i get the error:
Elasticsearch exception [type=illegal_argument_exception, reason=The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true.]
How can i fix that error?
And is the plan correct to create a index and a mapping with not all parameter and then save the documents with more parameters than created. So will elastic add the other missing parameters to the mappings or do i have to set the complete mapping within the creating index part?
The error comes from the _doc
in the JSON you are creating and elastic complains about it because of this.
Two options from here:
include_type_name=true
to the URLOR
request.mapping("_doc",builder);
with request.source(builder);
, See the docs for more details: