org.elasticsearch.search.sort.SortBuilder Java Examples

The following examples show how to use org.elasticsearch.search.sort.SortBuilder. 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: QueryOrderByParser.java    From elasticsearch-sql with MIT License 7 votes vote down vote up
@Override
public void parse(ElasticDslContext dslContext) {
    if(dslContext.getSqlContext().selectOperation()!=null&&dslContext.getSqlContext().selectOperation().groupByClause()==null){
        if(dslContext.getSqlContext().selectOperation().orderClause()!=null){
            ElasticsearchParser.OrderClauseContext orderClauseContext=dslContext.getSqlContext().selectOperation().orderClause();
            for(ElasticsearchParser.OrderContext orderContext:orderClauseContext.order()){
                ElasticsearchParser.NameClauseContext nameContext = orderContext.nameClause();
                if(nameContext instanceof ElasticsearchParser.FieldNameContext){
                    ElasticsearchParser.FieldNameContext fieldNameContext=(ElasticsearchParser.FieldNameContext)nameContext;
                    String field = fieldNameContext.field.getText();
                    if(fieldNameContext.highlighter!=null){
                        dslContext.getParseResult().getHighlighter().add(field);
                    }
                    SortOrder sortOrder;
                    if(orderContext.ASC()!=null) {
                        sortOrder=SortOrder.ASC;
                    }else{
                        sortOrder=SortOrder.DESC;
                    }
                    SortBuilder sortBuilder = SortBuilders.fieldSort(field).sortMode(SortMode.AVG).order(sortOrder);
                    dslContext.getParseResult().getOrderBy().add(sortBuilder);
                }
            }
        }
    }
}
 
Example #2
Source File: EsSort.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param sort_fileds
 * @return
 */
public static List<SortBuilder> sortAdapter(List<String> sort_fileds) {
    List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
    for (String sort : sort_fileds) {
        String[] items = StringUtils.split(sort, " ");
        if (items.length > 2 || items.length < 2) {
            throw new RuntimeException("排序参数格式不正确,必须为:filed desc|asc,多个filed以逗号分隔!");
        }
        String[] fileds = items[0].split(",");
        for (String filed : fileds) {
            SortBuilder sortb = null;
            if (items[0].equalsIgnoreCase("desc")) {
                sortb = SortBuilders.fieldSort(filed).order(SortOrder.DESC);
            } else {
                sortb = SortBuilders.fieldSort(filed).order(SortOrder.ASC);
            }
            sortBuilders.add(sortb);
        }

    }
    return sortBuilders;
}
 
Example #3
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private SearchRequest toRequest() {
    SearchRequest searchRequest = new SearchRequest();
    List<String> indexList = indices.parallelStream().map(StringManager::removeStringSymbol).collect(Collectors.toList());
    if (CollectionUtils.isNotEmpty(indexList)) {
        searchRequest.indices(indexList.toArray(new String[0]));
    } else {
        throw new ElasticSql2DslException("[syntax error] indices name must be set");
    }
    //这里不会修改from的值
    searchSourceBuilder.from(Math.max(from, 0));
    searchSourceBuilder.size(Math.max(size, 0));
    searchSourceBuilder.trackTotalHits(this.trackTotalHits);
    if (CollectionUtils.isNotEmpty(highlighter)) {
        HighlightBuilder highlightBuilder = HighlightBuilders.highlighter(highlighter);
        searchSourceBuilder.highlighter(highlightBuilder);
    }
    searchSourceBuilder.query(whereCondition);
    if (StringUtils.isNotBlank(distinctName)) {
        searchSourceBuilder.collapse(new CollapseBuilder(distinctName));
    }
    if (CollectionUtils.isNotEmpty(orderBy)) {
        for (SortBuilder sortBuilder : orderBy) {
            searchSourceBuilder.sort(sortBuilder);
        }
    }
    searchSourceBuilder.fetchSource(includeFields.toArray(new String[0]), excludeFields.toArray(new String[0]));
    if (CollectionUtils.isNotEmpty(routingBy)) {
        searchRequest.routing(routingBy.toArray(new String[0]));
    }
    if (CollectionUtils.isNotEmpty(groupBy)) {
        for (AggregationBuilder aggItem : groupBy) {
            searchSourceBuilder.aggregation(aggItem);
        }
    }
    return searchRequest.source(searchSourceBuilder);
}
 
