use of co.elastic.clients.elasticsearch.core.SearchRequest in project syncope by apache.
the class ElasticsearchAnySearchDAO method doSearch.
@Override
protected <T extends Any<?>> List<T> doSearch(final Set<String> adminRealms, final SearchCond cond, final int page, final int itemsPerPage, final List<OrderByClause> orderBy, final AnyTypeKind kind) {
SearchRequest request = new SearchRequest.Builder().index(ElasticsearchUtils.getContextDomainName(AuthContextUtils.getDomain(), kind)).searchType(SearchType.QueryThenFetch).query(getQuery(adminRealms, cond, kind)).from(itemsPerPage * (page <= 0 ? 0 : page - 1)).size(itemsPerPage < 0 ? elasticsearchUtils.getIndexMaxResultWindow() : itemsPerPage).sort(sortBuilders(kind, orderBy)).build();
@SuppressWarnings("rawtypes") List<Hit<Map>> esResult = null;
try {
esResult = client.search(request, Map.class).hits().hits();
} catch (Exception e) {
LOG.error("While searching in Elasticsearch", e);
}
return CollectionUtils.isEmpty(esResult) ? List.of() : buildResult(esResult.stream().map(Hit::id).collect(Collectors.toList()), kind);
}
use of co.elastic.clients.elasticsearch.core.SearchRequest 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.core.SearchRequest in project weicoder by wdcode.
the class ElasticSearch method all.
/**
* 查询全部数据
*
* @param <E>
* @param cls
* @return
*/
public <E extends Index> List<E> all(Class<E> cls) {
// 执行搜索,向ES发起http请求
List<E> list = Lists.newList();
try {
// 搜索请求对象
SearchRequest searchRequest = SearchRequest.of(r -> r.index(getIndexName(U.C.newInstance(cls))).query(QueryBuilders.matchAll().build()._toQuery()).source(new SourceConfig.Builder().build()));
// 搜索全部对象
client.search(searchRequest, cls).hits().hits().forEach(h -> list.add(BeanUtil.copy(h.fields(), cls)));
} catch (Exception e) {
Logs.error(e);
}
// 返回查询列表
return list;
}
Aggregations