コード例 #1
0
  private SearchRequestBuilder prepareSearch(Query query) {
    Assert.notNull(query.getIndices(), "No index defined for Query");
    Assert.notNull(query.getTypes(), "No type defined for Query");

    int startRecord = 0;
    SearchRequestBuilder searchRequestBuilder =
        client
            .prepareSearch(toArray(query.getIndices()))
            .setSearchType(query.getSearchType())
            .setTypes(toArray(query.getTypes()));

    if (query.getPageable() != null) {
      startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
      searchRequestBuilder.setSize(query.getPageable().getPageSize());
    }
    searchRequestBuilder.setFrom(startRecord);

    if (!query.getFields().isEmpty()) {
      searchRequestBuilder.addFields(toArray(query.getFields()));
    }

    if (query.getSort() != null) {
      for (Sort.Order order : query.getSort()) {
        searchRequestBuilder.addSort(
            order.getProperty(),
            order.getDirection() == Sort.Direction.DESC ? SortOrder.DESC : SortOrder.ASC);
      }
    }

    if (query.getMinScore() > 0) {
      searchRequestBuilder.setMinScore(query.getMinScore());
    }
    return searchRequestBuilder;
  }
コード例 #2
0
  @Override
  public String scan(SearchQuery searchQuery, long scrollTimeInMillis, boolean noFields) {
    Assert.notNull(searchQuery.getIndices(), "No index defined for Query");
    Assert.notNull(searchQuery.getTypes(), "No type define for Query");
    Assert.notNull(searchQuery.getPageable(), "Query.pageable is required for scan & scroll");

    SearchRequestBuilder requestBuilder =
        client
            .prepareSearch(toArray(searchQuery.getIndices()))
            .setSearchType(SCAN)
            .setQuery(searchQuery.getQuery())
            .setTypes(toArray(searchQuery.getTypes()))
            .setScroll(TimeValue.timeValueMillis(scrollTimeInMillis))
            .setFrom(0)
            .setSize(searchQuery.getPageable().getPageSize());

    if (searchQuery.getFilter() != null) {
      requestBuilder.setPostFilter(searchQuery.getFilter());
    }

    if (isNotEmpty(searchQuery.getFields())) {
      requestBuilder.addFields(toArray(searchQuery.getFields()));
    }

    if (noFields) {
      requestBuilder.setNoFields();
    }
    return requestBuilder.execute().actionGet().getScrollId();
  }
コード例 #3
0
ファイル: QProfileRuleLookup.java プロジェクト: red6/sonar
 private SearchHits searchRules(
     ProfileRuleQuery query, Paging paging, FilterBuilder filterBuilder, String... fields) {
   SearchRequestBuilder builder =
       index
           .client()
           .prepareSearch(INDEX_RULES)
           .setTypes(TYPE_RULE)
           .setPostFilter(filterBuilder)
           .setSize(paging.pageSize())
           .setFrom(paging.offset());
   if (fields.length > 0) {
     builder.addFields(fields);
   }
   addOrder(query, builder);
   return index.executeRequest(builder);
 }
コード例 #4
0
ファイル: QProfileRuleLookup.java プロジェクト: red6/sonar
 private SearchHits searchActiveRules(
     ProfileRuleQuery query, List<Integer> ruleIds, String... fields) {
   SearchRequestBuilder activeRuleBuilder =
       index
           .client()
           .prepareSearch(INDEX_RULES)
           .setTypes(ESActiveRule.TYPE_ACTIVE_RULE)
           .setPostFilter(
               boolFilter()
                   .must(
                       termFilter(ActiveRuleDocument.FIELD_PROFILE_ID, query.profileId()),
                       hasParentFilter(TYPE_RULE, termsFilter(RuleDocument.FIELD_ID, ruleIds))))
           .setSize(ruleIds.size());
   if (fields.length > 0) {
     activeRuleBuilder.addFields(fields);
   }
   return index.executeRequest(activeRuleBuilder);
 }