ElasticSearch Index Mapping with Java Client

Hi,

I want to use ElasticSearch 6.6 as a Time Series Database and need to map the timestamp as a date to plot the data in Grafana. I am stuck at the index creation because Java keeps throwing the same exception :

org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing;

I tried two different ways to make it working :

  1. Importing a JSON from a file :

     	ClassLoader loader = getClass().getClassLoader();
     	File file = new File(loader.getResource("elastic-mapping.json").getFile());
     	Scanner scanner = new Scanner(file);
     	String str = "";
     	while(scanner.hasNextLine()) { str += scanner.nextLine() + "\n"; }
     	scanner.close();
     	PutMappingRequest mapRequest = new PutMappingRequest("benchmark");
     	mapRequest.source(str, XContentType.JSON);
     	System.out.println(str);
     	AcknowledgedResponse putMappingResponse = client.indices().putMapping(mapRequest, RequestOptions.DEFAULT);
    

The JSON file :

{
    "properties":{
        "@timestamp":{"type":"date","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "alert":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "curve":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "engValidity":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "engValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "id":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "limits":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "monState":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "rawValidity":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "sample":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "spid":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "bitOffset":{"type":"float"},
        "bitSize":{"type":"float"},
        "pk":{"type":"float"},
        "rawValue":{"type":"float"},
        "receivedTime":{"type":"float"}
    }
}
  1. Building the JSON with a HashMap :

    // Properties
    Map<String, Object> timestamp = new HashMap<>();
    timestamp.put("type", "date");
    Map<String, Object> field2 = new HashMap<>();
    field2.put("type", "text");
    ...
    // Adds all the properties in the properties list
    Map<String, Object> properties = new HashMap<>();
    properties.put("@timestamp", timestamp);
    properties.put("field2", field2);
    ...
    // Final JSON map
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("properties", properties);
    // Builds and returns the request
    PutMappingRequest request = new PutMappingRequest("index");
    request.source(jsonMap);
    AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);

I can't find where I did a mistake...

I think that your JSON file should be like:

{
  "TYPENAME": {
    "properties":{
        "@timestamp":{"type":"date","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "alert":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "curve":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "engValidity":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "engValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "id":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "limits":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "monState":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "rawValidity":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "sample":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "spid":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
        "bitOffset":{"type":"float"},
        "bitSize":{"type":"float"},
        "pk":{"type":"float"},
        "rawValue":{"type":"float"},
        "receivedTime":{"type":"float"}
    }
  }
}

It returns the same error :cry:

I tried the following but on a 6.7 version.

        try (RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(HttpHost.create("http://localhost:9200")))) {
            try {
                client.indices().delete(new DeleteIndexRequest("test"), RequestOptions.DEFAULT);
            } catch (ElasticsearchStatusException ignored) { }
            client.indices().create(new CreateIndexRequest("test"), RequestOptions.DEFAULT);
            PutMappingRequest request =
                    new PutMappingRequest("test").source("{\n" +
                            "    \"properties\":{\n" +
                            "        \"foo\":{\"type\":\"text\"}\n" +
                            "    }\n" +
                            "}", XContentType.JSON);
            client.indices().putMapping(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }

This works well for me. May be that could help?

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