I am trying to insert a record under the index /testdata/doc. But, the code is not working. I would like to know if I am using the API in a right way. Please help.
public class ElasticSearchWriter {
//private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchWriter.class);
private static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(
"localhost", 9200, "http")));
public static void writeToElasticSearch(String jsonString) throws JsonProcessingException, IOException {
ObjectMapper om = new ObjectMapper();
JsonNode actualObject = om.readTree(jsonString);
String documentId = actualObject.get("id").textValue();
IndexRequest request = new IndexRequest("testdata", "doc",documentId);
request.source(jsonString, XContentType.JSON);
client.indexAsync(request, new ActionListener<IndexResponse>() {
public void onResponse(IndexResponse indexResponse) {
System.out.println("The Index ID is :: " + indexResponse.getId());
System.out.println("The Index is :: " + indexResponse.getIndex());
System.out.println("The type is :: " + indexResponse.getType());
}
public void onFailure(Exception e) {
e.printStackTrace();
}
});
client.close();
}
private static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("search-cdxsdses-ztxv3lvq23ptq4sdr6d3fsjat4.us-east-2.es.amazonaws.com", 9200, "https")));
public static void createIndex() {
IndexRequest request = new IndexRequest("posts", "doc", "1").source("user", "kimchy",
"postDate", new Date(),
"message", "trying out Elasticsearch");
client.indexAsync(request, new ActionListener<IndexResponse>() {
public void onResponse(IndexResponse indexResponse) {
System.out.println("The index is :: "+indexResponse.getIndex());
}
public void onFailure(Exception e) {
e.printStackTrace();
}
});
}
}
When I am trying to create an index it is throwing an exception
Exception in thread "main" java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW
at org.elasticsearch.common.xcontent.json.JsonXContent.<clinit>(JsonXContent.java:57)
at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:121)
at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:366)
at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:347)
at com.cisco.sdp.microservices.ElasticSearchMS.ElasticSearchIndexCreator.createIndex(ElasticSearchIndexCreator.java:19)
at com.cisco.sdp.microservices.ElasticSearchMS.MainApp.main(MainApp.java:21)
Elasticsearch 6.x does not longer support multiple document types per index, but still requires you to specify a document type. You should be able to continue using the code as before.
However, if you try to index a document under a different document type than the first one used on an index, the request will be rejected.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.