use of org.opengrok.suggest.query.PhraseScorer in project OpenGrok by OpenGrok.
the class CustomSloppyPhraseScorerTest method test.
public static void test(final int slop, final int offset, final String[] terms, final Integer[] expectedPositions) throws IOException {
Directory dir = new ByteBuffersDirectory();
try (IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig())) {
Document doc = new Document();
doc.add(new TextField("test", "zero one two three four five six seven eight nine ten", Field.Store.NO));
iw.addDocument(doc);
}
CustomPhraseQuery query = new CustomPhraseQuery(slop, "test", terms);
query.setOffset(offset);
try (IndexReader ir = DirectoryReader.open(dir)) {
IndexSearcher is = new IndexSearcher(ir);
Weight w = query.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1);
LeafReaderContext context = ir.getContext().leaves().get(0);
Scorer scorer = w.scorer(context);
TwoPhaseIterator it = scorer.twoPhaseIterator();
int correctDoc = -1;
int docId;
while ((docId = it.approximation().nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
if (it.matches()) {
correctDoc = docId;
}
}
BitIntsHolder bs = (BitIntsHolder) ((PhraseScorer) scorer).getPositions(correctDoc);
assertThat(toSet(bs), contains(expectedPositions));
}
}
use of org.opengrok.suggest.query.PhraseScorer in project OpenGrok by OpenGrok.
the class SuggesterSearcher method getComplexQueryData.
private ComplexQueryData getComplexQueryData(final Query query, final LeafReaderContext leafReaderContext) {
ComplexQueryData data = new ComplexQueryData();
if (query == null || query instanceof SuggesterQuery) {
data.documentIds = new BitIntsHolder(0);
return data;
}
BitIntsHolder documentIds = new BitIntsHolder();
try {
search(query, new Collector() {
@Override
public LeafCollector getLeafCollector(final LeafReaderContext context) {
return new LeafCollector() {
final int docBase = context.docBase;
@Override
public void setScorer(final Scorable scorer) {
if (leafReaderContext == context) {
if (scorer instanceof PhraseScorer) {
data.scorer = (PhraseScorer) scorer;
} else {
try {
// in #setScorer but no better way was found
for (Scorer.ChildScorable childScorer : scorer.getChildren()) {
if (childScorer.child instanceof PhraseScorer) {
data.scorer = (PhraseScorer) childScorer.child;
}
}
} catch (Exception e) {
// ignore
}
}
}
}
@Override
public void collect(int doc) {
if (leafReaderContext == context) {
documentIds.set(docBase + doc);
}
}
};
}
@Override
public ScoreMode scoreMode() {
return ScoreMode.COMPLETE_NO_SCORES;
}
});
} catch (IOException e) {
if (Thread.currentThread().isInterrupted()) {
interrupted = true;
return null;
} else {
logger.log(Level.WARNING, "Could not get document ids for " + query, e);
}
} catch (Exception e) {
logger.log(Level.WARNING, "Could not get document ids for " + query, e);
}
data.documentIds = documentIds;
return data;
}
Aggregations