@Test void mapQueryRuleNullValue() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(singletonList(null)); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.STRING); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseInGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery( QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("attr"))); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); String fieldName = getQueryFieldName(attributePath); List<Object> queryValues = getQueryValues(attr, queryRule.getValue()); List<Object> queryValuesWithoutNulls = queryValues.stream().filter(Objects::nonNull).collect(toList()); Optional<QueryBuilder> nonNullQueryBuilder; if (!queryValuesWithoutNulls.isEmpty()) { nonNullQueryBuilder = createNonNullValuesQueryBuilder( entityType, attributePath, attr, fieldName, queryValuesWithoutNulls); } else { nonNullQueryBuilder = Optional.empty(); } Optional<QueryBuilder> nullValueQueryBuilder; if (queryValuesWithoutNulls.size() < queryValues.size()) { nullValueQueryBuilder = createNullValuesQueryBuilder(entityType, attributePath, fieldName); } else { nullValueQueryBuilder = Optional.empty(); } QueryBuilder queryBuilder; if (nonNullQueryBuilder.isPresent()) { if (nullValueQueryBuilder.isPresent()) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(1); boolQueryBuilder.should(nonNullQueryBuilder.get()); boolQueryBuilder.should(nullValueQueryBuilder.get()); queryBuilder = boolQueryBuilder; } else { queryBuilder = nonNullQueryBuilder.get(); } } else { if (nullValueQueryBuilder.isPresent()) { queryBuilder = nullValueQueryBuilder.get(); } else { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } } return QueryBuilders.constantScoreQuery(queryBuilder); }
QueryClauseInGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); String fieldName = getQueryFieldName(attributePath); List<Object> queryValues = getQueryValues(attr, queryRule.getValue()); List<Object> queryValuesWithoutNulls = queryValues.stream().filter(Objects::nonNull).collect(toList()); Optional<QueryBuilder> nonNullQueryBuilder; if (!queryValuesWithoutNulls.isEmpty()) { nonNullQueryBuilder = createNonNullValuesQueryBuilder( entityType, attributePath, attr, fieldName, queryValuesWithoutNulls); } else { nonNullQueryBuilder = Optional.empty(); } Optional<QueryBuilder> nullValueQueryBuilder; if (queryValuesWithoutNulls.size() < queryValues.size()) { nullValueQueryBuilder = createNullValuesQueryBuilder(entityType, attributePath, fieldName); } else { nullValueQueryBuilder = Optional.empty(); } QueryBuilder queryBuilder; if (nonNullQueryBuilder.isPresent()) { if (nullValueQueryBuilder.isPresent()) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(1); boolQueryBuilder.should(nonNullQueryBuilder.get()); boolQueryBuilder.should(nullValueQueryBuilder.get()); queryBuilder = boolQueryBuilder; } else { queryBuilder = nonNullQueryBuilder.get(); } } else { if (nullValueQueryBuilder.isPresent()) { queryBuilder = nullValueQueryBuilder.get(); } else { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } } return QueryBuilders.constantScoreQuery(queryBuilder); } }
QueryClauseInGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); String fieldName = getQueryFieldName(attributePath); List<Object> queryValues = getQueryValues(attr, queryRule.getValue()); List<Object> queryValuesWithoutNulls = queryValues.stream().filter(Objects::nonNull).collect(toList()); Optional<QueryBuilder> nonNullQueryBuilder; if (!queryValuesWithoutNulls.isEmpty()) { nonNullQueryBuilder = createNonNullValuesQueryBuilder( entityType, attributePath, attr, fieldName, queryValuesWithoutNulls); } else { nonNullQueryBuilder = Optional.empty(); } Optional<QueryBuilder> nullValueQueryBuilder; if (queryValuesWithoutNulls.size() < queryValues.size()) { nullValueQueryBuilder = createNullValuesQueryBuilder(entityType, attributePath, fieldName); } else { nullValueQueryBuilder = Optional.empty(); } QueryBuilder queryBuilder; if (nonNullQueryBuilder.isPresent()) { if (nullValueQueryBuilder.isPresent()) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(1); boolQueryBuilder.should(nonNullQueryBuilder.get()); boolQueryBuilder.should(nullValueQueryBuilder.get()); queryBuilder = boolQueryBuilder; } else { queryBuilder = nonNullQueryBuilder.get(); } } else { if (nullValueQueryBuilder.isPresent()) { queryBuilder = nullValueQueryBuilder.get(); } else { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } } return QueryBuilders.constantScoreQuery(queryBuilder); } QueryClauseInGenerator(DocumentIdGenerator documentIdGenerator); }
QueryClauseInGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); String fieldName = getQueryFieldName(attributePath); List<Object> queryValues = getQueryValues(attr, queryRule.getValue()); List<Object> queryValuesWithoutNulls = queryValues.stream().filter(Objects::nonNull).collect(toList()); Optional<QueryBuilder> nonNullQueryBuilder; if (!queryValuesWithoutNulls.isEmpty()) { nonNullQueryBuilder = createNonNullValuesQueryBuilder( entityType, attributePath, attr, fieldName, queryValuesWithoutNulls); } else { nonNullQueryBuilder = Optional.empty(); } Optional<QueryBuilder> nullValueQueryBuilder; if (queryValuesWithoutNulls.size() < queryValues.size()) { nullValueQueryBuilder = createNullValuesQueryBuilder(entityType, attributePath, fieldName); } else { nullValueQueryBuilder = Optional.empty(); } QueryBuilder queryBuilder; if (nonNullQueryBuilder.isPresent()) { if (nullValueQueryBuilder.isPresent()) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(1); boolQueryBuilder.should(nonNullQueryBuilder.get()); boolQueryBuilder.should(nullValueQueryBuilder.get()); queryBuilder = boolQueryBuilder; } else { queryBuilder = nonNullQueryBuilder.get(); } } else { if (nullValueQueryBuilder.isPresent()) { queryBuilder = nullValueQueryBuilder.get(); } else { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } } return QueryBuilders.constantScoreQuery(queryBuilder); } QueryClauseInGenerator(DocumentIdGenerator documentIdGenerator);  }
QueryClauseInGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); String fieldName = getQueryFieldName(attributePath); List<Object> queryValues = getQueryValues(attr, queryRule.getValue()); List<Object> queryValuesWithoutNulls = queryValues.stream().filter(Objects::nonNull).collect(toList()); Optional<QueryBuilder> nonNullQueryBuilder; if (!queryValuesWithoutNulls.isEmpty()) { nonNullQueryBuilder = createNonNullValuesQueryBuilder( entityType, attributePath, attr, fieldName, queryValuesWithoutNulls); } else { nonNullQueryBuilder = Optional.empty(); } Optional<QueryBuilder> nullValueQueryBuilder; if (queryValuesWithoutNulls.size() < queryValues.size()) { nullValueQueryBuilder = createNullValuesQueryBuilder(entityType, attributePath, fieldName); } else { nullValueQueryBuilder = Optional.empty(); } QueryBuilder queryBuilder; if (nonNullQueryBuilder.isPresent()) { if (nullValueQueryBuilder.isPresent()) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(1); boolQueryBuilder.should(nonNullQueryBuilder.get()); boolQueryBuilder.should(nullValueQueryBuilder.get()); queryBuilder = boolQueryBuilder; } else { queryBuilder = nonNullQueryBuilder.get(); } } else { if (nullValueQueryBuilder.isPresent()) { queryBuilder = nullValueQueryBuilder.get(); } else { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } } return QueryBuilders.constantScoreQuery(queryBuilder); } QueryClauseInGenerator(DocumentIdGenerator documentIdGenerator);   }