Search in sources :

Example 16 with FieldSortBuilder

use of org.elasticsearch.search.sort.FieldSortBuilder in project titan by thinkaurelius.

the class ElasticSearchIndex method query.

@Override
public List<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, TransactionHandle tx) throws StorageException {
    SearchRequestBuilder srb = client.prepareSearch(indexName);
    srb.setTypes(query.getStore());
    srb.setQuery(QueryBuilders.matchAllQuery());
    srb.setFilter(getFilter(query.getCondition(), informations.get(query.getStore())));
    if (!query.getOrder().isEmpty()) {
        List<IndexQuery.OrderEntry> orders = query.getOrder();
        for (int i = 0; i < orders.size(); i++) {
            srb.addSort(new FieldSortBuilder(orders.get(i).getKey()).order(orders.get(i).getOrder() == Order.ASC ? SortOrder.ASC : SortOrder.DESC).ignoreUnmapped(true));
        }
    }
    srb.setFrom(0);
    if (query.hasLimit())
        srb.setSize(query.getLimit());
    else
        srb.setSize(maxResultsSize);
    srb.setNoFields();
    // srb.setExplain(true);
    SearchResponse response = srb.execute().actionGet();
    log.debug("Executed query [{}] in {} ms", query.getCondition(), response.getTookInMillis());
    SearchHits hits = response.getHits();
    if (!query.hasLimit() && hits.totalHits() >= maxResultsSize)
        log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query);
    List<String> result = new ArrayList<String>(hits.hits().length);
    for (SearchHit hit : hits) {
        result.add(hit.id());
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 17 with FieldSortBuilder

use of org.elasticsearch.search.sort.FieldSortBuilder in project sonarqube by SonarSource.

the class RuleIndex method setSorting.

private static void setSorting(RuleQuery query, SearchSourceBuilder esSearch) {
    /* integrate Query Sort */
    String queryText = query.getQueryText();
    if (query.getSortField() != null) {
        FieldSortBuilder sort = SortBuilders.fieldSort(appendSortSuffixIfNeeded(query.getSortField()));
        if (query.isAscendingSort()) {
            sort.order(SortOrder.ASC);
        } else {
            sort.order(SortOrder.DESC);
        }
        esSearch.sort(sort);
    } else if (StringUtils.isNotEmpty(queryText)) {
        esSearch.sort(SortBuilders.scoreSort());
    } else {
        esSearch.sort(appendSortSuffixIfNeeded(FIELD_RULE_UPDATED_AT), SortOrder.DESC);
        // deterministic sort when exactly the same updated_at (same millisecond)
        esSearch.sort(appendSortSuffixIfNeeded(FIELD_RULE_KEY), SortOrder.ASC);
    }
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder)

Example 18 with FieldSortBuilder

use of org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.

the class SearchModule method registerSorts.

private void registerSorts() {
    namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, GeoDistanceSortBuilder.NAME, GeoDistanceSortBuilder::new));
    namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, ScoreSortBuilder.NAME, ScoreSortBuilder::new));
    namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, ScriptSortBuilder.NAME, ScriptSortBuilder::new));
    namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, FieldSortBuilder.NAME, FieldSortBuilder::new));
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) GeoDistanceSortBuilder(org.elasticsearch.search.sort.GeoDistanceSortBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) ScriptSortBuilder(org.elasticsearch.search.sort.ScriptSortBuilder) ScoreSortBuilder(org.elasticsearch.search.sort.ScoreSortBuilder) Entry(org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry)

Example 19 with FieldSortBuilder

use of org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.

the class HasChildQueryBuilderTests method doCreateTestQueryBuilder.

/**
     * @return a {@link HasChildQueryBuilder} with random values all over the place
     */
@Override
protected HasChildQueryBuilder doCreateTestQueryBuilder() {
    int min = randomIntBetween(0, Integer.MAX_VALUE / 2);
    int max = randomIntBetween(min, Integer.MAX_VALUE);
    QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
    if (randomBoolean()) {
        requiresRewrite = true;
        innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
    }
    HasChildQueryBuilder hqb = new HasChildQueryBuilder(CHILD_TYPE, innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));
    hqb.minMaxChildren(min, max);
    hqb.ignoreUnmapped(randomBoolean());
    if (randomBoolean()) {
        hqb.innerHit(new InnerHitBuilder().setName(randomAsciiOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)), hqb.ignoreUnmapped());
    }
    return hqb;
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder)

Example 20 with FieldSortBuilder

use of org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.

the class NestedQueryBuilderTests method doCreateTestQueryBuilder.

/**
     * @return a {@link HasChildQueryBuilder} with random values all over the place
     */
@Override
protected NestedQueryBuilder doCreateTestQueryBuilder() {
    QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
    if (randomBoolean()) {
        requiresRewrite = true;
        innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
    }
    NestedQueryBuilder nqb = new NestedQueryBuilder("nested1", innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));
    nqb.ignoreUnmapped(randomBoolean());
    if (randomBoolean()) {
        nqb.innerHit(new InnerHitBuilder().setName(randomAsciiOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(INT_FIELD_NAME).order(SortOrder.ASC)), nqb.ignoreUnmapped());
    }
    return nqb;
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder)

Aggregations

FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)32 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 ArrayList (java.util.ArrayList)10 SearchHits (org.elasticsearch.search.SearchHits)7 SearchHit (org.elasticsearch.search.SearchHit)6 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)6 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)5 SortBuilder (org.elasticsearch.search.sort.SortBuilder)5 IOException (java.io.IOException)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)4 ScoreSortBuilder (org.elasticsearch.search.sort.ScoreSortBuilder)4 HashMap (java.util.HashMap)3 SortOrder (org.elasticsearch.search.sort.SortOrder)3 ScoreTiesAdjusterReranker (io.anserini.rerank.lib.ScoreTiesAdjusterReranker)2 FieldType (org.apache.metron.indexing.dao.search.FieldType)2 InvalidSearchException (org.apache.metron.indexing.dao.search.InvalidSearchException)2 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)2 SortField (org.apache.metron.indexing.dao.search.SortField)2 SortTerm (org.bedework.calfacade.filter.SortTerm)2