Example #4
Source File: BaseSearchService.java    From albert with MIT License 6 votes vote down vote up
protected SortBuilder addSort(SearchParam param){
		Integer sort = param.getSort();
		
//		if(null==sort){
//			return SortBuilders.fieldSort("lastRefresh").order(SortOrder.DESC);
//		}else if(Constants.Sort.nearest == sort){
//			return SortBuilders.geoDistanceSort("geoPoint").point(param.getLat(), param.getLon()).order(SortOrder.ASC);
//		
//		} else if(Constants.Sort.pageView == sort){
//			return SortBuilders.fieldSort("reviewNum").order(SortOrder.DESC);
//		
//		} else if(Constants.Sort.score == sort){
//			return SortBuilders.fieldSort("avgScore").order(SortOrder.DESC);
//		
//		} else if(Constants.Sort.priceAsc == sort){
//			return SortBuilders.fieldSort("price").order(SortOrder.ASC);
//		
//		} else if(Constants.Sort.priceDesc == sort){
//			return SortBuilders.fieldSort("price").order(SortOrder.DESC);
//		
//		}
		
		return SortBuilders.fieldSort("addTime").order(SortOrder.DESC);
	}
 
Example #5
Source File: InvokeChainQueryHandler.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param data
 * @param queryBuilder
 * @param postFilter
 */
@SuppressWarnings("rawtypes")
private void queryToList(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
        SortBuilder[] sorts) {

    SearchResponse sr = query(data, queryBuilder, postFilter, sorts);

    SearchHits shits = sr.getHits();

    List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();

    for (SearchHit sh : shits) {
        Map<String, Object> record = sh.getSourceAsMap();

        if (record == null) {
            continue;
        }

        records.add(record);
    }

    data.putResponse("rs", JSONHelper.toString(records));
    // 返回总的条数
    data.putResponse("count", shits.getTotalHits() + "");
}
 
Example #6
Source File: SlowOperQueryHandler.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
private SortBuilder[] buildSort(UAVHttpMessage data) {

    SortBuilder[] sorts = null;

    String sort = data.getRequest("sort");

    if (sort != null) {
        String[] sortFieldStrs = sort.split(",");
        List<SortBuilder> ls = new ArrayList<SortBuilder>();
        for (String sortFieldStr : sortFieldStrs) {
            String[] sortExp = sortFieldStr.split("=");
            SortBuilder stimeSort = new FieldSortBuilder(sortExp[0]);
            stimeSort.order(SortOrder.fromString(sortExp[1]));
            ls.add(stimeSort);
        }
        sorts = new SortBuilder[ls.size()];
        sorts = ls.toArray(sorts);
    }
    else {
        return null;
    }
    return sorts;
}
 
Example #7
Source File: BaseClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
public Long mostRecentDocument(String index) {
    if (client() == null) {
        return null;
    }
    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
    SortBuilder sort = SortBuilders.fieldSort("_timestamp").order(SortOrder.DESC);
    SearchResponse searchResponse = searchRequestBuilder.setIndices(index).addField("_timestamp").setSize(1).addSort(sort).execute().actionGet();
    if (searchResponse.getHits().getHits().length == 1) {
        SearchHit hit = searchResponse.getHits().getHits()[0];
        if (hit.getFields().get("_timestamp") != null) {
            return hit.getFields().get("_timestamp").getValue();
        } else {
            return 0L;
        }
    }
    return null;
}
 
