org.elasticsearch.join.query.JoinQueryBuilders Java Examples

The following examples show how to use org.elasticsearch.join.query.JoinQueryBuilders. 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: HasParentQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
@Override
public AtomicQuery parse(ElasticsearchParser.HasParentClauseContext expression) {
    String type=expression.type.getText();
    BoolExpressionParser   boolExpressionParser=new BoolExpressionParser();
    QueryBuilder queryBuilder=boolExpressionParser.parseBoolQueryExpr(expression.query);
    HasParentQueryBuilder hasParentQueryBuilder= JoinQueryBuilders.hasParentQuery(type,queryBuilder,true);
    AtomicQuery atomicQuery= new AtomicQuery(hasParentQueryBuilder);
    atomicQuery.getHighlighter().addAll(boolExpressionParser.highlighter);
    return atomicQuery;
}
 
Example #2
Source File: SearchService.java    From jakduk-api with MIT License 6 votes vote down vote up
private SearchRequestBuilder getCommentSearchRequestBuilder(String query, Integer from, Integer size, String preTags,
															String postTags) {

	HighlightBuilder highlightBuilder = new HighlightBuilder()
			.noMatchSize(Constants.SEARCH_NO_MATCH_SIZE)
			.fragmentSize(Constants.SEARCH_FRAGMENT_SIZE)
			.field("content", Constants.SEARCH_FRAGMENT_SIZE, 1);

	SearchRequestBuilder searchRequestBuilder = client.prepareSearch()
			.setIndices(elasticsearchProperties.getIndexBoard())
			.setTypes(Constants.ES_TYPE_COMMENT)
			.setFetchSource(null, new String[]{"content"})
			.setQuery(
					QueryBuilders.boolQuery()
							.must(QueryBuilders.matchQuery("content", query))
							.must(JoinQueryBuilders
                                       .hasParentQuery(Constants.ES_TYPE_ARTICLE, QueryBuilders.matchAllQuery(), false)
                                       .innerHit(new InnerHitBuilder())
							)
			)
			.setFrom(from)
			.setSize(size);

	if (StringUtils.isNotBlank(preTags))
		highlightBuilder.preTags(preTags);

	if (StringUtils.isNotBlank(postTags))
		highlightBuilder.postTags(postTags);

	searchRequestBuilder.highlighter(highlightBuilder);
	log.debug("getBoardCommentSearchRequestBuilder Query:\n{}", searchRequestBuilder);

	return searchRequestBuilder;
}
 
Example #3
Source File: HasChildQueryParser.java    From elasticsearch-sql with MIT License 5 votes vote down vote up
@Override
public AtomicQuery parse(ElasticsearchParser.HasChildClauseContext expression) {
    String type = expression.type.getText();
    BoolExpressionParser  boolExpressionParser=new BoolExpressionParser();
    QueryBuilder queryBuilder = boolExpressionParser.parseBoolQueryExpr(expression.query);
    QueryBuilder hasChildQueryBuilder=JoinQueryBuilders.hasChildQuery(type,queryBuilder, ScoreMode.Avg);
    AtomicQuery atomicQuery= new AtomicQuery(hasChildQueryBuilder);
    atomicQuery.getHighlighter().addAll(boolExpressionParser.highlighter);
    return atomicQuery;
}
 
Example #4
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);
	}
}