use of co.elastic.clients.elasticsearch._types.Refresh in project opensearch-java by opensearch-project.
the class RequestTest method testDataIngestion.
@Test
public void testDataIngestion() throws Exception {
String index = "ingest-test";
// Create an index
CreateIndexResponse createIndexResponse = highLevelClient().indices().create(b -> b.index(index));
assertEquals(index, createIndexResponse.index());
// Check that it actually exists. Example of a boolean response
BooleanResponse existsResponse = highLevelClient().indices().exists(b -> b.index(index));
assertTrue(existsResponse.value());
// Ingest some data
AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("foo");
String docId = highLevelClient().index(b -> b.index(index).id(// test with url-unsafe string
"my/Id").document(appData).refresh(// Make it visible for search
Refresh.True)).id();
assertEquals("my/Id", docId);
// Check auto-created mapping
GetMappingResponse mapping = highLevelClient().indices().getMapping(b -> b.index(index));
assertEquals(Property.Kind.Long, mapping.get("ingest-test").mappings().properties().get("intValue")._kind());
// Query by id
AppData esData = highLevelClient().get(b -> b.index(index).id(docId), AppData.class).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
// Query by id a non-existing document
final GetResponse<AppData> notExists = highLevelClient().get(b -> b.index(index).id("some-random-id"), AppData.class);
assertFalse(notExists.found());
assertNull(notExists.source());
// Search
SearchResponse<AppData> search = highLevelClient().search(b -> b.index(index), AppData.class);
long hits = search.hits().total().value();
assertEquals(1, hits);
esData = search.hits().hits().get(0).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
RequestItem item = RequestItem.of(_1 -> _1.header(_2 -> _2.index("test")).body(_2 -> _2.size(4)));
// MSearch: 1st search on an existing index, 2nd one on a non-existing index
final MsearchResponse<AppData> msearch = highLevelClient().msearch(_0 -> _0.searches(_1 -> _1.header(_3 -> _3.index(index)).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))).searches(_1 -> _1.header(_3 -> _3.index("non-existing")).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))), AppData.class);
assertEquals(2, msearch.responses().size());
assertTrue(msearch.responses().get(0).isResult());
assertEquals(1, msearch.responses().get(0).result().hits().hits().size());
assertTrue(msearch.responses().get(1).isFailure());
assertEquals(404, msearch.responses().get(1).failure().status());
}
use of co.elastic.clients.elasticsearch._types.Refresh in project opensearch-java by opensearch-project.
the class RequestTest method testSearchAggregation.
@Test
public void testSearchAggregation() throws IOException {
highLevelClient().create(_1 -> _1.index("products").id("A").document(new Product(5)).refresh(Refresh.True));
highLevelClient().create(_1 -> _1.index("products").id("B").document(new Product(15)).refresh(Refresh.True));
highLevelClient().create(_1 -> _1.index("products").id("C").document(new Product(25)).refresh(Refresh.True));
SearchResponse<Product> searchResponse = highLevelClient().search(_1 -> _1.index("products").size(0).aggregations("prices", _3 -> _3.histogram(_4 -> _4.field("price").interval(10.0)).aggregations("average", _5 -> _5.avg(_6 -> _6.field("price")))), Product.class);
HistogramAggregate prices = searchResponse.aggregations().get("prices").histogram();
assertEquals(3, prices.buckets().array().size());
assertEquals(1, prices.buckets().array().get(0).docCount());
assertEquals(5.0, prices.buckets().array().get(0).aggregations().get("average").avg().value(), 0.01);
// We've set "size" to zero
assertEquals(0, searchResponse.hits().hits().size());
}
use of co.elastic.clients.elasticsearch._types.Refresh in project para-search-elasticsearch by Erudika.
the class ESUtils method executeRequests.
/**
* Executes a batch of write requests.
* @param requests a list of index/delete requests,
*/
public static void executeRequests(List<BulkOperation> requests) {
if (requests == null || requests.isEmpty()) {
return;
}
try {
if (asyncEnabled()) {
getAsyncRESTClient().bulk(b -> b.operations(requests).refresh(flushImmediately() ? Refresh.True : Refresh.False)).thenAccept(b -> {
if (b.errors()) {
b.items().stream().filter(i -> i.status() != 200).forEach(item -> {
// FUTURE: Increment counter metric for failed document indexing
logger.error("Failed to execute async {} operation for index '{}', document id '{}': ", item.operationType(), item.index(), item.id(), item.error().reason());
});
}
});
} else {
BulkResponse res = getRESTClient().bulk(b -> b.operations(requests).refresh(flushImmediately() ? Refresh.True : Refresh.False));
if (res.errors()) {
res.items().stream().filter(i -> i.status() != 200).forEach(item -> {
// FUTURE: Increment counter metric for failed document indexing
logger.error("Failed to execute sync {} operation for index '{}', document id '{}': ", item.operationType(), item.index(), item.id(), item.error().reason());
});
handleFailedRequests();
}
}
} catch (Exception e) {
logger.error(null, e);
}
}
use of co.elastic.clients.elasticsearch._types.Refresh in project opensearch-java by opensearch-project.
the class RequestTest method testDefaultIndexSettings.
@Test
public void testDefaultIndexSettings() throws IOException {
String index = "index-settings";
highLevelClient().index(_1 -> _1.index(index).document(new Product(5)).refresh(Refresh.True));
GetIndicesSettingsResponse settings;
settings = highLevelClient().indices().getSettings(b -> b.index(index).includeDefaults(true));
assertNotNull(settings.get(index).defaults());
settings = highLevelClient().indices().getSettings(b -> b.index(index));
assertNull(settings.get(index).defaults());
}
use of co.elastic.clients.elasticsearch._types.Refresh in project elasticsearch-java by elastic.
the class RequestTest method testDataIngestion.
@Test
public void testDataIngestion() throws Exception {
String index = "ingest-test";
// Create an index
CreateIndexResponse createIndexResponse = client.indices().create(b -> b.index(index));
assertEquals(index, createIndexResponse.index());
// Check that it actually exists. Example of a boolean response
BooleanResponse existsResponse = client.indices().exists(b -> b.index(index));
assertTrue(existsResponse.value());
// Ingest some data
AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("foo");
String docId = client.index(b -> b.index(index).id(// test with url-unsafe string
"my/Id").document(appData).refresh(// Make it visible for search
Refresh.True)).id();
assertEquals("my/Id", docId);
// Check auto-created mapping
GetMappingResponse mapping = client.indices().getMapping(b -> b.index(index));
assertEquals(Property.Kind.Long, mapping.get("ingest-test").mappings().properties().get("intValue")._kind());
// Query by id
AppData esData = client.get(b -> b.index(index).id(docId), AppData.class).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
// Query by id a non-existing document
final GetResponse<AppData> notExists = client.get(b -> b.index(index).id("some-random-id"), AppData.class);
assertFalse(notExists.found());
assertNull(notExists.source());
// Search
SearchResponse<AppData> search = client.search(b -> b.index(index), AppData.class);
long hits = search.hits().total().value();
assertEquals(1, hits);
esData = search.hits().hits().get(0).source();
assertEquals(1337, esData.getIntValue());
assertEquals("foo", esData.getMsg());
RequestItem item = RequestItem.of(_1 -> _1.header(_2 -> _2.index("test")).body(_2 -> _2.size(4)));
// MSearch: 1st search on an existing index, 2nd one on a non-existing index
final MsearchResponse<AppData> msearch = client.msearch(_0 -> _0.searches(_1 -> _1.header(_3 -> _3.index(index)).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))).searches(_1 -> _1.header(_3 -> _3.index("non-existing")).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))), AppData.class);
assertEquals(2, msearch.responses().size());
assertTrue(msearch.responses().get(0).isResult());
assertEquals(1, msearch.responses().get(0).result().hits().hits().size());
assertTrue(msearch.responses().get(1).isFailure());
assertEquals(404, msearch.responses().get(1).failure().status());
}
Aggregations