Search in sources :

Example 1 with SearchType

use of co.elastic.clients.elasticsearch._types.SearchType in project syncope by apache.

the class ElasticsearchAnySearchDAOTest method searchRequest_groupOwner.

@Test
public void searchRequest_groupOwner() throws IOException {
    // 1. mock
    AnyUtils anyUtils = mock(AnyUtils.class);
    when(anyUtils.getField("id")).thenReturn(ReflectionUtils.findField(JPAUser.class, "id"));
    when(anyUtils.newPlainAttrValue()).thenReturn(new JPAUPlainAttrValue());
    when(anyUtilsFactory.getInstance(AnyTypeKind.USER)).thenReturn(anyUtils);
    when(entityFactory.newEntity(PlainSchema.class)).thenReturn(new JPAPlainSchema());
    when(groupDAO.findKey("groupKey")).thenReturn("groupKey");
    try (MockedStatic<ElasticsearchUtils> utils = Mockito.mockStatic(ElasticsearchUtils.class)) {
        utils.when(() -> ElasticsearchUtils.getContextDomainName(SyncopeConstants.MASTER_DOMAIN, AnyTypeKind.USER)).thenReturn("master_user");
        // 2. test
        Set<String> adminRealms = Set.of(RealmUtils.getGroupOwnerRealm("/any", "groupKey"));
        AnyCond anyCond = new AnyCond(AttrCond.Type.ISNOTNULL);
        anyCond.setSchema("id");
        SearchRequest request = new SearchRequest.Builder().index(ElasticsearchUtils.getContextDomainName(AuthContextUtils.getDomain(), AnyTypeKind.USER)).searchType(SearchType.QueryThenFetch).query(searchDAO.getQuery(adminRealms, SearchCond.getLeaf(anyCond), AnyTypeKind.USER)).from(1).size(10).build();
        assertThat(new Query.Builder().bool(QueryBuilders.bool().must(new Query.Builder().exists(QueryBuilders.exists().field("id").build()).build()).must(new Query.Builder().term(QueryBuilders.term().field("memberships").value(FieldValue.of("groupKey")).build()).build()).build()).build()).usingRecursiveComparison().isEqualTo(request.query());
    }
}
Also used : SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) JPAUPlainAttrValue(org.apache.syncope.core.persistence.jpa.entity.user.JPAUPlainAttrValue) ElasticsearchUtils(org.apache.syncope.ext.elasticsearch.client.ElasticsearchUtils) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) JPAUser(org.apache.syncope.core.persistence.jpa.entity.user.JPAUser) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) Test(org.junit.jupiter.api.Test)

Example 2 with SearchType

use of co.elastic.clients.elasticsearch._types.SearchType 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

ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)1 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)1 Result (co.elastic.clients.elasticsearch._types.Result)1 Time (co.elastic.clients.elasticsearch._types.Time)1 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)1 BulkResponse (co.elastic.clients.elasticsearch.core.BulkResponse)1 DeleteResponse (co.elastic.clients.elasticsearch.core.DeleteResponse)1 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)1 SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)1 BulkOperation (co.elastic.clients.elasticsearch.core.bulk.BulkOperation)1 BulkResponseItem (co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem)1 CompletionSuggestOption (co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption)1 Hit (co.elastic.clients.elasticsearch.core.search.Hit)1 Suggester (co.elastic.clients.elasticsearch.core.search.Suggester)1 AnalyzeResponse (co.elastic.clients.elasticsearch.indices.AnalyzeResponse)1 AnalyzeToken (co.elastic.clients.elasticsearch.indices.analyze.AnalyzeToken)1 JsonData (co.elastic.clients.json.JsonData)1 CodeBuilder (core.framework.internal.asm.CodeBuilder)1 Validator (core.framework.internal.validate.Validator)1 ActionLogContext (core.framework.log.ActionLogContext)1