From f68c00287b0f2909337cfd5b1e6ae5a47a67651c Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Fri, 4 Nov 2022 11:44:02 -0700 Subject: [PATCH] Document how to use Java 8 Time (JSR 310) objects Signed-off-by: Daniel Widdis --- CHANGELOG.md | 2 +- USER_GUIDE.md | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 477de08835..09232a16a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Add timeout and throttle to the jenkins workflows ([#231](https://github.com/opensearch-project/opensearch-java/pull/231)) - Updating maintainers, admins and documentation ([#248](https://github.com/opensearch-project/opensearch-java/pull/248)) - Migrate client transports to Apache HttpClient / Core 5.x ([#246](https://github.com/opensearch-project/opensearch-java/pull/246)) -- Add support for JSR-310 Date and Time API classes ([#251](https://github.com/opensearch-project/opensearch-java/pull/251)) +- Document how to use Java 8 Time (JSR 310) objects ([#251](https://github.com/opensearch-project/opensearch-java/pull/251)) - Fixing version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254)) ### Deprecated diff --git a/USER_GUIDE.md b/USER_GUIDE.md index f51063052e..c09f17c19b 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -2,9 +2,10 @@ - [User Guide](#user-guide) - [Sample data](#sample-data) + - [Create a client](#create-a-client) - [Create an index](#create-an-index) - [Index data](#index-data) - - [Search for the document](#search-for-the-document) + - [Search for the document](#search-for-the-documents) - [Search documents using a match query](#search-documents-using-a-match-query) - [Aggregations](#aggregations) - [Delete the document](#delete-the-document) @@ -48,6 +49,25 @@ static class IndexData { } ``` +## Create a client + +```java +Transport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); +OpenSearchClient client = new OpenSearchClient(transport); +``` + +The `JacksonJsonpMapper` class (2.x versions) only supports Java 7 objects by default. +[Java 8 modules](https://github.com/FasterXML/jackson-modules-java8) to support JDK8 classes +such as the Date and Time API (JSR-310), `Optional`, and more can be used by including +[the additional datatype dependency](https://github.com/FasterXML/jackson-modules-java8#usage) +and adding the module. For example, to include JSR-310 classes: + +```java +Transport transport = new RestClientTransport(restClient, + new JacksonJsonpMapper(new ObjectMapper().registerModule(new JavaTimeModule()))); +OpenSearchClient client = new OpenSearchClient(transport); +``` + ## Create an index ```java