use of org.apache.lucene.search.PhraseQuery in project lucene-solr by apache.
the class SynonymTokenizer method testGetBestFragmentsPhrase.
public void testGetBestFragmentsPhrase() throws Exception {
TestHighlightRunner helper = new TestHighlightRunner() {
@Override
public void run() throws Exception {
numHighlights = 0;
PhraseQuery phraseQuery = new PhraseQuery(FIELD_NAME, "john", "kennedy");
doSearching(phraseQuery);
doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
// Currently highlights "John" and "Kennedy" separately
assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 2);
}
};
helper.start();
}
use of org.apache.lucene.search.PhraseQuery in project lucene-solr by apache.
the class SynonymTokenizer method testGetBestFragmentsFilteredPhraseQuery.
public void testGetBestFragmentsFilteredPhraseQuery() throws Exception {
TestHighlightRunner helper = new TestHighlightRunner() {
@Override
public void run() throws Exception {
numHighlights = 0;
PhraseQuery pq = new PhraseQuery("contents", "john", "kennedy");
BooleanQuery.Builder bq = new BooleanQuery.Builder();
bq.add(pq, Occur.MUST);
bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER);
doSearching(bq.build());
doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
// Currently highlights "John" and "Kennedy" separately
assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 2);
}
};
helper.start();
}
use of org.apache.lucene.search.PhraseQuery in project lucene-solr by apache.
the class SynonymTokenizer method testSimpleQueryScorerPhraseHighlighting3.
public void testSimpleQueryScorerPhraseHighlighting3() throws Exception {
PhraseQuery phraseQuery = new PhraseQuery(FIELD_NAME, "x", "y", "z");
doSearching(phraseQuery);
int maxNumFragmentsRequired = 2;
for (int i = 0; i < hits.totalHits; i++) {
final int docId = hits.scoreDocs[i].doc;
final Document doc = searcher.doc(docId);
String text = doc.get(FIELD_NAME);
TokenStream tokenStream = getAnyTokenStream(FIELD_NAME, docId);
QueryScorer scorer = new QueryScorer(query, FIELD_NAME);
Highlighter highlighter = new Highlighter(this, scorer);
highlighter.setTextFragmenter(new SimpleFragmenter(40));
String result = highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired, "...");
if (VERBOSE)
System.out.println("\t" + result);
assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 3);
}
}
use of org.apache.lucene.search.PhraseQuery in project lucene-solr by apache.
the class TestDocument method testPositionIncrementMultiFields.
public void testPositionIncrementMultiFields() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
writer.addDocument(makeDocumentWithFields());
IndexReader reader = writer.getReader();
IndexSearcher searcher = newSearcher(reader);
PhraseQuery query = new PhraseQuery("indexed_not_tokenized", "test1", "test2");
ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;
assertEquals(1, hits.length);
doAssert(searcher.doc(hits[0].doc), true);
writer.close();
reader.close();
dir.close();
}
use of org.apache.lucene.search.PhraseQuery in project lucene-solr by apache.
the class TestQueryBuilder method testPhraseQueryPositionIncrements.
public void testPhraseQueryPositionIncrements() throws Exception {
PhraseQuery.Builder pqBuilder = new PhraseQuery.Builder();
pqBuilder.add(new Term("field", "1"), 0);
pqBuilder.add(new Term("field", "2"), 2);
PhraseQuery expected = pqBuilder.build();
CharacterRunAutomaton stopList = new CharacterRunAutomaton(new RegExp("[sS][tT][oO][pP]").toAutomaton());
Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false, stopList);
QueryBuilder builder = new QueryBuilder(analyzer);
assertEquals(expected, builder.createPhraseQuery("field", "1 stop 2"));
}
Aggregations