Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Dependencies

### Changed
- Branch off Java and Kotlin based samples ([#2301](https://github.com/opensearch-project/opensearch-java/pull/2301))

### Deprecated

Expand Down
8 changes: 4 additions & 4 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ final OpenSearchTransport transport = ApacheHttpClient5TransportBuilder
OpenSearchClient client = new OpenSearchClient(transport);
```

See [SampleClient.java](./samples/src/main/java/org/opensearch/client/samples/SampleClient.java) for a working sample.
See [SampleClient.java](./samples/samples-java/src/main/java/org/opensearch/client/samples/SampleClient.java) for a working sample.

#### Using `RestClientTransport` (deprecated)

Expand Down Expand Up @@ -150,12 +150,12 @@ final CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().i
client.indices().create(createIndexRequest);
```

You can find a working sample of the above code in [FlatObjectBasics.java](./samples/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java).
You can find a working sample of the above code in [FlatObjectBasics.java](./samples/samples-java/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java).


### Indexing data

[IndexData](./samples/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.
[IndexData](./samples/samples-java/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.

```java
IndexData indexData = new IndexData("Document 1", "Text for document 1");
Expand Down Expand Up @@ -191,7 +191,7 @@ DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(i
DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);
```

You can find a working sample of the above code in [IndexingBasics.java](./samples/src/main/java/org/opensearch/client/samples/IndexingBasics.java).
You can find a working sample of the above code in [IndexingBasics.java](./samples/samples-java/src/main/java/org/opensearch/client/samples/IndexingBasics.java).

## Metrics

Expand Down
8 changes: 4 additions & 4 deletions guides/bulk.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ ingester.add(op -> op.index(i -> i.index(indexName).id("id3").document(doc3)));
ingester.close();
```

[IndexData](../samples/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.
[IndexData](../samples/samples-java/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.

You can find a working sample of the above code in [BulkIngesterBasics.java](../samples/src/main/java/org/opensearch/client/samples/BulkIngesterBasics.java).
You can find a working sample of the above code in [BulkIngesterBasics.java](../samples/samples-java/src/main/java/org/opensearch/client/samples/BulkIngesterBasics.java).


## Bulk requests
Expand Down Expand Up @@ -73,6 +73,6 @@ BulkRequest.Builder bulkReq = new BulkRequest.Builder()
BulkResponse bulkResponse = client.bulk(bulkReq.build());
```

[IndexData](../samples/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.
[IndexData](../samples/samples-java/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.

You can find a working sample of the above code in [Bulk.java](../samples/src/main/java/org/opensearch/client/samples/Bulk.java).
You can find a working sample of the above code in [Bulk.java](../samples/samples-java/src/main/java/org/opensearch/client/samples/Bulk.java).
2 changes: 1 addition & 1 deletion guides/data_stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ DeleteDataStreamRequest deleteDataStreamRequest = new DeleteDataStreamRequest.Bu
DeleteDataStreamResponse deleteDataStreamResponse = client.indices().deleteDataStream(deleteDataStreamRequest);
```

You can find a working sample of the above code in [DataStreamBasics.java](../samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java).
You can find a working sample of the above code in [DataStreamBasics.java](../samples/samples-java/src/main/java/org/opensearch/client/samples/DataStreamBasics.java).
2 changes: 1 addition & 1 deletion guides/document_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

This guide covers common document lifecycle operations with the OpenSearch Java client: indexing, retrieving, updating, reindexing, and deleting documents.

You can find a working version of the code in [DocumentLifecycle.java](../samples/src/main/java/org/opensearch/client/samples/DocumentLifecycle.java).
You can find a working version of the code in [DocumentLifecycle.java](../samples/samples-java/src/main/java/org/opensearch/client/samples/DocumentLifecycle.java).

## Setup

Expand Down
2 changes: 1 addition & 1 deletion guides/index_templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ client.indices().create(CreateIndexRequest.of(c -> c.index(indexName)));

The index will be created with the index settings and mappings defined by the `my-index-template` index template.

You can find a working sample of the above code in [IndexTemplates.java](../samples/src/main/java/org/opensearch/client/samples/IndexTemplates.java).
You can find a working sample of the above code in [IndexTemplates.java](../samples/samples-java/src/main/java/org/opensearch/client/samples/IndexTemplates.java).
10 changes: 5 additions & 5 deletions guides/plugins/knn.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Short for k-nearest neighbors, the k-NN plugin enables users to search for the k

## Basic Approximate k-NN

In the following example we create a 5-dimensional k-NN index with random data. You can find a synchronous version of this working sample in [samples/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java](../../samples/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java).
In the following example we create a 5-dimensional k-NN index with random data. You can find a synchronous version of this working sample in [samples/samples-java/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java](../../samples/samples-java/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java).

```bash
$ ./gradlew :samples:run -Dsamples.mainClass=knn.KnnBasics
Expand Down Expand Up @@ -117,7 +117,7 @@ for (var hit : searchResponse.hits().hits()) {

## Approximate k-NN with a Boolean Filter

In the [KnnBooleanFilter.java sample](../../samples/src/main/java/org/opensearch/client/samples/knn/KnnBooleanFilter.java) we create a 5-dimensional k-NN index with random data and a `metadata` field that contains a book genre (e.g. `fiction`). The search query is a k-NN search filtered by genre. The filter clause is outside the k-NN query clause and is applied after the k-NN search.
In the [KnnBooleanFilter.java sample](../../samples/samples-java/src/main/java/org/opensearch/client/samples/knn/KnnBooleanFilter.java) we create a 5-dimensional k-NN index with random data and a `metadata` field that contains a book genre (e.g. `fiction`). The search query is a k-NN search filtered by genre. The filter clause is outside the k-NN query clause and is applied after the k-NN search.

```java
var searchResponse = client.search(s -> s
Expand Down Expand Up @@ -153,7 +153,7 @@ $ ./gradlew :samples:run -Dsamples.mainClass=knn.KnnBooleanFilter

## Approximate k-NN with an Efficient Filter

In the [KnnEfficientFilter.java sample](../../samples/src/main/java/org/opensearch/client/samples/knn/KnnEfficientFilter.java) we implement the example in [the k-NN documentation](https://opensearch.org/docs/latest/search-plugins/knn/filter-search-knn/), which creates an index that uses the Lucene engine and HNSW as the method in the mapping, containing hotel location and parking data, then search for the top three hotels near the location with the coordinates `[5, 4]` that are rated between 8 and 10, inclusive, and provide parking.
In the [KnnEfficientFilter.java sample](../../samples/samples-java/src/main/java/org/opensearch/client/samples/knn/KnnEfficientFilter.java) we implement the example in [the k-NN documentation](https://opensearch.org/docs/latest/search-plugins/knn/filter-search-knn/), which creates an index that uses the Lucene engine and HNSW as the method in the mapping, containing hotel location and parking data, then search for the top three hotels near the location with the coordinates `[5, 4]` that are rated between 8 and 10, inclusive, and provide parking.

```java
var searchResponse = client.search(s -> s
Expand Down Expand Up @@ -207,7 +207,7 @@ $ ./gradlew :samples:run -Dsamples.mainClass=knn.KnnEfficientFilter

## Exact k-NN with a scoring script

In the [KnnScriptScore.java sample](../../samples/src/main/java/org/opensearch/client/samples/knn/KnnScriptScore.java) we create a 5-dimensional k-NN index with random data. The search query uses the [k-NN scoring script](https://opensearch.org/docs/latest/search-plugins/knn/knn-score-script/) to calculate exact nearest neighbors.
In the [KnnScriptScore.java sample](../../samples/samples-java/src/main/java/org/opensearch/client/samples/knn/KnnScriptScore.java) we create a 5-dimensional k-NN index with random data. The search query uses the [k-NN scoring script](https://opensearch.org/docs/latest/search-plugins/knn/knn-score-script/) to calculate exact nearest neighbors.

```java
var searchResponse = client.search(s -> s
Expand Down Expand Up @@ -249,7 +249,7 @@ $ ./gradlew :samples:run -Dsamples.mainClass=knn.KnnScriptScore

## Exact k-NN with the Painless scripting extensions

In the [KnnPainlessScript.java sample](../../samples/src/main/java/org/opensearch/client/samples/knn/KnnPainlessScript.java) we create a 5-dimensional k-NN index with random data. The search query uses the [k-NN Painless extensions](https://opensearch.org/docs/latest/search-plugins/knn/painless-functions/) to calculate exact nearest neighbors.
In the [KnnPainlessScript.java sample](../../samples/samples-java/src/main/java/org/opensearch/client/samples/knn/KnnPainlessScript.java) we create a 5-dimensional k-NN index with random data. The search query uses the [k-NN Painless extensions](https://opensearch.org/docs/latest/search-plugins/knn/painless-functions/) to calculate exact nearest neighbors.

```java
var searchResponse = client.search(s -> s
Expand Down
2 changes: 1 addition & 1 deletion guides/point_in_time.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ DeletePitRequest deletePitRequest = new DeletePitRequest.Builder()
DeletePitResponse deletePitResponse = client.deletePit(deletePitRequest);
```

You can find a working sample of the above code in [PointInTime.java](../samples/src/main/java/org/opensearch/client/samples/PointInTime.java).
You can find a working sample of the above code in [PointInTime.java](../samples/samples-java/src/main/java/org/opensearch/client/samples/PointInTime.java).
6 changes: 3 additions & 3 deletions guides/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ indexRequest = new IndexRequest.Builder<IndexData>().index(index).id("2").docume
client.index(indexRequest);
```

[IndexData](../samples/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.
[IndexData](../samples/samples-java/src/main/java/org/opensearch/client/samples/util/IndexData.java) refers to sample data class.

## Search API

Expand Down Expand Up @@ -104,7 +104,7 @@ for (var hit : searchResponse.hits().hits()) {

### Search documents using suggesters

[AppData](../samples/src/main/java/org/opensearch/client/samples/util/AppData.java) refers to the sample data class used in the below samples.
[AppData](../samples/samples-java/src/main/java/org/opensearch/client/samples/util/AppData.java) refers to the sample data class used in the below samples.

#### Using completion suggester

Expand Down Expand Up @@ -321,4 +321,4 @@ for (Map.Entry<String, Aggregate> entry : response.aggregations().entrySet()) {
}
```

You can find a working sample of the above code in [Search.java](../samples/src/main/java/org/opensearch/client/samples/Search.java).
You can find a working sample of the above code in [Search.java](../samples/samples-java/src/main/java/org/opensearch/client/samples/Search.java).
15 changes: 15 additions & 0 deletions samples/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

// This file is intentionally blank. All configuration of the
// distribution is done in the parent project.

// See please https://docs.gradle.org/8.5/userguide/upgrading_version_8.html#deprecated_missing_project_directory
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies {
implementation("org.apache.logging.log4j", "log4j-core","[2.17.1,3.0)")
implementation("org.apache.logging.log4j", "log4j-slf4j2-impl","[2.17.1,3.0)")
implementation("commons-logging", "commons-logging", "1.2")
implementation("tools.jackson.core", "jackson-databind", "3.2.2")
implementation("software.amazon.awssdk", "sdk-core", "[2.21,3.0)")
implementation("software.amazon.awssdk", "auth", "[2.21,3.0)")
implementation("software.amazon.awssdk", "http-auth-aws", "[2.21,3.0)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.json.jackson3.JacksonJsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder;

Expand Down
37 changes: 37 additions & 0 deletions samples/samples-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

plugins {
java
kotlin("jvm") version "2.3.21"
application

id("opensearch-java.spotless-conventions")
}

kotlin {
jvmToolchain(21)
}

dependencies {
implementation(project(":java-client"))
implementation("org.apache.logging.log4j", "log4j-api","[2.17.1,3.0)")
implementation("org.apache.logging.log4j", "log4j-core","[2.17.1,3.0)")
implementation("org.apache.logging.log4j", "log4j-slf4j2-impl","[2.17.1,3.0)")
implementation("commons-logging", "commons-logging", "1.2")
implementation("tools.jackson.core", "jackson-core", "3.2.4")
implementation("tools.jackson.core", "jackson-databind", "3.2.4")
}

application {
mainClass.set("org.opensearch.client.samples.Main")
}

tasks.named<JavaExec>("run") {
systemProperty("samples.mainClass", System.getProperty("samples.mainClass"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.samples

import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.opensearch.client.opensearch._types.Refresh
import org.opensearch.client.opensearch._types.mapping.IntegerNumberProperty
import org.opensearch.client.opensearch._types.mapping.Property
import org.opensearch.client.opensearch._types.mapping.TypeMapping
import org.opensearch.client.opensearch._types.query_dsl.Query
import org.opensearch.client.opensearch.core.BulkRequest
import org.opensearch.client.opensearch.core.SearchRequest
import org.opensearch.client.opensearch.core.bulk.BulkOperation
import org.opensearch.client.opensearch.core.bulk.IndexOperation
import org.opensearch.client.opensearch.indices.CreateIndexRequest
import org.opensearch.client.opensearch.indices.DeleteIndexRequest
import org.opensearch.client.opensearch.indices.IndexSettings
import org.opensearch.client.samples.util.IndexData

/**
* Run with: `./gradlew :samples:samples-kotlin:run -Dsamples.mainClass=Bulk`
*/
object Bulk {
private val LOGGER: Logger = LogManager.getLogger(Bulk::class.java)

@JvmStatic
fun main(args: Array<String>) {
try {
val client = SampleClient.create()

val version = client.info().version()
LOGGER.info("Server: {}@{}", version.distribution(), version.number())

val indexName = "my-index"

if (!client.indices().exists { r -> r.index(indexName) }.value()) {
LOGGER.info("Creating index {}", indexName)
val settings = IndexSettings.Builder().numberOfShards(2).numberOfReplicas(1).build()
val mapping = TypeMapping.Builder().properties(
"age",
Property.Builder().integer(IntegerNumberProperty.Builder().build()).build()
).build()
val createIndexRequest = CreateIndexRequest.Builder().index(indexName)
.settings(settings)
.mappings(mapping)
.build()
client.indices().create(createIndexRequest)
}

LOGGER.info("Bulk indexing documents")
val ops = ArrayList<BulkOperation>()
val doc1 = IndexData("Document 1", "The text of document 1")
ops.add(BulkOperation.Builder().index(IndexOperation.of { io -> io.index(indexName).id("id1").document(doc1) }).build())
val doc2 = IndexData("Document 2", "The text of document 2")
ops.add(BulkOperation.Builder().index(IndexOperation.of { io -> io.index(indexName).id("id2").document(doc2) }).build())
val doc3 = IndexData("Document 3", "The text of document 3")
ops.add(BulkOperation.Builder().index(IndexOperation.of { io -> io.index(indexName).id("id3").document(doc3) }).build())

val bulkReq = BulkRequest.Builder().index(indexName).operations(ops).refresh(Refresh.WaitFor)
var bulkResponse = client.bulk(bulkReq.build())
LOGGER.info("Bulk response items: {}", bulkResponse.items().size)

val query = Query.of { qb -> qb.match { mb -> mb.field("title").query { fv -> fv.stringValue("Document") } } }
val searchReq = SearchRequest.Builder().allowPartialSearchResults(false)
.index(listOf(indexName))
.size(10)
.source { sc -> sc.fetch(false) }
.ignoreThrottled(false)
.query(query)

val searchResponse = client.search(searchReq.build(), IndexData::class.java)
LOGGER.info("Found {} documents", searchResponse.hits().hits().size)

LOGGER.info("Bulk update document")
doc1.text = "Updated Document"
val request = BulkRequest.Builder().operations { o -> o.update { u -> u.index(indexName).id("id1").document(doc1) } }
.refresh(Refresh.WaitFor)
.build()
bulkResponse = client.bulk(request)
LOGGER.info("Bulk update response items: {}", bulkResponse.items().size)

LOGGER.info("Deleting index {}", indexName)
val deleteIndexRequest = DeleteIndexRequest.Builder().index(indexName).build()
client.indices().delete(deleteIndexRequest)
} catch (e: Exception) {
LOGGER.error("Unexpected exception", e)
}
}
}
Loading
Loading