use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestQueryParser method testCJKSynonymsAND2.
/** more complex synonyms with default AND operator */
public void testCJKSynonymsAND2() 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 inner2 = new SynonymQuery(new Term(FIELD, "国"), new Term(FIELD, "國"));
expectedB.add(inner2, 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.BoostQuery in project lucene-solr by apache.
the class TestQueryParser method testSynonymsPhrase.
/** forms multiphrase query */
public void testSynonymsPhrase() throws Exception {
MultiPhraseQuery.Builder expectedQBuilder = new MultiPhraseQuery.Builder();
expectedQBuilder.add(new Term(FIELD, "old"));
expectedQBuilder.add(new Term[] { new Term(FIELD, "dogs"), new Term(FIELD, "dog") });
QueryParser qp = new QueryParser(FIELD, new MockSynonymAnalyzer());
assertEquals(expectedQBuilder.build(), qp.parse("\"old dogs\""));
qp.setDefaultOperator(Operator.AND);
assertEquals(expectedQBuilder.build(), qp.parse("\"old dogs\""));
BoostQuery expected = new BoostQuery(expectedQBuilder.build(), 2f);
assertEquals(expected, qp.parse("\"old dogs\"^2"));
expectedQBuilder.setSlop(3);
expected = new BoostQuery(expectedQBuilder.build(), 2f);
assertEquals(expected, qp.parse("\"old dogs\"~3^2"));
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestQueryParser method testStarParsing.
@Override
public void testStarParsing() throws Exception {
final int[] type = new int[1];
QueryParser qp = new QueryParser(FIELD, new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)) {
@Override
protected Query getWildcardQuery(String field, String termStr) {
// override error checking of superclass
type[0] = 1;
return new TermQuery(new Term(field, termStr));
}
@Override
protected Query getPrefixQuery(String field, String termStr) {
// override error checking of superclass
type[0] = 2;
return new TermQuery(new Term(field, termStr));
}
@Override
protected Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException {
type[0] = 3;
return super.getFieldQuery(field, queryText, quoted);
}
};
TermQuery tq;
tq = (TermQuery) qp.parse("foo:zoo*");
assertEquals("zoo", tq.getTerm().text());
assertEquals(2, type[0]);
BoostQuery bq = (BoostQuery) qp.parse("foo:zoo*^2");
tq = (TermQuery) bq.getQuery();
assertEquals("zoo", tq.getTerm().text());
assertEquals(2, type[0]);
assertEquals(bq.getBoost(), 2, 0);
tq = (TermQuery) qp.parse("foo:*");
assertEquals("*", tq.getTerm().text());
// could be a valid prefix query in the future too
assertEquals(1, type[0]);
bq = (BoostQuery) qp.parse("foo:*^2");
tq = (TermQuery) bq.getQuery();
assertEquals("*", tq.getTerm().text());
assertEquals(1, type[0]);
assertEquals(bq.getBoost(), 2, 0);
tq = (TermQuery) qp.parse("*:foo");
assertEquals("*", tq.getTerm().field());
assertEquals("foo", tq.getTerm().text());
assertEquals(3, type[0]);
tq = (TermQuery) qp.parse("*:*");
assertEquals("*", tq.getTerm().field());
assertEquals("*", tq.getTerm().text());
// could be handled as a prefix query in the
assertEquals(1, type[0]);
// future
tq = (TermQuery) qp.parse("(*:*)");
assertEquals("*", tq.getTerm().field());
assertEquals("*", tq.getTerm().text());
assertEquals(1, type[0]);
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestQueryParser method testCJKSynonym.
/** simple CJK synonym test */
public void testCJKSynonym() throws Exception {
Query expected = new SynonymQuery(new Term(FIELD, "国"), new Term(FIELD, "國"));
QueryParser qp = new QueryParser(FIELD, new MockCJKSynonymAnalyzer());
assertEquals(expected, qp.parse("国"));
qp.setDefaultOperator(Operator.AND);
assertEquals(expected, qp.parse("国"));
expected = new BoostQuery(expected, 2f);
assertEquals(expected, qp.parse("国^2"));
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class UserInputQueryBuilder method getQuery.
/* (non-Javadoc)
* @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
*/
@Override
public Query getQuery(Element e) throws ParserException {
String text = DOMUtils.getText(e);
try {
Query q = null;
if (unSafeParser != null) {
//synchronize on unsafe parser
synchronized (unSafeParser) {
q = unSafeParser.parse(text);
}
} else {
String fieldName = DOMUtils.getAttribute(e, "fieldName", defaultField);
//Create new parser
QueryParser parser = createQueryParser(fieldName, analyzer);
q = parser.parse(text);
}
float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
return new BoostQuery(q, boost);
} catch (ParseException e1) {
throw new ParserException(e1.getMessage());
}
}
Aggregations