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());
}
}
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);
}
}
Aggregations