Florian Ernst
Florian Ernst

Reputation: 3

Elastic Search 7 High level Client Create Index with mapping

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?

Upvotes: 0

Views: 5663

Answers (3)

Florian Ernst
Florian Ernst

Reputation: 3

i also tried to seperate the create index with settings and the mapping part. The create index + settings is working well.

But the step add the mapping are not working:

final PutMappingRequest requestMapping = new PutMappingRequest("seminar_map");

            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();
            requestMapping.source(builder);

            client.indices().putMapping(requestMapping, RequestOptions.DEFAULT);

Dies gibt aber leider den Fehler: org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing;

But when iam adding a type to the request: requestMapping.type("_doc") it throws the exception

Elasticsearch exception [type=illegal_argument_exception, reason=Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true.]

So it doesnt work with a type and doesnt work without a type :-D

Upvotes: 0

Kumar V
Kumar V

Reputation: 1670

Types are deprecated in Elastic 7.x and will be removed in future versions of elastic. Changing request.mapping("_doc",builder) to request.mapping(builder) should work.

For the second question, Yes elastic search will add the missing fields that are not mentioned in the mapping when we save the documents.

Upvotes: 0

ibexit
ibexit

Reputation: 3667

The error comes from the _doc in the JSON you are creating and elastic complains about it because of this.

Two options from here:

  1. Add include_type_name=true to the URL

OR

  1. Replace this line request.mapping("_doc",builder); with request.source(builder);,

See the docs for more details:

  1. https://www.elastic.co/guide/en/elasticsearch/reference/7.5/removal-of-types.html#_schedule_for_removal_of_mapping_types (note the difference between 6.8 and 7.X)

  2. https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-put-mapping.html#CO385-1

Upvotes: 2

Related Questions