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);
}
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());
}
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());
}
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);
}
Aggregations