use of org.apache.lucene.search.SynonymQuery in project lucene-solr by apache.
the class TestQueryParser method testCJKSynonymsAND.
/** synonyms with default AND operator */
public void testCJKSynonymsAND() throws Exception {
BooleanQuery.Builder expectedB = new BooleanQuery.Builder();
expectedB.add(new TermQuery(new Term(FIELD, "中")), BooleanClause.Occur.MUST);
Query inner = new SynonymQuery(new Term(FIELD, "国"), new Term(FIELD, "國"));
expectedB.add(inner, BooleanClause.Occur.MUST);
Query expected = expectedB.build();
QueryParser qp = new QueryParser(FIELD, new MockCJKSynonymAnalyzer());
qp.setDefaultOperator(Operator.AND);
assertEquals(expected, qp.parse("中国"));
expected = new BoostQuery(expected, 2f);
assertEquals(expected, qp.parse("中国^2"));
}
use of org.apache.lucene.search.SynonymQuery in project lucene-solr by apache.
the class TestQueryParser method testEnableGraphQueries.
public void testEnableGraphQueries() throws Exception {
QueryParser dumb = new QueryParser("field", new Analyzer1());
dumb.setSplitOnWhitespace(false);
dumb.setEnableGraphQueries(false);
TermQuery guinea = new TermQuery(new Term("field", "guinea"));
TermQuery pig = new TermQuery(new Term("field", "pig"));
TermQuery cavy = new TermQuery(new Term("field", "cavy"));
// A multi-word synonym source will just form a boolean query when graph queries are disabled:
Query inner = new SynonymQuery(new Term[] { new Term("field", "cavy"), new Term("field", "guinea") });
BooleanQuery.Builder b = new BooleanQuery.Builder();
b.add(inner, BooleanClause.Occur.SHOULD);
b.add(pig, BooleanClause.Occur.SHOULD);
BooleanQuery query = b.build();
assertEquals(query, dumb.parse("guinea pig"));
}
use of org.apache.lucene.search.SynonymQuery in project lucene-solr by apache.
the class TestQueryParser method testCJKSynonymsOR2.
/** more complex synonyms with default OR operator */
public void testCJKSynonymsOR2() throws Exception {
BooleanQuery.Builder expectedB = new BooleanQuery.Builder();
expectedB.add(new TermQuery(new Term(FIELD, "中")), BooleanClause.Occur.SHOULD);
SynonymQuery inner = new SynonymQuery(new Term(FIELD, "国"), new Term(FIELD, "國"));
expectedB.add(inner, BooleanClause.Occur.SHOULD);
SynonymQuery inner2 = new SynonymQuery(new Term(FIELD, "国"), new Term(FIELD, "國"));
expectedB.add(inner2, BooleanClause.Occur.SHOULD);
Query expected = expectedB.build();
QueryParser qp = new QueryParser(FIELD, new MockCJKSynonymAnalyzer());
assertEquals(expected, qp.parse("中国国"));
expected = new BoostQuery(expected, 2f);
assertEquals(expected, qp.parse("中国国^2"));
}
use of org.apache.lucene.search.SynonymQuery in project lucene-solr by apache.
the class TestQueryParser method testNewFieldQuery.
@Override
public void testNewFieldQuery() throws Exception {
/** ordinary behavior, synonyms form uncoordinated boolean query */
QueryParser dumb = new QueryParser(FIELD, new Analyzer1());
Query expanded = new SynonymQuery(new Term(FIELD, "dogs"), new Term(FIELD, "dog"));
assertEquals(expanded, dumb.parse("\"dogs\""));
/** even with the phrase operator the behavior is the same */
assertEquals(expanded, dumb.parse("dogs"));
/**
* custom behavior, the synonyms are expanded, unless you use quote operator
*/
QueryParser smart = new SmartQueryParser();
assertEquals(expanded, smart.parse("dogs"));
Query unexpanded = new TermQuery(new Term(FIELD, "dogs"));
assertEquals(unexpanded, smart.parse("\"dogs\""));
}
use of org.apache.lucene.search.SynonymQuery in project lucene-solr by apache.
the class SynonymTokenizer method testHighlightingSynonymQuery.
public void testHighlightingSynonymQuery() throws Exception {
searcher = newSearcher(reader);
Query query = new SynonymQuery(new Term(FIELD_NAME, "jfk"), new Term(FIELD_NAME, "kennedy"));
QueryScorer scorer = new QueryScorer(query, FIELD_NAME);
Highlighter highlighter = new Highlighter(scorer);
TokenStream stream = getAnyTokenStream(FIELD_NAME, 2);
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
highlighter.setTextFragmenter(fragmenter);
String storedField = searcher.doc(2).get(FIELD_NAME);
String fragment = highlighter.getBestFragment(stream, storedField);
assertEquals("<B>JFK</B> has been shot", fragment);
stream = getAnyTokenStream(FIELD_NAME, 3);
storedField = searcher.doc(3).get(FIELD_NAME);
fragment = highlighter.getBestFragment(stream, storedField);
assertEquals("John <B>Kennedy</B> has been shot", fragment);
}
Aggregations