Search in sources :

Example 1 with Query

use of org.opensearch.client.opensearch._types.query_dsl.Query in project syncope by apache.

the class ElasticsearchAnySearchDAO method fillAttrQuery.

protected Query fillAttrQuery(final PlainSchema schema, final PlainAttrValue attrValue, final AttrCond cond) {
    Object value = schema.getType() == AttrSchemaType.Date && attrValue.getDateValue() != null ? attrValue.getDateValue().getTime() : attrValue.getValue();
    Query query = null;
    switch(cond.getType()) {
        case ISNOTNULL:
            query = new Query.Builder().exists(QueryBuilders.exists().field(schema.getKey()).build()).build();
            break;
        case ISNULL:
            query = new Query.Builder().bool(QueryBuilders.bool().mustNot(new Query.Builder().exists(QueryBuilders.exists().field(schema.getKey()).build()).build()).build()).build();
            break;
        case ILIKE:
            StringBuilder output = new StringBuilder();
            for (char c : cond.getExpression().toLowerCase().toCharArray()) {
                if (c == '%') {
                    output.append(".*");
                } else if (Character.isLetter(c)) {
                    output.append('[').append(c).append(Character.toUpperCase(c)).append(']');
                } else {
                    output.append(escapeForLikeRegex(c));
                }
            }
            query = new Query.Builder().regexp(QueryBuilders.regexp().field(schema.getKey()).value(output.toString()).build()).build();
            break;
        case LIKE:
            query = new Query.Builder().wildcard(QueryBuilders.wildcard().field(schema.getKey()).value(cond.getExpression().replace('%', '*')).build()).build();
            break;
        case IEQ:
            query = new Query.Builder().match(QueryBuilders.match().field(schema.getKey()).query(FieldValue.of(cond.getExpression().toLowerCase())).build()).build();
            break;
        case EQ:
            FieldValue fieldValue;
            if (value instanceof Double) {
                fieldValue = FieldValue.of((Double) value);
            } else if (value instanceof Long) {
                fieldValue = FieldValue.of((Long) value);
            } else if (value instanceof Boolean) {
                fieldValue = FieldValue.of((Boolean) value);
            } else {
                fieldValue = FieldValue.of(value.toString());
            }
            query = new Query.Builder().term(QueryBuilders.term().field(schema.getKey()).value(fieldValue).build()).build();
            break;
        case GE:
            query = new Query.Builder().range(QueryBuilders.range().field(schema.getKey()).gte(JsonData.of(value)).build()).build();
            break;
        case GT:
            query = new Query.Builder().range(QueryBuilders.range().field(schema.getKey()).gt(JsonData.of(value)).build()).build();
            break;
        case LE:
            query = new Query.Builder().range(QueryBuilders.range().field(schema.getKey()).lte(JsonData.of(value)).build()).build();
            break;
        case LT:
            query = new Query.Builder().range(QueryBuilders.range().field(schema.getKey()).lt(JsonData.of(value)).build()).build();
            break;
        default:
    }
    return query;
}
Also used : Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue)

Example 2 with Query

use of org.opensearch.client.opensearch._types.query_dsl.Query in project syncope by apache.

the class ElasticsearchAnySearchDAO method getQuery.

protected Query getQuery(final Set<String> adminRealms, final SearchCond cond, final AnyTypeKind kind) {
    Triple<Optional<Query>, Set<String>, Set<String>> filter = getAdminRealmsFilter(kind, adminRealms);
    Query query;
    if (SyncopeConstants.FULL_ADMIN_REALMS.equals(adminRealms)) {
        query = getQuery(cond, kind);
    } else {
        query = getQuery(buildEffectiveCond(cond, filter.getMiddle(), filter.getRight(), kind), kind);
        if (filter.getLeft().isPresent()) {
            query = new Query.Builder().bool(QueryBuilders.bool().must(filter.getLeft().get()).must(query).build()).build();
        }
    }
    return query;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Optional(java.util.Optional) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query)

Example 3 with Query

use of org.opensearch.client.opensearch._types.query_dsl.Query 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 4 with Query

use of org.opensearch.client.opensearch._types.query_dsl.Query in project opensearch-java by opensearch-project.

