Search in sources :

Example 6 with ScoreTiesAdjusterReranker

use of io.anserini.rerank.lib.ScoreTiesAdjusterReranker in project Anserini by castorini.

the class SearchCollection method search.

public <K> ScoredDocuments search(IndexSearcher searcher, K qid, String queryString, RerankerCascade cascade, ScoredDocuments queryQrels, boolean hasRelDocs) throws IOException {
    Query query = null;
    if (args.sdm) {
        query = new SdmQueryGenerator(args.sdm_tw, args.sdm_ow, args.sdm_uw).buildQuery(IndexArgs.CONTENTS, analyzer, queryString);
    } else {
        QueryGenerator generator;
        try {
            generator = (QueryGenerator) Class.forName("io.anserini.search.query." + args.queryGenerator).getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Unable to load QueryGenerator: " + args.topicReader);
        }
        query = generator.buildQuery(IndexArgs.CONTENTS, analyzer, queryString);
    }
    TopDocs rs = new TopDocs(new TotalHits(0, TotalHits.Relation.EQUAL_TO), new ScoreDoc[] {});
    if (!isRerank || (args.rerankcutoff > 0 && args.rf_qrels == null) || (args.rf_qrels != null && !hasRelDocs)) {
        if (args.arbitraryScoreTieBreak) {
            // Figure out how to break the scoring ties.
            rs = searcher.search(query, (isRerank && args.rf_qrels == null) ? args.rerankcutoff : args.hits);
        } else {
            rs = searcher.search(query, (isRerank && args.rf_qrels == null) ? args.rerankcutoff : args.hits, BREAK_SCORE_TIES_BY_DOCID, true);
        }
    }
    List<String> queryTokens = AnalyzerUtils.analyze(analyzer, queryString);
    queries.put(qid.toString(), queryTokens);
    RerankerContext context = new RerankerContext<>(searcher, qid, query, null, queryString, queryTokens, null, args);
    ScoredDocuments scoredFbDocs;
    if (isRerank && args.rf_qrels != null) {
        if (hasRelDocs) {
            scoredFbDocs = queryQrels;
        } else {
            // if no relevant documents, only perform score based tie breaking next
            LOG.info("No relevant documents for " + qid.toString());
            scoredFbDocs = ScoredDocuments.fromTopDocs(rs, searcher);
            cascade = new RerankerCascade();
            cascade.add(new ScoreTiesAdjusterReranker());
        }
    } else {
        scoredFbDocs = ScoredDocuments.fromTopDocs(rs, searcher);
    }
    return cascade.run(scoredFbDocs, context);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) Query(org.apache.lucene.search.Query) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ScoredDocuments(io.anserini.rerank.ScoredDocuments) QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) CmdLineException(org.kohsuke.args4j.CmdLineException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) TopDocs(org.apache.lucene.search.TopDocs) RerankerCascade(io.anserini.rerank.RerankerCascade) QueryGenerator(io.anserini.search.query.QueryGenerator) SdmQueryGenerator(io.anserini.search.query.SdmQueryGenerator) ScoreTiesAdjusterReranker(io.anserini.rerank.lib.ScoreTiesAdjusterReranker) SdmQueryGenerator(io.anserini.search.query.SdmQueryGenerator) RerankerContext(io.anserini.rerank.RerankerContext)

Example 7 with ScoreTiesAdjusterReranker

use of io.anserini.rerank.lib.ScoreTiesAdjusterReranker in project Anserini by castorini.

the class SimpleSearcher method setRM3.

/**
 * Enables RM3 query expansion with default parameters.
 *
 * @param fbTerms number of expansion terms
 * @param fbDocs number of expansion documents
 * @param originalQueryWeight weight to assign to the original query
 * @param outputQuery flag to print original and expanded queries
 * @param filterTerms whether to filter terms to be English only
 */
public void setRM3(int fbTerms, int fbDocs, float originalQueryWeight, boolean outputQuery, boolean filterTerms) {
    useRM3 = true;
    cascade = new RerankerCascade("rm3");
    cascade.add(new Rm3Reranker(this.analyzer, IndexArgs.CONTENTS, fbTerms, fbDocs, originalQueryWeight, outputQuery, filterTerms));
    cascade.add(new ScoreTiesAdjusterReranker());
}
Also used : RerankerCascade(io.anserini.rerank.RerankerCascade) Rm3Reranker(io.anserini.rerank.lib.Rm3Reranker) ScoreTiesAdjusterReranker(io.anserini.rerank.lib.ScoreTiesAdjusterReranker)

Example 8 with ScoreTiesAdjusterReranker

use of io.anserini.rerank.lib.ScoreTiesAdjusterReranker in project Anserini by castorini.

the class SimpleSearcher method unsetRM3.

/**
 * Disables RM3 query expansion.
 */
public void unsetRM3() {
    this.useRM3 = false;
    cascade = new RerankerCascade();
    cascade.add(new ScoreTiesAdjusterReranker());
}
Also used : RerankerCascade(io.anserini.rerank.RerankerCascade) ScoreTiesAdjusterReranker(io.anserini.rerank.lib.ScoreTiesAdjusterReranker)

Example 9 with ScoreTiesAdjusterReranker

use of io.anserini.rerank.lib.ScoreTiesAdjusterReranker 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

ScoreTiesAdjusterReranker (io.anserini.rerank.lib.ScoreTiesAdjusterReranker)9 IOException (java.io.IOException)6 CmdLineException (org.kohsuke.args4j.CmdLineException)6 RerankerCascade (io.anserini.rerank.RerankerCascade)5 Query (org.apache.lucene.search.Query)3 RerankerContext (io.anserini.rerank.RerankerContext)2 ScoredDocuments (io.anserini.rerank.ScoredDocuments)2 Rm3Reranker (io.anserini.rerank.lib.Rm3Reranker)2 QueryGenerator (io.anserini.search.query.QueryGenerator)2 SdmQueryGenerator (io.anserini.search.query.SdmQueryGenerator)2 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)2 CompletionException (java.util.concurrent.CompletionException)2 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)2 TopDocs (org.apache.lucene.search.TopDocs)2 TotalHits (org.apache.lucene.search.TotalHits)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2