Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an example for bulk update operation #690

Merged
merged 9 commits into from
Oct 27, 2023
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Dependencies

### Changed
Allow null values in arrays ([#687](https://github.com/opensearch-project/opensearch-java/pull/687))
- Allow null values in arrays ([#687](https://github.com/opensearch-project/opensearch-java/pull/687))
- Add an example for bulk update operation in samples ([#690](https://github.com/opensearch-project/opensearch-java/pull/690))

### Deprecated

Expand Down
49 changes: 38 additions & 11 deletions samples/src/main/java/org/opensearch/client/samples/Bulk.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

package org.opensearch.client.samples;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.Refresh;
import org.opensearch.client.opensearch._types.mapping.IntegerNumberProperty;
import org.opensearch.client.opensearch._types.mapping.Property;
Expand All @@ -33,16 +35,16 @@
*/
public class Bulk {
private static final Logger LOGGER = LogManager.getLogger(Bulk.class);
private static OpenSearchClient client;
karthiks3000 marked this conversation as resolved.
Show resolved Hide resolved
private static final String indexName = "my-index";

public static void main(String[] args) {
try {
var client = SampleClient.create();
client = SampleClient.create();

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

final var indexName = "my-index";

if (!client.indices().exists(r -> r.index(indexName)).value()) {
LOGGER.info("Creating index {}", indexName);
IndexSettings settings = new IndexSettings.Builder().numberOfShards("2").numberOfReplicas("1").build();
Expand Down Expand Up @@ -70,21 +72,46 @@ public static void main(String[] args) {
BulkResponse bulkResponse = client.bulk(bulkReq.build());
LOGGER.info("Bulk response items: {}", bulkResponse.items().size());

Query query = Query.of(qb -> qb.match(mb -> mb.field("title").query(fv -> fv.stringValue("Document"))));
final SearchRequest.Builder searchReq = new SearchRequest.Builder().allowPartialSearchResults(false)
.index(List.of(indexName))
.size(10)
.source(sc -> sc.fetch(false))
.ignoreThrottled(false)
.query(query);
SearchResponse<IndexData> searchResponse = client.search(searchReq.build(), IndexData.class);
// wait for the changes to reflect
Thread.sleep(3000);
karthiks3000 marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.info("Search & bulk update documents");

SearchResponse<IndexData> searchResponse = search(client, indexName, "title", "Document");
LOGGER.info("Found {} documents", searchResponse.hits().hits().size());

for (var hit : searchResponse.hits().hits()) {
karthiks3000 marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.info("Found {} with score {} and id {}", hit.source(), hit.score(), hit.id());
IndexData finalSearchedData = hit.source();
finalSearchedData.setText("Updated document");
BulkRequest request = new BulkRequest.Builder().operations(
o -> o.update(u -> u.index(indexName).id(hit.id()).document(finalSearchedData))
).build();
bulkResponse = client.bulk(request);
LOGGER.info("Bulk update response items: {}", bulkResponse.items().size());
}
// wait for the changes to reflect
Thread.sleep(3000);
searchResponse = search(client, indexName, "title", "Document");

for (var hit : searchResponse.hits().hits()) {
LOGGER.info("Found {} with score {} and id {}", hit.source(), hit.score(), hit.id());
}
LOGGER.info("Deleting index {}", indexName);
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(indexName).build();
client.indices().delete(deleteIndexRequest);
} catch (Exception e) {
LOGGER.error("Unexpected exception", e);
}
}

public static SearchResponse<IndexData> search(OpenSearchClient client, String indexName, String field, String value)
karthiks3000 marked this conversation as resolved.
Show resolved Hide resolved
throws IOException {
Query query = Query.of(qb -> qb.match(mb -> mb.field(field).query(fv -> fv.stringValue(value))));
final SearchRequest.Builder searchReq = new SearchRequest.Builder().allowPartialSearchResults(false)
.index(List.of(indexName))
.size(10)
.ignoreThrottled(false)
.query(query);
return client.search(searchReq.build(), IndexData.class);
}
}