Example #8
Source File: SearchServiceImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public SearchResponse search(final QueryBuilder query,
                             @Nullable final List<SortBuilder> sort,
                             final int from,
                             final int size,
                             @Nullable final Integer seconds)
{
  RepositoryQueryBuilder repoQuery = repositoryQuery(query);

  if (sort != null) {
    repoQuery = repoQuery.sortBy(sort);
  }

  if (seconds != null) {
    repoQuery = repoQuery.timeout(ofSeconds(seconds));
  }

  return searchQueryService.search(repoQuery, from, size);
}
 
Example #9
Source File: ElasticsearchAnySearchDAO.java    From syncope with Apache License 2.0 6 votes vote down vote up
private SearchRequest searchRequest(
        final Set<String> adminRealms,
        final SearchCond cond,
        final AnyTypeKind kind,
        final int from,
        final int size,
        final List<SortBuilder<?>> sortBuilders) {

    Pair<DisMaxQueryBuilder, Set<String>> filter = adminRealmsFilter(adminRealms);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().
            query(SyncopeConstants.FULL_ADMIN_REALMS.equals(adminRealms)
                    ? getQueryBuilder(cond, kind)
                    : QueryBuilders.boolQuery().
                            must(filter.getLeft()).
                            must(getQueryBuilder(buildEffectiveCond(cond, filter.getRight()), kind))).
            from(from).
            size(size);
    sortBuilders.forEach(sourceBuilder::sort);

    return new SearchRequest(ElasticsearchUtils.getContextDomainName(AuthContextUtils.getDomain(), kind)).
            searchType(SearchType.QUERY_THEN_FETCH).
            source(sourceBuilder);
}
 
Example #10
Source File: SearchUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
public List<SortBuilder> getSortBuilders(final String sort, final String direction, boolean allowAnySort) {
  if (sort == null) {
    return emptyList();
  }

  switch (sort) {
    case GROUP:
      return handleGroupSort(direction);
    case NAME:
      return handleNameSort(direction);
    case VERSION:
      return handleVersionSort(direction);
    case "repository":
    case "repositoryName":
      return handleRepositoryNameSort(direction);
    default:
      return handleOtherSort(sort, direction, allowAnySort);
  }
}
 
Example #11
Source File: NewLogQueryHandler.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private void queryToList(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
        SortBuilder[] sorts) {

    SearchResponse sr = query(data, queryBuilder, postFilter, sorts);

    SearchHits shits = sr.getHits();

    List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();

    for (SearchHit sh : shits) {
        Map<String, Object> record = sh.getSourceAsMap();

        if (record == null) {
            continue;
        }

        records.add(record);
    }

    // 如果只存在eline则需要把结果逆序,保证其原始顺序
    long startLine = DataConvertHelper.toLong(data.getRequest("sline"), -1);
    long endLine = DataConvertHelper.toLong(data.getRequest("eline"), -1);
    if (startLine == -1 && endLine > -1) {
        Collections.reverse(records);
    }

    data.putResponse("rs", JSONHelper.toString(records));
    // 返回总条数
    data.putResponse("count", shits.getTotalHits() + "");
}
 
Example #12
Source File: EsSort.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param srb
 * @param sort_fileds
 * @return
 */
public static void sortAdapter(SearchRequestBuilder srb, List<String> sort_fileds) {
    // 处理排序
    for (SortBuilder s : sortAdapter(sort_fileds)) {
        srb.addSort(s);
    }
}
 
Example #13
Source File: ThreadAnalysisQueryHandler.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
        List<SortBuilder> sorts) {

    String date = data.getRequest("indexdate");
    String currentIndex;
    if (date != null) {
        // 指定index
        currentIndex = this.indexMgr.getIndexByDate(date);
    }
    else {
        // current index
        currentIndex = this.indexMgr.getCurrentIndex();
    }

    SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex)
            .setTypes(ThreadAnalysisIndexMgr.JTA_TABLE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH);

    int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
    int size = DataConvertHelper.toInt(data.getRequest("size"), -1);

    if (from != -1 && size != -1) {
        srb = srb.setFrom(from).setSize(size);
    }

    srb.setQuery(queryBuilder);

    if (postFilter != null) {
        srb.setPostFilter(postFilter);
    }

    for (SortBuilder sb : sorts) {
        srb.addSort(sb);
    }

    SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));

    return sr;
}
 
