Skip to content

Commit

Permalink
Document how to use Java 8 Time (JSR 310) objects (opensearch-project…
Browse files Browse the repository at this point in the history
…#251)

* Add support for JSR-310 Date and Time API classes

Signed-off-by: Daniel Widdis <widdis@gmail.com>

* Add unit tests

Signed-off-by: Daniel Widdis <widdis@gmail.com>

* Revert code changes in favor of a doc fix

Signed-off-by: Daniel Widdis <widdis@gmail.com>

* Document how to use Java 8 Time (JSR 310) objects

Signed-off-by: Daniel Widdis <widdis@gmail.com>

* Don't wrap markdown

Signed-off-by: Daniel Widdis <widdis@gmail.com>

Signed-off-by: Daniel Widdis <widdis@gmail.com>
  • Loading branch information
dbwiddis authored Nov 15, 2022
1 parent a67f102 commit fcdfa93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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))
- 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
Expand Down
20 changes: 18 additions & 2 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -48,6 +49,21 @@ 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
Expand Down Expand Up @@ -119,4 +135,4 @@ client.delete(d -> d.index(index).id("1"));
```java
DeleteIndexRequest deleteIndexRequest = new DeleteRequest.Builder().index(index).build();
DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);
```
```

0 comments on commit fcdfa93

Please sign in to comment.