Index mapping from Java client

Hi,

I want to use ElasticSearch as a Time Series Database and need to declare 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...

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