use of core.framework.search.Index in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method update.
@Override
public void update(UpdateRequest<T> request) {
var watch = new StopWatch();
if (request.script == null)
throw new Error("request.script must not be null");
String index = request.index == null ? this.index : request.index;
try {
Map<String, JsonData> params = request.params == null ? Map.of() : request.params.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, value -> JsonData.of(value.getValue())));
elasticSearch.client.update(builder -> builder.index(index).id(request.id).script(s -> s.inline(i -> i.source(request.script).params(params))), documentClass);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, 0, 1);
logger.debug("update, index={}, id={}, script={}, elapsed={}", index, request.id, request.script, elapsed);
checkSlowOperation(elapsed);
}
}
use of core.framework.search.Index in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method bulkIndex.
@Override
public void bulkIndex(BulkIndexRequest<T> request) {
var watch = new StopWatch();
if (request.sources == null || request.sources.isEmpty())
throw new Error("request.sources must not be empty");
String index = request.index == null ? this.index : request.index;
List<BulkOperation> operations = new ArrayList<>(request.sources.size());
for (Map.Entry<String, T> entry : request.sources.entrySet()) {
String id = entry.getKey();
T source = entry.getValue();
validator.validate(source, false);
operations.add(BulkOperation.of(builder -> builder.index(i -> i.index(index).id(id).document(source))));
}
long esTook = 0;
try {
BulkResponse response = elasticSearch.client.bulk(builder -> builder.operations(operations));
// mills to nano
esTook = response.took() * 1_000_000;
validate(response);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, 0, request.sources.size());
logger.debug("bulkIndex, index={}, size={}, esTook={}, elapsed={}", index, request.sources.size(), esTook, elapsed);
checkSlowOperation(elapsed);
}
}
use of core.framework.search.Index in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method bulkDelete.
@Override
public void bulkDelete(BulkDeleteRequest request) {
var watch = new StopWatch();
if (request.ids == null || request.ids.isEmpty())
throw new Error("request.ids must not be empty");
String index = request.index == null ? this.index : request.index;
List<BulkOperation> operations = new ArrayList<>(request.ids.size());
for (String id : request.ids) {
operations.add(BulkOperation.of(builder -> builder.delete(r -> r.index(index).id(id))));
}
long esTook = 0;
try {
BulkResponse response = elasticSearch.client.bulk(builder -> builder.operations(operations));
// mills to nano
esTook = response.took() * 1_000_000;
validate(response);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
int size = request.ids.size();
ActionLogContext.track("elasticsearch", elapsed, 0, size);
logger.debug("bulkDelete, index={}, ids={}, size={}, esTook={}, elapsed={}", index, request.ids, size, esTook, elapsed);
checkSlowOperation(elapsed);
}
}
use of core.framework.search.Index in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method complete.
@Override
public List<String> complete(CompleteRequest request) {
var watch = new StopWatch();
long esTook = 0;
String index = request.index == null ? this.index : request.index;
int options = 0;
try {
var suggest = Suggester.of(builder -> {
builder.text(request.prefix);
for (String field : request.fields) {
builder.suggesters(field, s -> s.completion(c -> c.field(field).skipDuplicates(Boolean.TRUE).size(request.limit)));
}
return builder;
});
var response = elasticSearch.client.search(builder -> builder.index(index).suggest(suggest).source(s -> s.fetch(Boolean.FALSE)), documentClass);
esTook = response.took() * 1_000_000;
List<String> suggestions = response.suggest().values().stream().flatMap(Collection::stream).flatMap(suggestion -> suggestion.completion().options().stream()).map(CompletionSuggestOption::text).distinct().collect(Collectors.toList());
options = suggestions.size();
return suggestions;
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, options, 0);
logger.debug("complete, options={}, esTook={}, elapsed={}", options, esTook, elapsed);
checkSlowOperation(elapsed);
}
}
use of core.framework.search.Index in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method forEach.
@Override
public void forEach(ForEach<T> forEach) {
var watch = new StopWatch();
long start = System.nanoTime();
long esClientTook = 0;
long esServerTook = 0;
validate(forEach);
Time keepAlive = Time.of(t -> t.time(forEach.scrollTimeout.toNanos() + "nanos"));
String index = forEach.index == null ? this.index : forEach.index;
int totalHits = 0;
try {
var response = elasticSearch.client.search(builder -> builder.index(index).scroll(keepAlive).query(forEach.query).sort(s -> s.field(f -> f.field("_doc"))).size(forEach.limit), documentClass);
var holder = new ScrollIdHolder();
holder.scrollId = response.scrollId();
while (true) {
esServerTook += response.took() * 1_000_000;
var hits = response.hits().hits();
esClientTook += System.nanoTime() - start;
if (hits.isEmpty())
break;
totalHits += hits.size();
for (var hit : hits) {
forEach.consumer.accept(hit.source());
}
start = System.nanoTime();
response = elasticSearch.client.scroll(builder -> builder.scrollId(holder.scrollId).scroll(keepAlive), documentClass);
holder.scrollId = response.scrollId();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, totalHits, 0);
logger.debug("forEach, totalHits={}, esServerTook={}, esClientTook={}, elapsed={}", totalHits, esServerTook, esClientTook, elapsed);
}
}
Aggregations