@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); }
QueryBuilder nestedQueryBuilder( EntityType entityType, List<Attribute> attributePath, QueryBuilder queryBuilder) { validateIndexingDepth(entityType, attributePath); QueryBuilder nestedQueryBuilder = queryBuilder; if (attributePath.size() > 1) { List<String> fieldNamePath = attributePath.stream().map(this::getQueryFieldName).collect(toList()); for (int i = attributePath.size() - 1; i > 0; --i) { String path = String.join(".", fieldNamePath.subList(0, i)); nestedQueryBuilder = QueryBuilders.nestedQuery(path, nestedQueryBuilder, ScoreMode.Avg); } } return nestedQueryBuilder; }
BaseQueryClauseGenerator implements QueryClauseGenerator { QueryBuilder nestedQueryBuilder( EntityType entityType, List<Attribute> attributePath, QueryBuilder queryBuilder) { validateIndexingDepth(entityType, attributePath); QueryBuilder nestedQueryBuilder = queryBuilder; if (attributePath.size() > 1) { List<String> fieldNamePath = attributePath.stream().map(this::getQueryFieldName).collect(toList()); for (int i = attributePath.size() - 1; i > 0; --i) { String path = String.join(".", fieldNamePath.subList(0, i)); nestedQueryBuilder = QueryBuilders.nestedQuery(path, nestedQueryBuilder, ScoreMode.Avg); } } return nestedQueryBuilder; } }
BaseQueryClauseGenerator implements QueryClauseGenerator { QueryBuilder nestedQueryBuilder( EntityType entityType, List<Attribute> attributePath, QueryBuilder queryBuilder) { validateIndexingDepth(entityType, attributePath); QueryBuilder nestedQueryBuilder = queryBuilder; if (attributePath.size() > 1) { List<String> fieldNamePath = attributePath.stream().map(this::getQueryFieldName).collect(toList()); for (int i = attributePath.size() - 1; i > 0; --i) { String path = String.join(".", fieldNamePath.subList(0, i)); nestedQueryBuilder = QueryBuilders.nestedQuery(path, nestedQueryBuilder, ScoreMode.Avg); } } return nestedQueryBuilder; } BaseQueryClauseGenerator(DocumentIdGenerator documentIdGenerator, Operator operator); }
BaseQueryClauseGenerator implements QueryClauseGenerator { QueryBuilder nestedQueryBuilder( EntityType entityType, List<Attribute> attributePath, QueryBuilder queryBuilder) { validateIndexingDepth(entityType, attributePath); QueryBuilder nestedQueryBuilder = queryBuilder; if (attributePath.size() > 1) { List<String> fieldNamePath = attributePath.stream().map(this::getQueryFieldName).collect(toList()); for (int i = attributePath.size() - 1; i > 0; --i) { String path = String.join(".", fieldNamePath.subList(0, i)); nestedQueryBuilder = QueryBuilders.nestedQuery(path, nestedQueryBuilder, ScoreMode.Avg); } } return nestedQueryBuilder; } BaseQueryClauseGenerator(DocumentIdGenerator documentIdGenerator, Operator operator); @Override Operator getOperator(); @Override QueryBuilder createQueryClause(QueryRule queryRule, EntityType entityType); }
BaseQueryClauseGenerator implements QueryClauseGenerator { QueryBuilder nestedQueryBuilder( EntityType entityType, List<Attribute> attributePath, QueryBuilder queryBuilder) { validateIndexingDepth(entityType, attributePath); QueryBuilder nestedQueryBuilder = queryBuilder; if (attributePath.size() > 1) { List<String> fieldNamePath = attributePath.stream().map(this::getQueryFieldName).collect(toList()); for (int i = attributePath.size() - 1; i > 0; --i) { String path = String.join(".", fieldNamePath.subList(0, i)); nestedQueryBuilder = QueryBuilders.nestedQuery(path, nestedQueryBuilder, ScoreMode.Avg); } } return nestedQueryBuilder; } BaseQueryClauseGenerator(DocumentIdGenerator documentIdGenerator, Operator operator); @Override Operator getOperator(); @Override QueryBuilder createQueryClause(QueryRule queryRule, EntityType entityType);  }