Search in sources :

Example 1 with SearchRequest

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);
}
Also used : SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) Hit(co.elastic.clients.elasticsearch.core.search.Hit) Map(java.util.Map) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) IOException(java.io.IOException)

Example 2 with SearchRequest

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());
    }
}
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 3 with SearchRequest

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;
}
Also used : SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SourceConfig(co.elastic.clients.elasticsearch.core.search.SourceConfig) IOException(java.io.IOException)

Aggregations

SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)3 IOException (java.io.IOException)2 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)1 Hit (co.elastic.clients.elasticsearch.core.search.Hit)1 SourceConfig (co.elastic.clients.elasticsearch.core.search.SourceConfig)1 Map (java.util.Map)1 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 AnyCond (org.apache.syncope.core.persistence.api.dao.search.AnyCond)1 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)1 JPAPlainSchema (org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema)1 JPAUPlainAttrValue (org.apache.syncope.core.persistence.jpa.entity.user.JPAUPlainAttrValue)1 JPAUser (org.apache.syncope.core.persistence.jpa.entity.user.JPAUser)1 ElasticsearchUtils (org.apache.syncope.ext.elasticsearch.client.ElasticsearchUtils)1 Test (org.junit.jupiter.api.Test)1