Example #14
Source File: ElasticsearchAnySearchDAO.java    From syncope with Apache License 2.0 5 votes vote down vote up
private List<SortBuilder<?>> sortBuilders(
        final AnyTypeKind kind,
        final List<OrderByClause> orderBy) {

    AnyUtils anyUtils = anyUtilsFactory.getInstance(kind);

    List<SortBuilder<?>> builders = new ArrayList<>();
    orderBy.forEach(clause -> {
        String sortName = null;

        // Manage difference among external key attribute and internal JPA @Id
        String fieldName = "key".equals(clause.getField()) ? "id" : clause.getField();

        Field anyField = anyUtils.getField(fieldName);
        if (anyField == null) {
            PlainSchema schema = schemaDAO.find(fieldName);
            if (schema != null) {
                sortName = fieldName;
            }
        } else {
            sortName = fieldName;
        }

        if (sortName == null) {
            LOG.warn("Cannot build any valid clause from {}", clause);
        } else {
            builders.add(new FieldSortBuilder(sortName).order(SortOrder.valueOf(clause.getDirection().name())));
        }
    });
    return builders;
}
 
Example #15
Source File: ESIndexState.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
public Collection<T> searchSortedAndFirstNQuery(String query, List<String> indices, List<String> types,
                                                SortBuilder sortBuilder, int firstN) {
    SearchResponse response = buildSearchQuery(query, indices, types, firstN)
            .addSort(sortBuilder)
            .setQuery(query).execute().actionGet();

    return buildResult(response);
}
 
Example #16
Source File: SearchUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void assertSearchBuilder(SortBuilder sortBuilder, String field, String order) throws Exception {
  //see https://github.com/elastic/elasticsearch/issues/20853 as to why i can't do something simple like
  //assertThat(sortBuilders.get(0).toString(), is("somejson"));
  XContentBuilder xContentBuilder = mock(XContentBuilder.class);
  sortBuilder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
  verify(xContentBuilder).startObject(field);
  verify(xContentBuilder).field("order", order);
  verify(xContentBuilder).endObject();
  verifyNoMoreInteractions(xContentBuilder);
}
 
Example #17
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public SimpleSearchQueryBuilder withScriptSort(String script, SortOrder order){
	if(StringUtils.isBlank(script))
		return this;
	Script sortScript = new Script(script);
	SortBuilder sortBuilder = SortBuilders.scriptSort(sortScript, "number");
	sortBuilder.order(order);
	this.searchQueryBuilder.withSort(sortBuilder);
	return this;
}
 
Example #18
Source File: PercolateSourceBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a sort builder. Only sorting by score desc is supported.
 *
 * By default the matching percolator queries are returned in an undefined order.
 */
public PercolateSourceBuilder addSort(SortBuilder sort) {
    if (sorts == null) {
        sorts = new ArrayList<>();
    }
    sorts.add(sort);
    return this;
}
 
