use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class TestQueryWrapperFilter method testBasic.
public void testBasic() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(newTextField("field", "value", Field.Store.NO));
writer.addDocument(doc);
IndexReader reader = writer.getReader();
writer.close();
TermQuery termQuery = new TermQuery(new Term("field", "value"));
// should not throw exception with primitive query
QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery);
IndexSearcher searcher = newSearcher(reader);
TopDocs hits = searcher.search(qwf, 10);
assertEquals(1, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(1, hits.totalHits);
// should not throw exception with complex primitive query
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
booleanQuery.add(termQuery, Occur.MUST);
booleanQuery.add(new TermQuery(new Term("field", "missing")), Occur.MUST_NOT);
qwf = new QueryWrapperFilter(termQuery);
hits = searcher.search(qwf, 10);
assertEquals(1, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(1, hits.totalHits);
// should not throw exception with non primitive Query (doesn't implement
// Query#createWeight)
qwf = new QueryWrapperFilter(new FuzzyQuery(new Term("field", "valu")));
hits = searcher.search(qwf, 10);
assertEquals(1, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(1, hits.totalHits);
// test a query with no hits
termQuery = new TermQuery(new Term("field", "not_exist"));
qwf = new QueryWrapperFilter(termQuery);
hits = searcher.search(qwf, 10);
assertEquals(0, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(0, hits.totalHits);
reader.close();
dir.close();
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class PolyFieldTest method testSearchDetails.
@Test
public void testSearchDetails() throws Exception {
SolrCore core = h.getCore();
IndexSchema schema = core.getLatestSchema();
double[] xy = new double[] { 35.0, -79.34 };
String point = xy[0] + "," + xy[1];
//How about some queries?
//don't need a parser for this path currently. This may change
assertU(adoc("id", "0", "home_ns", point));
assertU(commit());
SchemaField home = schema.getField("home_ns");
PointType pt = (PointType) home.getType();
assertEquals(pt.getDimension(), 2);
Query q = pt.getFieldQuery(null, home, point);
assertNotNull(q);
assertTrue(q instanceof BooleanQuery);
//should have two clauses, one for 35.0 and the other for -79.34
BooleanQuery bq = (BooleanQuery) q;
assertEquals(2, bq.clauses().size());
clearIndex();
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class TestSolrCoreParser method testHandySpanQuery.
// test custom query (HandyQueryBuilder) wrapping a SpanQuery
public void testHandySpanQuery() throws IOException, ParserException {
final String lhsXml = "<SpanOr fieldName='contents'>" + "<SpanTerm>rain</SpanTerm>" + "<SpanTerm>spain</SpanTerm>" + "<SpanTerm>plain</SpanTerm>" + "</SpanOr>";
final String rhsXml = "<SpanNear fieldName='contents' slop='2' inOrder='true'>" + "<SpanTerm>sunny</SpanTerm>" + "<SpanTerm>sky</SpanTerm>" + "</SpanNear>";
final Query query = parseHandyQuery(lhsXml, rhsXml);
final BooleanQuery bq = (BooleanQuery) query;
assertEquals(2, bq.clauses().size());
for (int ii = 0; ii < bq.clauses().size(); ++ii) {
final Query clauseQuery = bq.clauses().get(ii).getQuery();
switch(ii) {
case 0:
assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanOrQuery);
break;
case 1:
assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanNearQuery);
break;
default:
fail("unexpected clause index " + ii);
}
}
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class SolrPluginUtilsTest method testDisjunctionMaxQueryParser.
@Test
public void testDisjunctionMaxQueryParser() throws Exception {
Query out;
String t;
SolrQueryRequest req = req("df", "text");
QParser qparser = QParser.getParser("hi", "dismax", req);
DisjunctionMaxQueryParser qp = new SolrPluginUtils.DisjunctionMaxQueryParser(qparser, req.getParams().get("df"));
qp.addAlias("hoss", 0.01f, SolrPluginUtils.parseFieldBoosts("title^2.0 title_stemmed name^1.2 subject^0.5"));
qp.addAlias("test", 0.01f, SolrPluginUtils.parseFieldBoosts("text^2.0"));
qp.addAlias("unused", 1.0f, SolrPluginUtils.parseFieldBoosts("subject^0.5 sind^1.5"));
/* first some sanity tests that don't use aliasing at all */
t = "XXXXXXXX";
out = qp.parse(t);
assertNotNull(t + " sanity test gave back null", out);
assertTrue(t + " sanity test isn't TermQuery: " + out.getClass(), out instanceof TermQuery);
assertEquals(t + " sanity test is wrong field", qp.getDefaultField(), ((TermQuery) out).getTerm().field());
t = "subject:XXXXXXXX";
out = qp.parse(t);
assertNotNull(t + " sanity test gave back null", out);
assertTrue(t + " sanity test isn't TermQuery: " + out.getClass(), out instanceof TermQuery);
assertEquals(t + " sanity test is wrong field", "subject", ((TermQuery) out).getTerm().field());
/* field has untokenzied type, so this should be a term anyway */
t = "sind:\"simple phrase\"";
out = qp.parse(t);
assertNotNull(t + " sanity test gave back null", out);
assertTrue(t + " sanity test isn't TermQuery: " + out.getClass(), out instanceof TermQuery);
assertEquals(t + " sanity test is wrong field", "sind", ((TermQuery) out).getTerm().field());
t = "subject:\"simple phrase\"";
out = qp.parse(t);
assertNotNull(t + " sanity test gave back null", out);
assertTrue(t + " sanity test isn't PhraseQuery: " + out.getClass(), out instanceof PhraseQuery);
assertEquals(t + " sanity test is wrong field", "subject", ((PhraseQuery) out).getTerms()[0].field());
/* now some tests that use aliasing */
/* basic usage of single "term" */
t = "hoss:XXXXXXXX";
out = qp.parse(t);
assertNotNull(t + " was null", out);
assertTrue(t + " wasn't a DMQ:" + out.getClass(), out instanceof DisjunctionMaxQuery);
assertEquals(t + " wrong number of clauses", 4, countItems(((DisjunctionMaxQuery) out).iterator()));
/* odd case, but should still work, DMQ of one clause */
t = "test:YYYYY";
out = qp.parse(t);
assertNotNull(t + " was null", out);
assertTrue(t + " wasn't a DMQ:" + out.getClass(), out instanceof DisjunctionMaxQuery);
assertEquals(t + " wrong number of clauses", 1, countItems(((DisjunctionMaxQuery) out).iterator()));
/* basic usage of multiple "terms" */
t = "hoss:XXXXXXXX test:YYYYY";
out = qp.parse(t);
assertNotNull(t + " was null", out);
assertTrue(t + " wasn't a boolean:" + out.getClass(), out instanceof BooleanQuery);
{
BooleanQuery bq = (BooleanQuery) out;
List<BooleanClause> clauses = new ArrayList<>(bq.clauses());
assertEquals(t + " wrong number of clauses", 2, clauses.size());
Query sub = clauses.get(0).getQuery();
assertTrue(t + " first wasn't a DMQ:" + sub.getClass(), sub instanceof DisjunctionMaxQuery);
assertEquals(t + " first had wrong number of clauses", 4, countItems(((DisjunctionMaxQuery) sub).iterator()));
sub = clauses.get(1).getQuery();
assertTrue(t + " second wasn't a DMQ:" + sub.getClass(), sub instanceof DisjunctionMaxQuery);
assertEquals(t + " second had wrong number of clauses", 1, countItems(((DisjunctionMaxQuery) sub).iterator()));
}
/* a phrase, and a term that is a stop word for some fields */
t = "hoss:\"XXXXXX YYYYY\" hoss:the";
out = qp.parse(t);
assertNotNull(t + " was null", out);
assertTrue(t + " wasn't a boolean:" + out.getClass(), out instanceof BooleanQuery);
{
BooleanQuery bq = (BooleanQuery) out;
List<BooleanClause> clauses = new ArrayList<>(bq.clauses());
assertEquals(t + " wrong number of clauses", 2, clauses.size());
Query sub = clauses.get(0).getQuery();
assertTrue(t + " first wasn't a DMQ:" + sub.getClass(), sub instanceof DisjunctionMaxQuery);
assertEquals(t + " first had wrong number of clauses", 4, countItems(((DisjunctionMaxQuery) sub).iterator()));
sub = clauses.get(1).getQuery();
assertTrue(t + " second wasn't a DMQ:" + sub.getClass(), sub instanceof DisjunctionMaxQuery);
assertEquals(t + " second had wrong number of clauses (stop words)", 2, countItems(((DisjunctionMaxQuery) sub).iterator()));
}
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class SimpleNaiveBayesDocumentClassifier method getWordFreqForClass.
/**
* Returns the number of documents of the input class ( from the whole index or from a subset)
* that contains the word ( in a specific field or in all the fields if no one selected)
*
* @param word the token produced by the analyzer
* @param fieldName the field the word is coming from
* @param term the class term
* @return number of documents of the input class
* @throws java.io.IOException If there is a low-level I/O error
*/
private int getWordFreqForClass(String word, String fieldName, Term term) throws IOException {
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
BooleanQuery.Builder subQuery = new BooleanQuery.Builder();
subQuery.add(new BooleanClause(new TermQuery(new Term(fieldName, word)), BooleanClause.Occur.SHOULD));
booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST));
booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST));
if (query != null) {
booleanQuery.add(query, BooleanClause.Occur.MUST);
}
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
return totalHitCountCollector.getTotalHits();
}
Aggregations