org.elasticsearch.index.query.NestedQueryBuilder Java Examples

The following examples show how to use org.elasticsearch.index.query.NestedQueryBuilder. 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: NestedQueryParser.java    From elasticsearch-sql with MIT License 5 votes vote down vote up
@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 #2
Source File: DetectorMappingRepositoryImpl.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
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 #3
Source File: DetectorMappingRepositoryImpl.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
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 #4
Source File: NestedQueryDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    NestedQueryBuilder nestedQueryBuilder = QueryBuilders
            .nestedQuery(
                    "keywords",
                    QueryBuilders.termQuery("keywords.keyword","北京"), ScoreMode.None);
}
 
Example #5
Source File: IndexerQueryBuilder.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
private void addAttributeToQueries(List<QueryBuilder> queries, String attributeName, List<SearchQuery.AbstractAttributeQuery> attributeList) {

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        boolQuery.must(QueryBuilders.nestedQuery(IndexerMapping.ATTRIBUTES_KEY,
                QueryBuilders.termQuery(IndexerMapping.ATTRIBUTES_KEY + "." + IndexerMapping.ATTRIBUTE_NAME, attributeName), ScoreMode.None));

        List<NestedQueryBuilder> nestedQueries = new ArrayList<>();
        BoolQueryBuilder valuesQuery = QueryBuilders.boolQuery();

        for (SearchQuery.AbstractAttributeQuery attr : attributeList) {
            String attributeValue = attr.toString();
            if (attributeValue != null && !attributeValue.isEmpty()) {
                nestedQueries.add(QueryBuilders.nestedQuery(IndexerMapping.ATTRIBUTES_KEY,
                        QueryBuilders.termQuery(IndexerMapping.ATTRIBUTES_KEY + "." + IndexerMapping.ATTRIBUTE_VALUE, attributeValue), ScoreMode.None));

            }
        }

        // Use 'should' on same attribute name, and 'must' for different attribute names
        // Only request for attribute name if no values
        // Use bool must if only one value passed
        // Compound should queries if many values (but must not be empty)
        if (!nestedQueries.isEmpty()) {
            if (nestedQueries.size() == 1) {
                boolQuery.must(nestedQueries.get(0));
            } else {
                nestedQueries.forEach(valuesQuery::should);
                boolQuery.must(valuesQuery);
                boolQuery.mustNot(QueryBuilders.nestedQuery(IndexerMapping.ATTRIBUTES_KEY,
                        QueryBuilders.termQuery(IndexerMapping.ATTRIBUTES_KEY + "." + IndexerMapping.ATTRIBUTE_VALUE, ""), ScoreMode.None));
            }
        }

        queries.add(boolQuery);

    }
 
Example #6
Source File: QueryMaker.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
/**
 * 增加嵌套插
 * 
 * @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 #7
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}