Example #19
Source File: PercolateSourceBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    if (docBuilder != null) {
        docBuilder.toXContent(builder, params);
    }
    if (queryBuilder != null) {
        builder.field("query");
        queryBuilder.toXContent(builder, params);
    }
    if (size != null) {
        builder.field("size", size);
    }
    if (sorts != null) {
        builder.startArray("sort");
        for (SortBuilder sort : sorts) {
            builder.startObject();
            sort.toXContent(builder, params);
            builder.endObject();
        }
        builder.endArray();
    }
    if (trackScores != null) {
        builder.field("track_scores", trackScores);
    }
    if (highlightBuilder != null) {
        highlightBuilder.toXContent(builder, params);
    }
    if (aggregations != null) {
        builder.field("aggregations");
        builder.startObject();
        for (AbstractAggregationBuilder aggregation : aggregations) {
            aggregation.toXContent(builder, params);
        }
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
 
Example #20
Source File: ClientFacade.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private SearchRequestBuilder createSearchRequest(
    QueryBuilder query,
    Integer from,
    Integer size,
    Sort sort,
    List<AggregationBuilder> aggregations,
    List<Index> indexes) {
  String[] indexNames = toIndexNames(indexes);
  SearchRequestBuilder searchRequest = client.prepareSearch(indexNames);
  if (query != null) {
    searchRequest.setQuery(query);
  }
  if (from != null) {
    searchRequest.setFrom(from);
  }
  if (size != null) {
    searchRequest.setSize(size);
  }
  if (sort != null) {
    List<SortBuilder> sorts = sortContentBuilder.createSorts(sort);
    sorts.forEach(searchRequest::addSort);
  }
  if (aggregations != null) {
    aggregations.forEach(searchRequest::addAggregation);
  }
  return searchRequest;
}
 
Example #21
Source File: SearchServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public SearchResponse searchUnrestrictedInRepos(final QueryBuilder query,
                                                @Nullable final List<SortBuilder> sort,
                                                final int from,
                                                final int size,
                                                final Collection<String> repoNames)
{
  RepositoryQueryBuilder repoQuery = repositoryQuery(query).inRepositories(repoNames);

  if (sort != null) {
    repoQuery = repoQuery.sortBy(sort);
  }

  return searchQueryService.search(repoQuery.unrestricted(), from, size);
}
 
Example #22
Source File: SearchServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public SearchResponse searchUnrestricted(final QueryBuilder query,
                                         @Nullable final List<SortBuilder> sort,
                                         final int from,
                                         final int size)
{
  RepositoryQueryBuilder repoQuery = repositoryQuery(query);

  if (sort != null) {
    repoQuery = repoQuery.sortBy(sort);
  }

  return searchQueryService.search(repoQuery.unrestricted(), from, size);
}
 
Example #23
Source File: QueryHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
public void addDefaultSort(final String fieldName, final String order) {
    final List<SortBuilder<?>> list = new ArrayList<>();
    if (defaultSortBuilders != null) {
        stream(defaultSortBuilders).of(stream -> stream.forEach(builder -> list.add(builder)));
    }
    list.add(createFieldSortBuilder(fieldName, SortOrder.DESC.toString().equalsIgnoreCase(order) ? SortOrder.DESC : SortOrder.ASC));
    defaultSortBuilders = list.toArray(new SortBuilder[list.size()]);
}
 
Example #24
Source File: SearchUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGetSortBuilders_byNameDescending() throws Exception{
  List<SortBuilder> sortBuilders = underTest.getSortBuilders("name", "desc");
  assertThat(sortBuilders.size(), is(3));
  assertSearchBuilder(sortBuilders.get(0), "name.case_insensitive", "desc");
  assertSearchBuilder(sortBuilders.get(1), "version", "asc");
  assertSearchBuilder(sortBuilders.get(2), "group.case_insensitive", "asc");
}
 
Example #25
Source File: FessProp.java    From fess with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
default OptionalThing<SortBuilder[]> getQueryCollapseInnerHitsSortBuilders() {
    @SuppressWarnings("unchecked")
    OptionalThing<SortBuilder[]> ot = (OptionalThing<SortBuilder[]>) propMap.get(QUERY_COLLAPSE_INNER_HITS_SORTS);
    if (ot == null) {
        final String sorts = getQueryCollapseInnerHitsSorts();
        if (StringUtil.isBlank(sorts)) {
            ot = OptionalThing.empty();
        } else {
            final SortBuilder[] sortBuilders =
                    split(sorts, ",").get(
                            stream -> stream
                                    .filter(StringUtil::isNotBlank)
                                    .map(s -> {
                                        final String[] values = s.split(":");
                                        if (values.length > 1) {
                                            return SortBuilders.fieldSort(values[0]).order(
                                                    values[0].equalsIgnoreCase("desc") ? SortOrder.DESC : SortOrder.ASC);
                                        } else {
                                            return SortBuilders.fieldSort(values[0]).order(SortOrder.ASC);
                                        }
                                    }).toArray(n -> new SortBuilder[n]));
            ot = OptionalThing.of(sortBuilders);
        }
        propMap.put(QUERY_COLLAPSE_INNER_HITS_SORTS, ot);
    }
    return ot;
}
 
Example #26
Source File: SearchUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGetSortBuilders_byGroupDefaultSort() throws Exception {
  List<SortBuilder> sortBuilders = underTest.getSortBuilders("group", null);
  assertThat(sortBuilders.size(), is(3));
  assertSearchBuilder(sortBuilders.get(0), "group.case_insensitive", "asc");
  assertSearchBuilder(sortBuilders.get(1), "name.case_insensitive", "asc");
  assertSearchBuilder(sortBuilders.get(2), "version", "asc");
}
 
Example #27
Source File: SearchQueryServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private SearchResponse executeSearch(final RepositoryQueryBuilder repoQuery,
                                     final String[] searchableIndexes,
                                     final int from, final int size,
                                     @Nullable final QueryBuilder postFilter)
{
  SearchRequestBuilder searchRequestBuilder = client.get().prepareSearch(searchableIndexes)
      .setTypes(TYPE)
      .setQuery(repoQuery)
      .setFrom(from)
      .setSize(size)
      .setProfile(profile);

  if (repoQuery.sort != null) {
    for (SortBuilder entry : repoQuery.sort) {
      searchRequestBuilder.addSort(entry);
    }
  }

  if (postFilter != null) {
    searchRequestBuilder.setPostFilter(postFilter);
  }

  if (repoQuery.timeout != null) {
    searchRequestBuilder.setTimeout(repoQuery.timeout.getSeconds() + "s");
  }

  SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

  if (profile) {
    logProfileResults(searchResponse);
  }

  return searchResponse;
}
 
Example #28
Source File: SearchQueryServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private SearchResponse executeSearch(final RepositoryQueryBuilder repoQuery,
                                     final String[] searchableIndexes,
                                     final List<AggregationBuilder> aggregations,
                                     @Nullable final QueryBuilder postFilter)
{
  SearchRequestBuilder searchRequestBuilder = client.get().prepareSearch(searchableIndexes)
      .setTypes(TYPE)
      .setQuery(repoQuery)
      .setFrom(0)
      .setSize(0)
      .setProfile(profile)
      .setTrackScores(true);

  for (AggregationBuilder aggregation : aggregations) {
    searchRequestBuilder.addAggregation(aggregation);
  }

  if (repoQuery.sort != null) {
    for (SortBuilder entry : repoQuery.sort) {
      searchRequestBuilder.addSort(entry);
    }
  }

  if (postFilter != null) {
    searchRequestBuilder.setPostFilter(postFilter);
  }

  if (repoQuery.timeout != null) {
    searchRequestBuilder.setTimeout(repoQuery.timeout.getSeconds() + "s");
  }

  SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

  if (profile) {
    logProfileResults(searchResponse);
  }

  return searchResponse;
}
 
Example #29
Source File: SearchUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGetSortBuilders_byGroup() throws Exception {
  List<SortBuilder> sortBuilders = underTest.getSortBuilders("group", "asc");
  assertThat(sortBuilders.size(), is(3));
  assertSearchBuilder(sortBuilders.get(0), "group.case_insensitive", "asc");
  assertSearchBuilder(sortBuilders.get(1), "name.case_insensitive", "asc");
  assertSearchBuilder(sortBuilders.get(2), "version", "asc");
}
 
Example #30
Source File: SearchUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGetSortBuilders_byGroupDescending() throws Exception{
  List<SortBuilder> sortBuilders = underTest.getSortBuilders("group", "desc");
  assertThat(sortBuilders.size(), is(3));
  assertSearchBuilder(sortBuilders.get(0), "group.case_insensitive", "desc");
  assertSearchBuilder(sortBuilders.get(1), "name.case_insensitive", "asc");
  assertSearchBuilder(sortBuilders.get(2), "version", "asc");
}