Search in sources :

Example 91 with BooleanQuery

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();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) PlanResult(org.apache.jackrabbit.oak.plugins.index.lucene.IndexPlanner.PlanResult) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) FullTextOr(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) AtomicReference(java.util.concurrent.atomic.AtomicReference) FullTextVisitor(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression)

Example 92 with BooleanQuery

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);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) TermFactory.newPathTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newPathTerm) TermFactory.newFulltextTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newFulltextTerm)

Example 93 with BooleanQuery

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);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) TermFactory.newPathTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newPathTerm) TermFactory.newFulltextTerm(org.apache.jackrabbit.oak.plugins.index.lucene.TermFactory.newFulltextTerm)

Example 94 with BooleanQuery

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());
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Test(org.junit.Test)

Example 95 with BooleanQuery

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)");
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) AbstractOlatDocument(org.olat.search.model.AbstractOlatDocument) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Sort(org.apache.lucene.search.Sort) HashSet(java.util.HashSet)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)297 TermQuery (org.apache.lucene.search.TermQuery)176 Term (org.apache.lucene.index.Term)144 Query (org.apache.lucene.search.Query)129 BooleanClause (org.apache.lucene.search.BooleanClause)89 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)70 BoostQuery (org.apache.lucene.search.BoostQuery)58 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)52 PhraseQuery (org.apache.lucene.search.PhraseQuery)50 ArrayList (java.util.ArrayList)47 TopDocs (org.apache.lucene.search.TopDocs)47 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)43 WildcardQuery (org.apache.lucene.search.WildcardQuery)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)40 IndexReader (org.apache.lucene.index.IndexReader)39 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 Test (org.junit.Test)39 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)38 Document (org.apache.lucene.document.Document)36 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)33