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