Java Code Examples for org.elasticsearch.index.query.QueryBuilders#nestedQuery()
The following examples show how to use
org.elasticsearch.index.query.QueryBuilders#nestedQuery() .
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: QueryClauseSearchGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 7 votes |
@Test void mapQueryRuleAttributeSearchReferencedAttribute() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn("val"); Attribute refIdAttribute = mock(Attribute.class); when(refIdAttribute.getDataType()).thenReturn(STRING); EntityType refEntityType = mock(EntityType.class); when(refEntityType.getIdAttribute()).thenReturn(refIdAttribute); when(documentIdGenerator.generateId(refIdAttribute)).thenReturn("refAttr"); Attribute attribute = mock(Attribute.class); when(attribute.hasRefEntity()).thenReturn(true); when(attribute.getRefEntity()).thenReturn(refEntityType); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getIndexingDepth()).thenReturn(1); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseSearchGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.nestedQuery( "attr", QueryBuilders.matchQuery("attr.refAttr", "val"), ScoreMode.Avg); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 2
Source File: BaseQueryClauseGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void testNestedQueryBuilderSizeTwo() { EntityType entityType = when(mock(EntityType.class).getIndexingDepth()).thenReturn(1).getMock(); String parentFieldName = "parent"; String childFieldName = "child"; String queryValue = "value"; QueryBuilder queryBuilder = termQuery(parentFieldName + '.' + childFieldName, queryValue); Attribute parentAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(parentAttribute)).thenReturn(parentFieldName); Attribute childAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(childAttribute)).thenReturn(childFieldName); QueryBuilder nestedQueryBuilder = queryGenerator.nestedQueryBuilder( entityType, asList(parentAttribute, childAttribute), queryBuilder); QueryBuilder expectedQueryBuilder = QueryBuilders.nestedQuery( parentFieldName, termQuery(parentFieldName + '.' + childFieldName, queryValue), ScoreMode.Avg); assertQueryBuilderEquals(nestedQueryBuilder, expectedQueryBuilder); }
Example 3
Source File: QueryClauseSearchQueryGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
@Test void mapQueryRuleAttributeSearchReferencedAttribute() { when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn("val"); when(entityType.getIndexingDepth()).thenReturn(1); when(entityType.getAttributeByName("attr")).thenReturn(attribute); when(attribute.hasRefEntity()).thenReturn(true); when(attribute.getRefEntity()).thenReturn(refEntityType); when(refIdAttribute.getDataType()).thenReturn(STRING); when(refEntityType.getIdAttribute()).thenReturn(refIdAttribute); when(documentIdGenerator.generateId(refIdAttribute)).thenReturn("refAttr"); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); QueryBuilder queryBuilder = searchQueryGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.nestedQuery( "attr", simpleQueryStringQuery("val").field("attr.refAttr").defaultOperator(AND), ScoreMode.Avg); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
Example 4
Source File: NestedQueryParser.java From elasticsearch-sql with MIT License | 5 votes |
@Override public AtomicQuery parse(ElasticsearchParser.NestedContext expression) { String nestedPath=expression.nestedClause().nestedPath.getText(); BoolExpressionParser boolExpressionParser=new BoolExpressionParser(); QueryBuilder queryBuilder = boolExpressionParser.parseBoolQueryExpr(expression.nestedClause().query); NestedQueryBuilder nestedQueryBuilder= QueryBuilders.nestedQuery(nestedPath,queryBuilder, ScoreMode.Avg); return new AtomicQuery(nestedQueryBuilder); }
Example 5
Source File: DetectorMappingRepositoryImpl.java From adaptive-alerting with Apache License 2.0 | 5 votes |
private NestedQueryBuilder detectorIdQuery(SearchMappingsRequest searchMappingsRequest) { return QueryBuilders.nestedQuery(PercolatorDetectorMapping.DETECTOR_KEYWORD, QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery( PercolatorDetectorMapping.DETECTOR_KEYWORD + "." + PercolatorDetectorMapping.DETECTOR_ID_KEYWORD, searchMappingsRequest.getDetectorUuid().toString())), ScoreMode.None); }
Example 6
Source File: DetectorMappingRepositoryImpl.java From adaptive-alerting with Apache License 2.0 | 5 votes |
private NestedQueryBuilder userIdQuery(SearchMappingsRequest searchMappingsRequest) { return QueryBuilders.nestedQuery(PercolatorDetectorMapping.USER_KEYWORD, QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery( PercolatorDetectorMapping.USER_KEYWORD + "." + PercolatorDetectorMapping.USER_ID_KEYWORD, searchMappingsRequest.getUserId())), ScoreMode.None); }
Example 7
Source File: NestedQueryDemo.java From elasticsearch-full with Apache License 2.0 | 5 votes |
@Test public void test() throws Exception { NestedQueryBuilder nestedQueryBuilder = QueryBuilders .nestedQuery( "keywords", QueryBuilders.termQuery("keywords.keyword","北京"), ScoreMode.None); }
Example 8
Source File: BaseQueryClauseGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
@Test void testNestedQueryBuilderSizeThree() { EntityType entityType = when(mock(EntityType.class).getIndexingDepth()).thenReturn(2).getMock(); String grandparentFieldName = "grandparent"; String parentFieldName = "parent"; String childFieldName = "child"; String queryValue = "value"; QueryBuilder queryBuilder = termQuery(grandparentFieldName + '.' + parentFieldName + '.' + childFieldName, queryValue); Attribute grandparentAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(grandparentAttribute)).thenReturn(grandparentFieldName); Attribute parentAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(parentAttribute)).thenReturn(parentFieldName); Attribute childAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(childAttribute)).thenReturn(childFieldName); QueryBuilder nestedQueryBuilder = queryGenerator.nestedQueryBuilder( entityType, asList(grandparentAttribute, parentAttribute, childAttribute), queryBuilder); QueryBuilder expectedQueryBuilder = QueryBuilders.nestedQuery( grandparentFieldName, QueryBuilders.nestedQuery( grandparentFieldName + '.' + parentFieldName, termQuery( grandparentFieldName + '.' + parentFieldName + '.' + childFieldName, queryValue), ScoreMode.Avg), ScoreMode.Avg); assertQueryBuilderEquals(nestedQueryBuilder, expectedQueryBuilder); }
Example 9
Source File: QueryGeneratorReferencesIT.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
@Test void generateOneQueryRuleLikeCompoundPartString() { String value = "value"; Query<Entity> q = new QueryImpl<>().like(PREFIX + refCompoundPart0AttributeName, value); QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType); QueryBuilder expectedQuery = QueryBuilders.nestedQuery( REF_ENTITY_ATT, QueryBuilders.matchPhrasePrefixQuery(PREFIX + refCompoundPart0AttributeName, value) .slop(10) .analyzer(DEFAULT_ANALYZER), ScoreMode.Avg); assertQueryBuilderEquals(expectedQuery, query); }
Example 10
Source File: SearchConditionBuilder.java From search-spring-boot-starter with Apache License 2.0 | 4 votes |
/** * 根据查询条件,构建基础查询 * * @param condition 查询条件 * @return 基础查询 */ public QueryBuilder builder(SearchCondition condition, IndexHelper helper) { String fieldName = condition.getFieldName(); final Object singleValue = condition.getSingleValue(); final boolean multipleValue = condition.getMultipleValue(); final ConditionExpressionEnum expression = condition.getConditionExpression(); QueryBuilder queryBuilder; switch (expression) { case EQUAL: fieldName = getFieldName(helper, fieldName, singleValue); queryBuilder = QueryBuilders.termQuery(fieldName, singleValue); break; case LESSER: queryBuilder = QueryBuilders.rangeQuery(fieldName).lt(singleValue); break; case GREATER: queryBuilder = QueryBuilders.rangeQuery(fieldName).gt(singleValue); break; case LESSER_OR_EQUAL: queryBuilder = QueryBuilders.rangeQuery(fieldName).lte(singleValue); break; case GREATER_OR_EQUAL: queryBuilder = QueryBuilders.rangeQuery(fieldName).gte(singleValue); break; case UNEQUAL: fieldName = getFieldName(helper, fieldName, singleValue); queryBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(fieldName, singleValue)); break; case LIKE: fieldName = getFieldName(helper, fieldName, singleValue); queryBuilder = QueryBuilders.wildcardQuery(fieldName, "*" + singleValue + "*"); break; case NULL: queryBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(fieldName)); break; case NOT_NULL: queryBuilder = QueryBuilders.existsQuery(fieldName); break; case IN: fieldName = getFieldName(helper, fieldName, condition.getFieldValues()); queryBuilder = QueryBuilders.termsQuery(fieldName, condition.getFieldValues()); break; case NOT_IN: fieldName = getFieldName(helper, fieldName, condition.getFieldValues()); queryBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery(fieldName, condition.getFieldValues())); break; case BETWEEN: queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.rangeQuery(fieldName).gt(condition.getMinValue()).lt(condition.getMaxValue())); break; case BETWEEN_AND: queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.rangeQuery(fieldName).gte(condition.getMinValue()).lte(condition.getMaxValue())); break; case BETWEEN_LEFT: queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.rangeQuery(fieldName).gte(condition.getMinValue()).lt(condition.getMaxValue())); break; case BETWEEN_RIGHR: queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.rangeQuery(fieldName).gt(condition.getMinValue()).lte(condition.getMaxValue())); break; case MATCH: queryBuilder = QueryBuilders.matchQuery(fieldName, singleValue); break; default: throw new RuntimeException("表达不存在"); } // 若是多值字段,需使用nestedQuery保证查询结果的准确性 if (multipleValue) { if (fieldName.contains(".")) { fieldName = fieldName.substring(0, fieldName.lastIndexOf(".")); } queryBuilder = QueryBuilders.nestedQuery(fieldName, queryBuilder, ScoreMode.None); } return queryBuilder; }
Example 11
Source File: WhereParser.java From sql4es with Apache License 2.0 | 4 votes |
public QueryBuilder parse(Expression node, QueryState state){ QueryWrapper qw = this.visitExpression(node, state); if(qw == null) return null; if(qw.getNestField() != null) return QueryBuilders.nestedQuery(qw.getNestField(), qw.getQuery(), ScoreMode.None); else return qw.getQuery(); }
Example 12
Source File: QueryMaker.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
/** * 增加嵌套插 * * @param boolQuery * @param where * @param subQuery */ private void addSubQuery(BoolQueryBuilder boolQuery, Where where, QueryBuilder subQuery) { if(where instanceof Condition){ Condition condition = (Condition) where; if (condition.isNested()) { boolean isNestedQuery = subQuery instanceof NestedQueryBuilder; InnerHitBuilder ihb = null; if (condition.getInnerHits() != null) { try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, condition.getInnerHits())) { ihb = InnerHitBuilder.fromXContent(parser); } catch (IOException e) { throw new IllegalArgumentException("couldn't parse inner_hits: " + e.getMessage(), e); } } // bugfix #628 if ("missing".equalsIgnoreCase(String.valueOf(condition.getValue())) && (condition.getOpear() == Condition.OPEAR.IS || condition.getOpear() == Condition.OPEAR.EQ)) { NestedQueryBuilder q = isNestedQuery ? (NestedQueryBuilder) subQuery : QueryBuilders.nestedQuery(condition.getNestedPath(), QueryBuilders.boolQuery().mustNot(subQuery), ScoreMode.None); if (ihb != null) { q.innerHit(ihb); } boolQuery.mustNot(q); return; } // support not nested if (condition.getOpear() == Condition.OPEAR.NNESTED_COMPLEX) { if (ihb != null) { NestedQueryBuilder.class.cast(subQuery).innerHit(ihb); } boolQuery.mustNot(subQuery); return; } if (!isNestedQuery) { subQuery = QueryBuilders.nestedQuery(condition.getNestedPath(), subQuery, ScoreMode.None); } if (ihb != null) { ((NestedQueryBuilder) subQuery).innerHit(ihb); } } else if(condition.isChildren()) { subQuery = JoinQueryBuilders.hasChildQuery(condition.getChildType(), subQuery, ScoreMode.None); } } //zhongshu-comment 将subQuery对象纳入到boolQuery中,即boolQuery是上一级,subQuery是下一级 if (where.getConn() == CONN.AND) { boolQuery.must(subQuery); } else { boolQuery.should(subQuery); } }
Example 13
Source File: QueryClauseFuzzyMatchNgramGenerator.java From molgenis with GNU Lesser General Public License v3.0 | 4 votes |
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { String queryField = queryRule.getField(); Object queryValue = queryRule.getValue(); QueryBuilder queryBuilder; if (queryValue == null) throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); if (queryField == null) { queryBuilder = QueryBuilders.matchQuery("_all", queryValue); } else { Attribute attr = entityType.getAttributeByName(queryField); // construct query part AttributeType dataType = attr.getDataType(); switch (dataType) { 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: queryField = getQueryFieldName(attr) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"); break; case MREF: case XREF: queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.nestedQuery( getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max); break; default: throw new UnexpectedEnumException(dataType); } } return queryBuilder; }
Example 14
Source File: BaseQueryClauseGeneratorTest.java From molgenis with GNU Lesser General Public License v3.0 | 4 votes |
@Test void testNestedQueryBuilderSizeFour() { EntityType entityType = when(mock(EntityType.class).getIndexingDepth()).thenReturn(3).getMock(); String greatGrandparentFieldName = "grandGrandparent"; String grandparentFieldName = "grandparent"; String parentFieldName = "parent"; String childFieldName = "child"; String queryValue = "value"; QueryBuilder queryBuilder = termQuery( greatGrandparentFieldName + '.' + grandparentFieldName + '.' + parentFieldName + '.' + childFieldName, queryValue); Attribute greatGrandparentAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(greatGrandparentAttribute)) .thenReturn(greatGrandparentFieldName); Attribute grandparentAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(grandparentAttribute)).thenReturn(grandparentFieldName); Attribute parentAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(parentAttribute)).thenReturn(parentFieldName); Attribute childAttribute = mock(Attribute.class); when(documentIdGenerator.generateId(childAttribute)).thenReturn(childFieldName); QueryBuilder nestedQueryBuilder = queryGenerator.nestedQueryBuilder( entityType, asList( greatGrandparentAttribute, grandparentAttribute, parentAttribute, childAttribute), queryBuilder); QueryBuilder expectedQueryBuilder = QueryBuilders.nestedQuery( greatGrandparentFieldName, QueryBuilders.nestedQuery( greatGrandparentFieldName + '.' + grandparentFieldName, QueryBuilders.nestedQuery( greatGrandparentFieldName + '.' + grandparentFieldName + '.' + parentFieldName, termQuery( greatGrandparentFieldName + '.' + grandparentFieldName + '.' + parentFieldName + '.' + childFieldName, queryValue), ScoreMode.Avg), ScoreMode.Avg), ScoreMode.Avg); assertQueryBuilderEquals(nestedQueryBuilder, expectedQueryBuilder); }
Example 15
Source File: EsQueryVistor.java From usergrid with Apache License 2.0 | 3 votes |
/** * Generate the field name term for the field name for queries */ private NestedQueryBuilder fieldNameTerm( final String fieldName, final QueryBuilder fieldValueQuery ) { final BoolQueryBuilder booleanQuery = QueryBuilders.boolQuery(); booleanQuery.must( QueryBuilders.termQuery(IndexingUtils.FIELD_NAME_NESTED, fieldName) ); booleanQuery.must( fieldValueQuery ); return QueryBuilders.nestedQuery(IndexingUtils.ENTITY_FIELDS, booleanQuery); }