use of core.framework.search.SearchRequest in project core-ng-project by neowu.
the class ElasticSearchIntegrationTest method search.
@Test
void search() {
TestDocument document = document("1", "1st Test's Product", 1, 0, null, LocalTime.NOON);
documentType.index(document.id, document);
elasticSearch.refreshIndex("document");
// test synonyms
var request = new SearchRequest();
request.type = SearchType.QueryThenFetch;
request.query = new Query.Builder().bool(b -> b.must(m -> m.match(match("string_field", "first"))).filter(f -> f.term(term("enum_field", JSON.toEnumValue(TestDocument.TestEnum.VALUE1))))).build();
request.sorts.add(SortOptions.of(builder -> builder.script(s -> s.script(script -> script.inline(i -> i.source("doc['int_field'].value * 3"))).type(ScriptSortType.Number))));
SearchResponse<TestDocument> response = documentType.search(request);
assertThat(response.totalHits).isEqualTo(1);
assertThat(response.hits).hasSize(1).first().usingRecursiveComparison().withComparatorForType(ChronoZonedDateTime.timeLineOrder(), ZonedDateTime.class).isEqualTo(document);
// test stemmer
request = new SearchRequest();
request.query = new Query.Builder().match(match("string_field", "test")).build();
response = documentType.search(request);
assertThat(response.totalHits).isEqualTo(1);
assertThat(response.hits).hasSize(1).first().usingRecursiveComparison().withComparatorForType(ChronoZonedDateTime.timeLineOrder(), ZonedDateTime.class).isEqualTo(document);
}
use of core.framework.search.SearchRequest in project core-ng-project by neowu.
the class ElasticSearchIntegrationTest method searchDateRange.
@Test
void searchDateRange() {
ZonedDateTime from = ZonedDateTime.now();
ZonedDateTime to = from.plusDays(5);
documentType.index("1", document("1", "value1", 1, 0, from, LocalTime.of(12, 0)));
documentType.index("2", document("2", "value2", 1, 0, from.plusDays(1), LocalTime.of(13, 0)));
documentType.index("3", document("3", "value3", 1, 0, to, LocalTime.of(14, 0)));
documentType.index("4", document("4", "value4", 1, 0, to.plusDays(1), LocalTime.of(15, 0)));
elasticSearch.refreshIndex("document");
var request = new SearchRequest();
request.query = new Query.Builder().range(range("zoned_date_time_field", from, to)).build();
request.sorts.add(Sorts.fieldSort("id", SortOrder.Asc));
SearchResponse<TestDocument> response = documentType.search(request);
assertThat(response.totalHits).isEqualTo(3);
assertThat(response.hits.stream().map(document1 -> document1.stringField).collect(Collectors.toList())).containsOnly("value1", "value2", "value3");
request.query = new Query.Builder().range(r -> r.field("local_time_field").gt(JsonData.of(LocalTime.of(13, 0)))).build();
response = documentType.search(request);
assertThat(response.totalHits).isEqualTo(2);
assertThat(response.hits.stream().map(document -> document.stringField).collect(Collectors.toList())).containsOnly("value3", "value4");
}
use of core.framework.search.SearchRequest in project core-ng-project by neowu.
the class DiagramService method getActionById.
private ActionDocument getActionById(String id) {
var request = new SearchRequest();
request.index = "action-*";
request.query = new Query.Builder().ids(ids(List.of(id))).build();
List<ActionDocument> documents = actionType.search(request).hits;
if (documents.isEmpty())
throw new NotFoundException("action not found, id=" + id);
return documents.get(0);
}
use of core.framework.search.SearchRequest in project core-ng-project by neowu.
the class ElasticSearchIntegrationTest method search.
@Test
void search() {
TestDocument document = createDocument("1", "value1", 1);
elasticSearch.flush("document");
SearchRequest request = new SearchRequest();
request.query = QueryBuilders.matchQuery("string_field", document.stringField);
request.sorts.add(SortBuilders.scriptSort(new Script("doc['num_field'].value * 3"), ScriptSortBuilder.ScriptSortType.NUMBER));
SearchResponse<TestDocument> response = documentType.search(request);
assertEquals(1, response.totalHits);
TestDocument returnedDocument = response.hits.get(0);
assertEquals(document.stringField, returnedDocument.stringField);
}
use of core.framework.search.SearchRequest in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method search.
@Override
public SearchResponse<T> search(SearchRequest request) {
var watch = new StopWatch();
validate(request);
long esTook = 0;
String index = request.index == null ? this.index : request.index;
int hits = 0;
try {
var searchRequest = co.elastic.clients.elasticsearch.core.SearchRequest.of(builder -> {
builder.index(index).query(request.query).aggregations(request.aggregations).sort(request.sorts).searchType(request.type).from(request.skip).size(request.limit);
if (request.trackTotalHitsUpTo != null)
builder.trackTotalHits(t -> t.count(request.trackTotalHitsUpTo));
return builder;
});
var response = elasticSearch.client.search(searchRequest, documentClass);
esTook = response.took() * 1_000_000;
hits = response.hits().hits().size();
long total = response.hits().total().value();
List<T> items = response.hits().hits().stream().map(Hit::source).toList();
return new SearchResponse<>(items, total, response.aggregations());
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ElasticsearchException e) {
throw elasticSearch.searchException(e);
} finally {
long elapsed = watch.elapsed();
ActionLogContext.track("elasticsearch", elapsed, hits, 0);
logger.debug("search, hits={}, esTook={}, elapsed={}", hits, esTook, elapsed);
checkSlowOperation(elapsed);
}
}
Aggregations