5

I want to put mapping for a field by JAVA API but failed. Following is detailed information:

My data structure is :

{
  "mje-test-execution-id": "464b66ea6c914ddda217659c84a3cb9d",
  "jvm-free-memory": 315245608,
  "jvm-total-memory": 361758720,
  "system-free-memory": 0,
  "jvm-max-memory": 7600078848,
  "system-total-memory": 34199306240,
  "memory-time-stamp": "2020-03-12T05:12:16.835Z",
  "mje-host-name": "CN-00015345",
  "mje-test-suite-name": "SCF Test no mje version",
  "mje-version": "1.8.7771-SNAPSHOT",
  "mje-test-artifact-id": "msran-regression-tests",
  "mje-test-version": "1.8.7771-SNAPSHOT",
  "stp-id": "vran-stp",
  "mje-test-location": {
    "lat": 58.41,
    "lon": 15.62
  }
}

What I want to do is : put "mje-test-location" type to be "geo_point"

My code snippet :

public void postMapping(String indexName, String field, String type) throws IOException {
        GetIndexRequest request = new GetIndexRequest(indexName);

        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        if (!exists) {
            LOGGER.info("index {} does not exist. Now to post mapping.", indexName);
            PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {

                builder.startObject("properties");
                {
                    builder.startObject(field);
                    {
                        builder.field("type", type);
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
            putMappingRequest.source(builder);
            //

            AcknowledgedResponse putMappingResponse = client.indices().putMapping(putMappingRequest,
                    RequestOptions.DEFAULT);

            boolean acknowledged = putMappingResponse.isAcknowledged();
            if (acknowledged) {
                LOGGER.info("Succeed to put mapping: field:{}, type: {}", field, type);
            }
        }

        LOGGER.info("Fail to put mapping due to index {} already exist, ", indexName);
    }

Error info:

15:59:54.397 [main] DEBUG org.elasticsearch.client.RestClient - request [PUT http://seliiuapp00269.lmera.ericsson.se:9208/mje-scf-v2-20200313-post/_mapping?master_timeout=30s&timeout=30s] returned [HTTP/1.1 400 Bad Request]
org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=action_request_validation_exception, reason=Validation Failed: 1: mapping type is missing;]

ElasticSearch JAVA API Version : <elasticsearch.rest.high.level.client>7.0.0</elasticsearch.rest.high.level.client>

13
  • 1
    If I understand, the method postMapping() is called for each field of your document ? Commented Mar 13, 2020 at 12:13
  • yes. so do you have any ideas about this ? Commented Mar 17, 2020 at 3:56
  • @Littlesun, why you are calling this for each field, this would cause a lot of n/w calls, instead of that you should create a mapping in one go which includes all the fields Commented Mar 17, 2020 at 5:08
  • Yep, you should write a method postMapping(indexName), who handle all fields, with a json file for example or another method in : elastic.co/guide/en/elasticsearch/client/java-rest/master/… Commented Mar 17, 2020 at 12:46
  • 1
    Maybe you imported the wrong type. What is the full name of your class "PutMappingRequest": org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest or org.elasticsearch.client.indices.PutMappingRequest? You should use the second one. Commented Apr 4, 2020 at 20:11

2 Answers 2

1

You need to specify the document type like this:

putMappingRequest.type("_doc");

And also need to specify the types of the fields in here:

builder.field("type", type);

Like: ("type", "text") or ("type", "long"), ("type", "date") ......

You can see the datatypes in here

Sign up to request clarification or add additional context in comments.

Comments

1

I'll post @Mincong's comment as an answer because I was importing the wrong import that I found from github searchs:

What is the full name of your class "PutMappingRequest": org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest or org.elasticsearch.client.indices.PutMappingRequest? You should use the second one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.