Java Code Examples for org.elasticsearch.index.query.QueryBuilders#constantScoreQuery()
The following examples show how to use
org.elasticsearch.index.query.QueryBuilders#constantScoreQuery() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryClauseInGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRuleNonNullValuesAndNullValue() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(asList("val0", "val1", 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() .minimumShouldMatch(1) .should(QueryBuilders.termsQuery("attr.raw", "val0", "val1")) .should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("attr")))); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 2
Source File: QueryClauseEqualsGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRuleNullValue() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); 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 = queryClauseEqualsGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery( QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("attr"))); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 3
Source File: QueryClauseEqualsGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRuleNonNullIntValues() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(0); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.INT); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseEqualsGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("attr", 0)); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 4
Source File: QueryClauseEqualsGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRuleNonNullValues() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn("val"); 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 = queryClauseEqualsGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("attr.raw", "val")); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 5
Source File: QueryClauseRangeGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRule() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(ImmutableList.of(2, 4)); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.INT); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseRangeGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.rangeQuery("attr").from(2).to(4)); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 6
Source File: QueryClauseGreaterEqualGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRule() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(2); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.INT); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseGreaterEqualGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.rangeQuery("attr").gte(2)); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 7
Source File: QueryClauseGreaterGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRule() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(2); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.INT); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseGreaterGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.rangeQuery("attr").gt(2)); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 8
Source File: QueryClauseLessEqualGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRule() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(2); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.INT); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseLessEqualGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.rangeQuery("attr").lte(2)); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 9
Source File: QueryClauseLessGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRule() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(2); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.INT); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseLessGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.rangeQuery("attr").lt(2)); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 10
Source File: QueryClauseInGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@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); }
Example 11
Source File: QueryClauseInGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRuleNonNullIntValues() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(asList(0, 1)); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.INT); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseInGenerator.mapQueryRule(queryRule, entityType); @SuppressWarnings("RedundantArrayCreation") QueryBuilder expectedQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termsQuery("attr", new int[] {0, 1})); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 12
Source File: QueryClauseInGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRuleNonNullValues() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn(asList("val0", "val1")); 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.termsQuery("attr.raw", "val0", "val1")); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 13
Source File: QueryClauseRangeGenerator.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); validateNumericalQueryField(attr); String fieldName = getQueryFieldName(attributePath); Object queryValue = getQueryValue(attr, queryRule.getValue()); if (queryValue == null) { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } if (!(queryValue instanceof Iterable<?>)) { throw new MolgenisQueryException( format( "Query value must be a Iterable instead of [%s]", queryValue.getClass().getSimpleName())); } Iterator<?> queryValuesIterator = ((Iterable<?>) queryValue).iterator(); Object queryValueFrom = getQueryValue(attr, queryValuesIterator.next()); Object queryValueTo = getQueryValue(attr, queryValuesIterator.next()); return QueryBuilders.constantScoreQuery( nestedQueryBuilder( entityType, attributePath, QueryBuilders.rangeQuery(fieldName).gte(queryValueFrom).lte(queryValueTo))); }
Example 14
Source File: BaseQueryClauseRangeGenerator.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); validateNumericalQueryField(attr); String fieldName = getQueryFieldName(attributePath); Object queryValue = getQueryValue(attr, queryRule.getValue()); if (queryValue == null) { throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); } RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(fieldName); switch (getOperator()) { case GREATER: rangeQueryBuilder.gt(queryValue); break; case GREATER_EQUAL: rangeQueryBuilder.gte(queryValue); break; case LESS: rangeQueryBuilder.lt(queryValue); break; case LESS_EQUAL: rangeQueryBuilder.lte(queryValue); break; default: throw new UnexpectedEnumException(getOperator()); } return QueryBuilders.constantScoreQuery( nestedQueryBuilder(entityType, attributePath, rangeQueryBuilder)); }
Example 15
Source File: ElasticsearchDataStructure.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
private QueryBuilder getQueryBuilder(Resource subject, IRI predicate, Value object, boolean inferred, Resource[] contexts) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); if (subject != null) { boolQueryBuilder.must(QueryBuilders.termQuery("subject", subject.stringValue())); if (subject instanceof IRI) { boolQueryBuilder.must(QueryBuilders.termQuery("subject_IRI", true)); } else { boolQueryBuilder.must(QueryBuilders.termQuery("subject_BNode", true)); } } if (predicate != null) { boolQueryBuilder.must(QueryBuilders.termQuery("predicate", predicate.stringValue())); } if (object != null) { boolQueryBuilder.must(QueryBuilders.termQuery("object_Hash", object.stringValue().hashCode())); if (object instanceof IRI) { boolQueryBuilder.must(QueryBuilders.termQuery("object_IRI", true)); } else if (object instanceof BNode) { boolQueryBuilder.must(QueryBuilders.termQuery("object_BNode", true)); } else { boolQueryBuilder.must( QueryBuilders.termQuery("object_Datatype", ((Literal) object).getDatatype().stringValue())); if (((Literal) object).getLanguage().isPresent()) { boolQueryBuilder .must(QueryBuilders.termQuery("object_Lang", ((Literal) object).getLanguage().get())); } } } if (contexts != null && contexts.length > 0) { BoolQueryBuilder contextQueryBuilder = new BoolQueryBuilder(); for (Resource context : contexts) { if (context == null) { contextQueryBuilder.should(new BoolQueryBuilder().mustNot(QueryBuilders.existsQuery("context"))); } else if (context instanceof IRI) { contextQueryBuilder.should( new BoolQueryBuilder() .must(QueryBuilders.termQuery("context", context.stringValue())) .must(QueryBuilders.termQuery("context_IRI", true))); } else { // BNode contextQueryBuilder.should( new BoolQueryBuilder() .must(QueryBuilders.termQuery("context", context.stringValue())) .must(QueryBuilders.termQuery("context_BNode", true))); } } boolQueryBuilder.must(contextQueryBuilder); } boolQueryBuilder.must(QueryBuilders.termQuery("inferred", inferred)); return QueryBuilders.constantScoreQuery(boolQueryBuilder); }
Example 16
Source File: QueryClauseInGenerator.java From molgenis with GNU Lesser General Public License v3.0 | 4 votes |
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); }
Example 17
Source File: QueryClauseEqualsGenerator.java From molgenis with GNU Lesser General Public License v3.0 | 4 votes |
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType); Attribute attr = attributePath.get(attributePath.size() - 1); QueryBuilder queryBuilder; AttributeType attrType = attr.getDataType(); switch (attrType) { case BOOL: case DATE: case DATE_TIME: case DECIMAL: case EMAIL: case ENUM: case HTML: case HYPERLINK: case INT: case LONG: case SCRIPT: case STRING: case TEXT: String fieldName = getQueryFieldName(attributePath); if (queryRule.getValue() != null) { Object queryValue = getQueryValue(attr, queryRule.getValue()); if (useNotAnalyzedField(attr)) { fieldName = fieldName + '.' + FIELD_NOT_ANALYZED; } queryBuilder = nestedQueryBuilder( entityType, attributePath, QueryBuilders.termQuery(fieldName, queryValue)); } else { queryBuilder = QueryBuilders.boolQuery() .mustNot( nestedQueryBuilder( entityType, attributePath, QueryBuilders.existsQuery(fieldName))); } break; case CATEGORICAL: case CATEGORICAL_MREF: case XREF: case MREF: case FILE: case ONE_TO_MANY: case COMPOUND: throw new MolgenisQueryException(new IllegalAttributeTypeException(attrType)); default: throw new UnexpectedEnumException(attrType); } return QueryBuilders.constantScoreQuery(queryBuilder); }
Example 18
Source File: ElasticSearchQueryGenerator.java From foxtrot with Apache License 2.0 | 4 votes |
public QueryBuilder genFilter(List<Filter> filters) { for(Filter filter : filters) { filter.accept(this); } return QueryBuilders.constantScoreQuery(boolFilterBuilder); }