the class SearchRequest method serializeInternal.

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
    if (this.source != null) {
        generator.writeKey("_source");
        this.source.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.aggregations)) {
        generator.writeKey("aggregations");
        generator.writeStartObject();
        for (Map.Entry<String, Aggregation> item0 : this.aggregations.entrySet()) {
            generator.writeKey(item0.getKey());
            item0.getValue().serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (this.collapse != null) {
        generator.writeKey("collapse");
        this.collapse.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.docvalueFields)) {
        generator.writeKey("docvalue_fields");
        generator.writeStartArray();
        for (FieldAndFormat item0 : this.docvalueFields) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (this.explain != null) {
        generator.writeKey("explain");
        generator.write(this.explain);
    }
    if (ApiTypeHelper.isDefined(this.fields)) {
        generator.writeKey("fields");
        generator.writeStartArray();
        for (FieldAndFormat item0 : this.fields) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (this.from != null) {
        generator.writeKey("from");
        generator.write(this.from);
    }
    if (this.highlight != null) {
        generator.writeKey("highlight");
        this.highlight.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.indicesBoost)) {
        generator.writeKey("indices_boost");
        generator.writeStartArray();
        for (Map<String, Double> item0 : this.indicesBoost) {
            generator.writeStartObject();
            if (item0 != null) {
                for (Map.Entry<String, Double> item1 : item0.entrySet()) {
                    generator.writeKey(item1.getKey());
                    generator.write(item1.getValue());
                }
            }
            generator.writeEnd();
        }
        generator.writeEnd();
    }
    if (this.minScore != null) {
        generator.writeKey("min_score");
        generator.write(this.minScore);
    }
    if (this.postFilter != null) {
        generator.writeKey("post_filter");
        this.postFilter.serialize(generator, mapper);
    }
    if (this.profile != null) {
        generator.writeKey("profile");
        generator.write(this.profile);
    }
    if (this.query != null) {
        generator.writeKey("query");
        this.query.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.rescore)) {
        generator.writeKey("rescore");
        generator.writeStartArray();
        for (Rescore item0 : this.rescore) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.runtimeMappings)) {
        generator.writeKey("runtime_mappings");
        generator.writeStartObject();
        for (Map.Entry<String, RuntimeField> item0 : this.runtimeMappings.entrySet()) {
            generator.writeKey(item0.getKey());
            item0.getValue().serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.scriptFields)) {
        generator.writeKey("script_fields");
        generator.writeStartObject();
        for (Map.Entry<String, ScriptField> item0 : this.scriptFields.entrySet()) {
            generator.writeKey(item0.getKey());
            item0.getValue().serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.searchAfter)) {
        generator.writeKey("search_after");
        generator.writeStartArray();
        for (String item0 : this.searchAfter) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (this.seqNoPrimaryTerm != null) {
        generator.writeKey("seq_no_primary_term");
        generator.write(this.seqNoPrimaryTerm);
    }
    if (this.size != null) {
        generator.writeKey("size");
        generator.write(this.size);
    }
    if (this.slice != null) {
        generator.writeKey("slice");
        this.slice.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.sort)) {
        generator.writeKey("sort");
        generator.writeStartArray();
        for (SortOptions item0 : this.sort) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.stats)) {
        generator.writeKey("stats");
        generator.writeStartArray();
        for (String item0 : this.stats) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.storedFields)) {
        generator.writeKey("stored_fields");
        generator.writeStartArray();
        for (String item0 : this.storedFields) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (this.suggest != null) {
        generator.writeKey("suggest");
        this.suggest.serialize(generator, mapper);
    }
    if (this.terminateAfter != null) {
        generator.writeKey("terminate_after");
        generator.write(this.terminateAfter);
    }
    if (this.timeout != null) {
        generator.writeKey("timeout");
        generator.write(this.timeout);
    }
    if (this.trackScores != null) {
        generator.writeKey("track_scores");
        generator.write(this.trackScores);
    }
    if (this.trackTotalHits != null) {
        generator.writeKey("track_total_hits");
        this.trackTotalHits.serialize(generator, mapper);
    }
    if (this.version != null) {
        generator.writeKey("version");
        generator.write(this.version);
    }
}
Also used : Rescore(org.opensearch.client.opensearch.core.search.Rescore) RuntimeField(org.opensearch.client.opensearch._types.mapping.RuntimeField) FieldAndFormat(org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) ScriptField(org.opensearch.client.opensearch._types.ScriptField) HashMap(java.util.HashMap) Map(java.util.Map) SortOptions(org.opensearch.client.opensearch._types.SortOptions)

Example 5 with Query

use of org.opensearch.client.opensearch._types.query_dsl.Query in project opensearch-java by opensearch-project.

the class Source method serializeInternal.

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
    if (ApiTypeHelper.isDefined(this.index)) {
        generator.writeKey("index");
        generator.writeStartArray();
        for (String item0 : this.index) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (this.query != null) {
        generator.writeKey("query");
        this.query.serialize(generator, mapper);
    }
    if (this.remote != null) {
        generator.writeKey("remote");
        this.remote.serialize(generator, mapper);
    }
    if (this.size != null) {
        generator.writeKey("size");
        generator.write(this.size);
    }
    if (this.slice != null) {
        generator.writeKey("slice");
        this.slice.serialize(generator, mapper);
    }
    if (ApiTypeHelper.isDefined(this.sort)) {
        generator.writeKey("sort");
        generator.writeStartArray();
        for (SortOptions item0 : this.sort) {
            item0.serialize(generator, mapper);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.sourceFields)) {
        generator.writeKey("_source");
        generator.writeStartArray();
        for (String item0 : this.sourceFields) {
            generator.write(item0);
        }
        generator.writeEnd();
    }
    if (ApiTypeHelper.isDefined(this.runtimeMappings)) {
        generator.writeKey("runtime_mappings");
        generator.writeStartObject();
        for (Map.Entry<String, RuntimeField> item0 : this.runtimeMappings.entrySet()) {
            generator.writeKey(item0.getKey());
            item0.getValue().serialize(generator, mapper);
        }
        generator.writeEnd();
    }
}
Also used : RuntimeField(org.opensearch.client.opensearch._types.mapping.RuntimeField) Map(java.util.Map) SortOptions(org.opensearch.client.opensearch._types.SortOptions)

Aggregations

Test (org.junit.Test)9 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)5 Map (java.util.Map)5 Query (org.opensearch.client.opensearch._types.query_dsl.Query)5 HashMap (java.util.HashMap)3 Assert (org.junit.Assert)3 OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)3 Aggregation (org.opensearch.client.opensearch._types.aggregations.Aggregation)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 LogManager (java.util.logging.LogManager)2 Logger (java.util.logging.Logger)2 DynRealm (org.apache.syncope.core.persistence.api.entity.DynRealm)2 Realm (org.apache.syncope.core.persistence.api.entity.Realm)2 Test (org.junit.jupiter.api.Test)2 OpenSearchClient (org.opensearch.client.opensearch.OpenSearchClient)2 NodeStatistics (org.opensearch.client.opensearch._types.NodeStatistics)2 SortOptions (org.opensearch.client.opensearch._types.SortOptions)2