use of org.apache.lucene.search.BooleanQuery in project jackrabbit-oak by apache.
the class LucenePropertyIndex method getFullTextQuery.
static Query getFullTextQuery(final IndexPlan plan, FullTextExpression ft, final Analyzer analyzer, final FulltextQueryTermsProvider augmentor) {
final PlanResult pr = getPlanResult(plan);
// a reference to the query, so it can be set in the visitor
// (a "non-local return")
final AtomicReference<Query> result = new AtomicReference<Query>();
ft.accept(new FullTextVisitor() {
@Override
public boolean visit(FullTextContains contains) {
visitTerm(contains.getPropertyName(), contains.getRawText(), null, contains.isNot());
return true;
}
@Override
public boolean visit(FullTextOr or) {
BooleanQuery q = new BooleanQuery();
for (FullTextExpression e : or.list) {
Query x = getFullTextQuery(plan, e, analyzer, augmentor);
q.add(x, SHOULD);
}
result.set(q);
return true;
}
@Override
public boolean visit(FullTextAnd and) {
BooleanQuery q = new BooleanQuery();
for (FullTextExpression e : and.list) {
Query x = getFullTextQuery(plan, e, analyzer, augmentor);
/* Only unwrap the clause if MUST_NOT(x) */
boolean hasMustNot = false;
if (x instanceof BooleanQuery) {
BooleanQuery bq = (BooleanQuery) x;
if ((bq.getClauses().length == 1) && (bq.getClauses()[0].getOccur() == BooleanClause.Occur.MUST_NOT)) {
hasMustNot = true;
q.add(bq.getClauses()[0]);
}
}
if (!hasMustNot) {
q.add(x, MUST);
}
}
result.set(q);
return true;
}
@Override
public boolean visit(FullTextTerm term) {
return visitTerm(term.getPropertyName(), term.getText(), term.getBoost(), term.isNot());
}
private boolean visitTerm(String propertyName, String text, String boost, boolean not) {
String p = getLuceneFieldName(propertyName, pr);
Query q = tokenToQuery(text, p, pr, analyzer, augmentor);
if (q == null) {
return false;
}
if (boost != null) {
q.setBoost(Float.parseFloat(boost));
}
if (not) {
BooleanQuery bq = new BooleanQuery();
bq.add(q, MUST_NOT);
result.set(bq);
} else {
result.set(q);
}
return true;
}
});
return result.get();
}
use of org.apache.lucene.search.BooleanQuery in project jackrabbit-oak by apache.
the class LuceneIndex method addNodeTypeConstraints.
private static void addNodeTypeConstraints(List<Query> qs, Filter filter) {
BooleanQuery bq = new BooleanQuery();
for (String type : filter.getPrimaryTypes()) {
bq.add(new TermQuery(new Term(JCR_PRIMARYTYPE, type)), SHOULD);
}
for (String type : filter.getMixinTypes()) {
bq.add(new TermQuery(new Term(JCR_MIXINTYPES, type)), SHOULD);
}
qs.add(bq);
}
use of org.apache.lucene.search.BooleanQuery in project jackrabbit-oak by apache.
the class LuceneIndex method addReferenceConstraint.
private static void addReferenceConstraint(String uuid, List<Query> qs, IndexReader reader) {
if (reader == null) {
// getPlan call
qs.add(new TermQuery(new Term("*", uuid)));
return;
}
// reference query
BooleanQuery bq = new BooleanQuery();
Collection<String> fields = MultiFields.getIndexedFields(reader);
for (String f : fields) {
bq.add(new TermQuery(new Term(f, uuid)), SHOULD);
}
qs.add(bq);
}
use of org.apache.lucene.search.BooleanQuery in project tutorials by eugenp.
the class LuceneInMemorySearchTest method givenBooleanQueryWhenFetchedDocumentThenCorrect.
@Test
public void givenBooleanQueryWhenFetchedDocumentThenCorrect() {
InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
inMemoryLuceneIndex.indexDocument("Destination", "Las Vegas singapore car");
inMemoryLuceneIndex.indexDocument("Commutes in singapore", "Bus Car Bikes");
Term term1 = new Term("body", "singapore");
Term term2 = new Term("body", "car");
TermQuery query1 = new TermQuery(term1);
TermQuery query2 = new TermQuery(term2);
BooleanQuery booleanQuery = new BooleanQuery.Builder().add(query1, BooleanClause.Occur.MUST).add(query2, BooleanClause.Occur.MUST).build();
List<Document> documents = inMemoryLuceneIndex.searchIndex(booleanQuery);
Assert.assertEquals(1, documents.size());
}
use of org.apache.lucene.search.BooleanQuery in project OpenOLAT by OpenOLAT.
the class SearchOrderByCallable method call.
@Override
public List<Long> call() {
IndexSearcher searcher = null;
int found = -1;
try {
if (!searchService.existIndex()) {
log.warn("Index does not exist, can't search for queryString: " + queryString);
throw new ServiceNotAvailableException("Index does not exist");
}
searcher = searchService.getIndexSearcher();
BooleanQuery query = searchService.createQuery(queryString, condQueries);
// only search document with an primary key
String idNotNull = AbstractOlatDocument.DB_ID_NAME + ":[* TO *]";
QueryParser idQueryParser = new QueryParser(SearchService.OO_LUCENE_VERSION, idNotNull, searchService.getAnalyzer());
Query idQuery = idQueryParser.parse(idNotNull);
query.add(idQuery, Occur.MUST);
int n = searchService.getSearchModuleConfig().getMaxHits();
TopDocs docs;
if (orderBy != null && orderBy.length > 0 && orderBy[0] != null) {
SortField[] sortFields = new SortField[orderBy.length];
for (int i = 0; i < orderBy.length; i++) {
sortFields[i] = new SortField(orderBy[i].getKey(), SortField.Type.STRING_VAL, orderBy[i].isAsc());
}
Sort sort = new Sort(sortFields);
docs = searcher.search(query, n, sort);
} else {
docs = searcher.search(query, n);
}
int numOfDocs = Math.min(n, docs.totalHits);
Set<String> retrievedFields = new HashSet<String>();
retrievedFields.add(AbstractOlatDocument.DB_ID_NAME);
List<Long> res = new ArrayList<Long>();
for (int i = firstResult; i < numOfDocs && res.size() < maxResults; i++) {
Document doc = searcher.doc(docs.scoreDocs[i].doc, retrievedFields);
String dbKeyStr = doc.get(AbstractOlatDocument.DB_ID_NAME);
if (StringHelper.containsNonWhitespace(dbKeyStr)) {
res.add(Long.parseLong(dbKeyStr));
}
}
found = res.size();
return res;
} catch (Exception naex) {
log.error("", naex);
return null;
} finally {
searchService.releaseIndexSearcher(searcher);
DBFactory.getInstance().commitAndCloseSession();
log.info("queryString=" + queryString + " (" + found + " hits)");
}
}
Aggregations