org.elasticsearch.index.query.InnerHitBuilder Java Examples

The following examples show how to use org.elasticsearch.index.query.InnerHitBuilder. 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: ElasticsearchMetaAlertUpdateDao.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * Given an alert GUID, retrieve all associated meta alerts.
 * @param alertGuid The GUID of the child alert
 * @return The Elasticsearch response containing the meta alerts
 */
protected SearchResponse getMetaAlertsForAlert(String alertGuid) throws IOException {
  QueryBuilder qb = boolQuery()
      .must(
          nestedQuery(
              MetaAlertConstants.ALERT_FIELD,
              boolQuery()
                  .must(termQuery(MetaAlertConstants.ALERT_FIELD + "." + Constants.GUID,
                      alertGuid)),
              ScoreMode.None
          ).innerHit(new InnerHitBuilder())
      )
      .must(termQuery(MetaAlertConstants.STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString()));
  return ElasticsearchUtils
      .queryAllResults(elasticsearchDao.getClient().getHighLevelClient(), qb, getConfig().getMetaAlertIndex(),
          pageSize);
}
 
Example #2
Source File: ElasticsearchMetaAlertSearchDao.java    From metron with Apache License 2.0 6 votes vote down vote up
@Override
public SearchResponse getAllMetaAlertsForAlert(String guid) throws InvalidSearchException, IOException {
  if (guid == null || guid.trim().isEmpty()) {
    throw new InvalidSearchException("Guid cannot be empty");
  }
  // Searches for all alerts containing the meta alert guid in it's "metalerts" array
  QueryBuilder qb = boolQuery()
      .must(
          nestedQuery(
              MetaAlertConstants.ALERT_FIELD,
              boolQuery()
                  .must(termQuery(MetaAlertConstants.ALERT_FIELD + "." + GUID, guid)),
              ScoreMode.None
          ).innerHit(new InnerHitBuilder())
      )
      .must(termQuery(MetaAlertConstants.STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString()));
  return queryAllResults(elasticsearchDao.getClient().getHighLevelClient(), qb, config.getMetaAlertIndex(),
      pageSize);
}
 
Example #3
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 #4
Source File: FessEsClient.java    From fess with Apache License 2.0 5 votes vote down vote up
protected CollapseBuilder getCollapseBuilder(final FessConfig fessConfig) {
    final InnerHitBuilder innerHitBuilder =
            new InnerHitBuilder().setName(fessConfig.getQueryCollapseInnerHitsName()).setSize(
                    fessConfig.getQueryCollapseInnerHitsSizeAsInteger());
    fessConfig.getQueryCollapseInnerHitsSortBuilders().ifPresent(
            builders -> stream(builders).of(stream -> stream.forEach(innerHitBuilder::addSort)));
    return new CollapseBuilder(fessConfig.getIndexFieldContentMinhashBits()).setMaxConcurrentGroupRequests(
            fessConfig.getQueryCollapseMaxConcurrentGroupResultsAsInteger()).setInnerHits(innerHitBuilder);
}
 
Example #5
Source File: LoggingIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
public void testLog() throws Exception {
    prepareModels();
    Map<String, Doc> docs = buildIndex();

    Map<String, Object> params = new HashMap<>();
    params.put("query", "found");
    List<String> idsColl = new ArrayList<>(docs.keySet());
    Collections.shuffle(idsColl, random());
    String[] ids = idsColl.subList(0, TestUtil.nextInt(random(), 5, 15)).toArray(new String[0]);
    StoredLtrQueryBuilder sbuilder = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test")
            .boost(random().nextInt(3));

    StoredLtrQueryBuilder sbuilder_rescore = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test_rescore")
            .boost(random().nextInt(3));

    QueryBuilder query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHits(docs, resp);
    sbuilder.featureSetName(null);
    sbuilder.modelName("my_model");
    sbuilder.boost(random().nextInt(3));
    sbuilder_rescore.featureSetName(null);
    sbuilder_rescore.modelName("my_model");
    sbuilder_rescore.boost(random().nextInt(3));

    query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp2 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHits(docs, resp2);

    query = QueryBuilders.boolQuery()
            .must(new WrapperQueryBuilder(sbuilder.toString()))
            .must(
                QueryBuilders.nestedQuery(
                    "nesteddocs1",
                    QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("nesteddocs1.field1", "nestedvalue")),
                    ScoreMode.None
                ).innerHit(new InnerHitBuilder())
    );
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));
    SearchResponse resp3 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHits(docs, resp3);
}
 
Example #6
Source File: LoggingIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
public void testLogExtraLogging() throws Exception {
    prepareModelsExtraLogging();
    Map<String, Doc> docs = buildIndex();

    Map<String, Object> params = new HashMap<>();
    params.put("query", "found");
    List<String> idsColl = new ArrayList<>(docs.keySet());
    Collections.shuffle(idsColl, random());
    String[] ids = idsColl.subList(0, TestUtil.nextInt(random(), 5, 15)).toArray(new String[0]);
    StoredLtrQueryBuilder sbuilder = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test")
            .boost(random().nextInt(3));

    StoredLtrQueryBuilder sbuilder_rescore = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test_rescore")
            .boost(random().nextInt(3));

    QueryBuilder query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHitsExtraLogging(docs, resp);
    sbuilder.featureSetName(null);
    sbuilder.modelName("my_model");
    sbuilder.boost(random().nextInt(3));
    sbuilder_rescore.featureSetName(null);
    sbuilder_rescore.modelName("my_model");
    sbuilder_rescore.boost(random().nextInt(3));

    query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp2 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHitsExtraLogging(docs, resp2);

    query = QueryBuilders.boolQuery()
            .must(new WrapperQueryBuilder(sbuilder.toString()))
            .must(
                    QueryBuilders.nestedQuery(
                            "nesteddocs1",
                            QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("nesteddocs1.field1", "nestedvalue")),
                            ScoreMode.None
                    ).innerHit(new InnerHitBuilder())
            );
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));
    SearchResponse resp3 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHitsExtraLogging(docs, resp3);
}
 
Example #7
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);
	}
}