Search in sources :

Example 1 with Query

use of co.elastic.clients.elasticsearch._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 co.elastic.clients.elasticsearch._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 co.elastic.clients.elasticsearch._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 co.elastic.clients.elasticsearch._types.query_dsl.Query in project opensearch-java by opensearch-project.

the class VariantsTest method testNested.

@Test
public void testNested() {
    // nested containers: query > intervals > interval
    // intervals is a single key dictionary
    // query has container properties
    Query q = Query.of(_0 -> _0.intervals(_1 -> _1.queryName("my-query").field("a_field").anyOf(_2 -> _2.intervals(_3 -> _3.match(_5 -> _5.query("match-query").analyzer("lowercase"))))));
    assertEquals(Query.Kind.Intervals, q._kind());
    assertNotNull(q.intervals());
    assertEquals("a_field", q.intervals().field());
    assertEquals(1, q.intervals().anyOf().intervals().size());
    assertEquals("lowercase", q.intervals().anyOf().intervals().get(0).match().analyzer());
    String json = toJson(q);
    assertEquals("{\"intervals\":{\"a_field\":{\"_name\":\"my-query\"," + "\"any_of\":{\"intervals\":[{\"match\":{\"analyzer\":\"lowercase\",\"query\":\"match-query\"}}]}}}}", json);
    Query q2 = fromJson(json, Query.class);
    assertEquals(json, toJson(q2));
    assertEquals(Query.Kind.Intervals, q2._kind());
    assertNotNull(q2.intervals());
    assertEquals("a_field", q2.intervals().field());
    assertEquals(1, q2.intervals().anyOf().intervals().size());
    assertEquals("lowercase", q2.intervals().anyOf().intervals().get(0).match().analyzer());
}
Also used : SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) Query(org.opensearch.client.opensearch._types.query_dsl.Query) JsonData(org.opensearch.client.json.JsonData) Property(org.opensearch.client.opensearch._types.mapping.Property) QueryBuilders(org.opensearch.client.opensearch._types.query_dsl.QueryBuilders) Test(org.junit.Test) TypeMapping(org.opensearch.client.opensearch._types.mapping.TypeMapping) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) Query(org.opensearch.client.opensearch._types.query_dsl.Query) Test(org.junit.Test)

Example 5 with Query

use of co.elastic.clients.elasticsearch._types.query_dsl.Query in project opensearch-java by opensearch-project.

the class BehaviorsTest method testShortcutProperty.

@Test
public void testShortcutProperty() {
    // All-in-one: a variant, wrapping a single-key dictionary with a shortcut property
    String json = "{\"term\":{\"some-field\":\"some-value\"}}";
    Query q = fromJson(json, Query.class);
    assertEquals("some-field", q.term().field());
    assertEquals("some-value", q.term().value().stringValue());
}
Also used : Query(org.opensearch.client.opensearch._types.query_dsl.Query) ShapeQuery(org.opensearch.client.opensearch._types.query_dsl.ShapeQuery) TermQuery(org.opensearch.client.opensearch._types.query_dsl.TermQuery) Test(org.junit.Test)

Aggregations

Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)5 Test (org.junit.Test)5 Query (org.opensearch.client.opensearch._types.query_dsl.Query)5 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 ArrayList (java.util.ArrayList)3 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)2 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)2 Result (co.elastic.clients.elasticsearch._types.Result)2 Time (co.elastic.clients.elasticsearch._types.Time)2 BulkResponse (co.elastic.clients.elasticsearch.core.BulkResponse)2 DeleteResponse (co.elastic.clients.elasticsearch.core.DeleteResponse)2 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)2 BulkOperation (co.elastic.clients.elasticsearch.core.bulk.BulkOperation)2 BulkResponseItem (co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem)2 CompletionSuggestOption (co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption)2 Hit (co.elastic.clients.elasticsearch.core.search.Hit)2 Suggester (co.elastic.clients.elasticsearch.core.search.Suggester)2 AnalyzeResponse (co.elastic.clients.elasticsearch.indices.AnalyzeResponse)2