Search in sources :

Example 1 with SearchRequest

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);
}
Also used : ScriptSortType(co.elastic.clients.elasticsearch._types.ScriptSortType) IntStream.range(java.util.stream.IntStream.range) IntegrationTest(core.framework.search.IntegrationTest) Inject(core.framework.inject.Inject) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SearchType(co.elastic.clients.elasticsearch._types.SearchType) Sorts(core.framework.search.query.Sorts) Queries.match(core.framework.search.query.Queries.match) BigDecimal(java.math.BigDecimal) Lists(core.framework.util.Lists) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) JSON(core.framework.json.JSON) ClusterStateResponse(core.framework.search.ClusterStateResponse) SearchRequest(core.framework.search.SearchRequest) SearchResponse(core.framework.search.SearchResponse) Maps(core.framework.util.Maps) LocalTime(java.time.LocalTime) Queries.term(core.framework.search.query.Queries.term) RoundingMode(java.math.RoundingMode) Aggregations.sum(core.framework.search.query.Aggregations.sum) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) JsonData(co.elastic.clients.json.JsonData) ForEach(core.framework.search.ForEach) Collectors(java.util.stream.Collectors) Queries.range(core.framework.search.query.Queries.range) Test(org.junit.jupiter.api.Test) ElasticSearchType(core.framework.search.ElasticSearchType) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) ElasticSearch(core.framework.search.ElasticSearch) ClasspathResources(core.framework.util.ClasspathResources) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) SortOptions(co.elastic.clients.elasticsearch._types.SortOptions) SearchRequest(core.framework.search.SearchRequest) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) ZonedDateTime(java.time.ZonedDateTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) IntegrationTest(core.framework.search.IntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with SearchRequest

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");
}
Also used : ScriptSortType(co.elastic.clients.elasticsearch._types.ScriptSortType) IntStream.range(java.util.stream.IntStream.range) IntegrationTest(core.framework.search.IntegrationTest) Inject(core.framework.inject.Inject) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SearchType(co.elastic.clients.elasticsearch._types.SearchType) Sorts(core.framework.search.query.Sorts) Queries.match(core.framework.search.query.Queries.match) BigDecimal(java.math.BigDecimal) Lists(core.framework.util.Lists) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) JSON(core.framework.json.JSON) ClusterStateResponse(core.framework.search.ClusterStateResponse) SearchRequest(core.framework.search.SearchRequest) SearchResponse(core.framework.search.SearchResponse) Maps(core.framework.util.Maps) LocalTime(java.time.LocalTime) Queries.term(core.framework.search.query.Queries.term) RoundingMode(java.math.RoundingMode) Aggregations.sum(core.framework.search.query.Aggregations.sum) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) JsonData(co.elastic.clients.json.JsonData) ForEach(core.framework.search.ForEach) Collectors(java.util.stream.Collectors) Queries.range(core.framework.search.query.Queries.range) Test(org.junit.jupiter.api.Test) ElasticSearchType(core.framework.search.ElasticSearchType) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) ElasticSearch(core.framework.search.ElasticSearch) ClasspathResources(core.framework.util.ClasspathResources) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) SortOptions(co.elastic.clients.elasticsearch._types.SortOptions) SearchRequest(core.framework.search.SearchRequest) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) ZonedDateTime(java.time.ZonedDateTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) IntegrationTest(core.framework.search.IntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with SearchRequest

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);
}
Also used : SearchRequest(core.framework.search.SearchRequest) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) NotFoundException(core.framework.web.exception.NotFoundException) ActionDocument(core.log.domain.ActionDocument)

Example 4 with SearchRequest

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);
}
Also used : SearchRequest(core.framework.search.SearchRequest) Script(org.elasticsearch.script.Script) IntegrationTest(core.framework.test.IntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with SearchRequest

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);
    }
}
Also used : AnalyzeResponse(co.elastic.clients.elasticsearch.indices.AnalyzeResponse) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) CompletionSuggestOption(co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption) GetRequest(core.framework.search.GetRequest) CodeBuilder(core.framework.internal.asm.CodeBuilder) ActionLogContext(core.framework.log.ActionLogContext) LoggerFactory(org.slf4j.LoggerFactory) UpdateRequest(core.framework.search.UpdateRequest) Index(core.framework.search.Index) BulkIndexRequest(core.framework.search.BulkIndexRequest) IndexRequest(core.framework.search.IndexRequest) StopWatch(core.framework.util.StopWatch) ArrayList(java.util.ArrayList) DeleteResponse(co.elastic.clients.elasticsearch.core.DeleteResponse) BulkDeleteRequest(core.framework.search.BulkDeleteRequest) Markers.errorCode(core.framework.log.Markers.errorCode) CompleteRequest(core.framework.search.CompleteRequest) Duration(java.time.Duration) Map(java.util.Map) SearchException(core.framework.search.SearchException) SearchRequest(core.framework.search.SearchRequest) SearchResponse(core.framework.search.SearchResponse) ErrorCause(co.elastic.clients.elasticsearch._types.ErrorCause) Time(co.elastic.clients.elasticsearch._types.Time) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) Validator(core.framework.internal.validate.Validator) Logger(org.slf4j.Logger) JsonData(co.elastic.clients.json.JsonData) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) Collection(java.util.Collection) Suggester(co.elastic.clients.elasticsearch.core.search.Suggester) IOException(java.io.IOException) AnalyzeToken(co.elastic.clients.elasticsearch.indices.analyze.AnalyzeToken) ForEach(core.framework.search.ForEach) Collectors(java.util.stream.Collectors) Result(co.elastic.clients.elasticsearch._types.Result) UncheckedIOException(java.io.UncheckedIOException) DeleteRequest(core.framework.search.DeleteRequest) Hit(co.elastic.clients.elasticsearch.core.search.Hit) ElasticSearchType(core.framework.search.ElasticSearchType) List(java.util.List) AnalyzeRequest(core.framework.search.AnalyzeRequest) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) BulkResponseItem(co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem) Strings(core.framework.util.Strings) Optional(java.util.Optional) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) StopWatch(core.framework.util.StopWatch) SearchResponse(core.framework.search.SearchResponse)

Aggregations

SearchRequest (core.framework.search.SearchRequest)5 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)3 JsonData (co.elastic.clients.json.JsonData)3 ElasticSearchType (core.framework.search.ElasticSearchType)3 ForEach (core.framework.search.ForEach)3 SearchResponse (core.framework.search.SearchResponse)3 ScriptSortType (co.elastic.clients.elasticsearch._types.ScriptSortType)2 SearchType (co.elastic.clients.elasticsearch._types.SearchType)2 SortOptions (co.elastic.clients.elasticsearch._types.SortOptions)2 SortOrder (co.elastic.clients.elasticsearch._types.SortOrder)2 Inject (core.framework.inject.Inject)2 JSON (core.framework.json.JSON)2 ClusterStateResponse (core.framework.search.ClusterStateResponse)2 ElasticSearch (core.framework.search.ElasticSearch)2 IntegrationTest (core.framework.search.IntegrationTest)2 Aggregations.sum (core.framework.search.query.Aggregations.sum)2 Queries.match (core.framework.search.query.Queries.match)2 Queries.range (core.framework.search.query.Queries.range)2 Queries.term (core.framework.search.query.Queries.term)2 Sorts (core.framework.search.query.Sorts)2