Search in sources :

Example 1 with ScoreSortBuilder

use of org.elasticsearch.search.sort.ScoreSortBuilder in project Anserini by castorini.

the class SearchElastic method searchTweets.

public <K> ScoredDocuments searchTweets(String queryString, long t) {
    SearchHits results = null;
    String specials = "+-=&|><!(){}[]^\"~*?:\\/";
    for (int i = 0; i < specials.length(); i++) {
        char c = specials.charAt(i);
        queryString = queryString.replace(String.valueOf(c), " ");
    }
    // Do not consider the tweets with tweet ids that are beyond the queryTweetTime
    // <querytweettime> tag contains the timestamp of the query in terms of the
    // chronologically nearest tweet id within the corpus
    RangeQueryBuilder queryTweetTime = QueryBuilders.rangeQuery(TweetGenerator.TweetField.ID_LONG.name).from(0L).to(t);
    QueryStringQueryBuilder queryTerms = QueryBuilders.queryStringQuery(queryString).defaultField("contents").analyzer("english");
    BoolQueryBuilder query = QueryBuilders.boolQuery().filter(queryTweetTime).should(queryTerms);
    SearchRequest searchRequest = new SearchRequest(args.esIndex);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(query);
    sourceBuilder.size(args.hits);
    sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    sourceBuilder.sort(new FieldSortBuilder(TweetGenerator.TweetField.ID_LONG.name).order(SortOrder.DESC));
    searchRequest.source(sourceBuilder);
    try {
        SearchResponse searchResponse = client.search(searchRequest, COMMON_OPTIONS);
        results = searchResponse.getHits();
    } catch (Exception e) {
        LOG.error("Exception during ES query: ", e);
    }
    ScoreTiesAdjusterReranker reranker = new ScoreTiesAdjusterReranker();
    return reranker.rerank(ScoredDocuments.fromESDocs(results), null);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ScoreSortBuilder(org.elasticsearch.search.sort.ScoreSortBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ScoreTiesAdjusterReranker(io.anserini.rerank.lib.ScoreTiesAdjusterReranker) SearchHits(org.elasticsearch.search.SearchHits) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder)

Example 2 with ScoreSortBuilder

use of org.elasticsearch.search.sort.ScoreSortBuilder 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 3 with ScoreSortBuilder

use of org.elasticsearch.search.sort.ScoreSortBuilder in project Anserini by castorini.

the class SearchElastic method search.

public <K> ScoredDocuments search(String queryString) {
    SearchHits results = null;
    String specials = "+-=&|><!(){}[]^\"~*?:\\/";
    for (int i = 0; i < specials.length(); i++) {
        char c = specials.charAt(i);
        queryString = queryString.replace(String.valueOf(c), " ");
    }
    QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(queryString).defaultField("contents").analyzer("english");
    SearchRequest searchRequest = new SearchRequest(args.esIndex);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(query);
    sourceBuilder.size(args.hits);
    sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    sourceBuilder.sort(new FieldSortBuilder(IndexArgs.ID).order(SortOrder.ASC));
    searchRequest.source(sourceBuilder);
    try {
        SearchResponse searchResponse = client.search(searchRequest, COMMON_OPTIONS);
        results = searchResponse.getHits();
    } catch (Exception e) {
        LOG.error("Exception during ES query: ", e);
    }
    ScoreTiesAdjusterReranker reranker = new ScoreTiesAdjusterReranker();
    return reranker.rerank(ScoredDocuments.fromESDocs(results), null);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ScoreSortBuilder(org.elasticsearch.search.sort.ScoreSortBuilder) ScoreTiesAdjusterReranker(io.anserini.rerank.lib.ScoreTiesAdjusterReranker) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchHits(org.elasticsearch.search.SearchHits) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)3 ScoreSortBuilder (org.elasticsearch.search.sort.ScoreSortBuilder)3 ScoreTiesAdjusterReranker (io.anserini.rerank.lib.ScoreTiesAdjusterReranker)2 IOException (java.io.IOException)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 QueryStringQueryBuilder (org.elasticsearch.index.query.QueryStringQueryBuilder)2 SearchHits (org.elasticsearch.search.SearchHits)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 CmdLineException (org.kohsuke.args4j.CmdLineException)2 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)1 Entry (org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)1 GeoDistanceSortBuilder (org.elasticsearch.search.sort.GeoDistanceSortBuilder)1 ScriptSortBuilder (org.elasticsearch.search.sort.ScriptSortBuilder)1 SortBuilder (org.elasticsearch.search.sort.SortBuilder)1