Example usage for org.apache.lucene.search WildcardQuery WILDCARD_STRING

List of usage examples for org.apache.lucene.search WildcardQuery WILDCARD_STRING

Introduction

In this page you can find the example usage for org.apache.lucene.search WildcardQuery WILDCARD_STRING.

Prototype

char WILDCARD_STRING

To view the source code for org.apache.lucene.search WildcardQuery WILDCARD_STRING.

Click Source Link

Document

String equality with support for wildcards

Usage

From source file:SimpleNaiveBayesClassifier.java

License:Apache License

/**
 * count the number of documents in the index having at least a value for the 'class' field
 *
 * @return the no. of documents having a value for the 'class' field
 * @throws IOException if accessing to term vectors or search fails
 *//*www. j  a  va 2 s . com*/
protected int countDocsWithClass() throws IOException {
    int docCount = MultiFields.getTerms(this.leafReader, this.classFieldName).getDocCount();
    if (docCount == -1) { // in case codec doesn't support getDocCount
        TotalHitCountCollector classQueryCountCollector = new TotalHitCountCollector();
        BooleanQuery.Builder q = new BooleanQuery.Builder();
        q.add(new BooleanClause(
                new WildcardQuery(new Term(classFieldName, String.valueOf(WildcardQuery.WILDCARD_STRING))),
                BooleanClause.Occur.MUST));
        if (query != null) {
            q.add(query, BooleanClause.Occur.MUST);
        }
        indexSearcher.search(q.build(), classQueryCountCollector);
        docCount = classQueryCountCollector.getTotalHits();
    }
    return docCount;
}

From source file:com.meizu.nlp.classification.SimpleNaiveBayesClassifier.java

License:Apache License

/**
 * count the number of documents in the index having at least a value for the 'class' field
 *
 * @return the no. of documents having a value for the 'class' field
 * @throws IOException if accessing to term vectors or search fails
 *//*from w  w w . j  a  v a  2s.  c o  m*/
protected int countDocsWithClass() throws IOException {
    int docCount = MultiFields.getTerms(this.leafReader, this.classFieldName).getDocCount();
    if (docCount == -1) { // in case codec doesn't support getDocCount
        TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
        BooleanQuery q = new BooleanQuery();
        q.add(new BooleanClause(
                new WildcardQuery(new Term(classFieldName, String.valueOf(WildcardQuery.WILDCARD_STRING))),
                BooleanClause.Occur.MUST));
        if (query != null) {
            q.add(query, BooleanClause.Occur.MUST);
        }
        indexSearcher.search(q, totalHitCountCollector);
        docCount = totalHitCountCollector.getTotalHits();
    }
    return docCount;
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndex.java

License:Apache License

private static void addNonFullTextConstraints(List<Query> qs, Filter filter, IndexReader reader,
        Analyzer analyzer, IndexDefinition indexDefinition) {
    if (!filter.matchesAllTypes()) {
        addNodeTypeConstraints(qs, filter);
    }/*from  w  w  w. j  ava2  s.  c  o  m*/

    String path = filter.getPath();
    switch (filter.getPathRestriction()) {
    case ALL_CHILDREN:
        if (USE_PATH_RESTRICTION) {
            if ("/".equals(path)) {
                break;
            }
            if (!path.endsWith("/")) {
                path += "/";
            }
            qs.add(new PrefixQuery(newPathTerm(path)));
        }
        break;
    case DIRECT_CHILDREN:
        if (USE_PATH_RESTRICTION) {
            if (!path.endsWith("/")) {
                path += "/";
            }
            qs.add(new PrefixQuery(newPathTerm(path)));
        }
        break;
    case EXACT:
        qs.add(new TermQuery(newPathTerm(path)));
        break;
    case PARENT:
        if (denotesRoot(path)) {
            // there's no parent of the root node
            // we add a path that can not possibly occur because there
            // is no way to say "match no documents" in Lucene
            qs.add(new TermQuery(new Term(FieldNames.PATH, "///")));
        } else {
            qs.add(new TermQuery(newPathTerm(getParentPath(path))));
        }
        break;
    case NO_RESTRICTION:
        break;
    }

    //Fulltext index definition used by LuceneIndex only works with old format
    //which is not nodeType based. So just use the nt:base index
    IndexingRule rule = indexDefinition.getApplicableIndexingRule(JcrConstants.NT_BASE);
    for (PropertyRestriction pr : filter.getPropertyRestrictions()) {

        if (pr.first == null && pr.last == null) {
            // we only support equality or range queries,
            // but not "in", "is null", "is not null" 
            // queries (OAK-1208)
            continue;
        }

        // check excluded properties and types
        if (isExcludedProperty(pr, rule)) {
            continue;
        }

        String name = pr.propertyName;
        if (QueryImpl.REP_EXCERPT.equals(name) || QueryImpl.OAK_SCORE_EXPLANATION.equals(name)
                || QueryImpl.REP_FACET.equals(name)) {
            continue;
        }
        if (JCR_PRIMARYTYPE.equals(name)) {
            continue;
        }
        if (QueryConstants.RESTRICTION_LOCAL_NAME.equals(name)) {
            continue;
        }

        if (skipTokenization(name)) {
            qs.add(new TermQuery(new Term(name, pr.first.getValue(STRING))));
            continue;
        }

        String first = null;
        String last = null;
        boolean isLike = pr.isLike;

        // TODO what to do with escaped tokens?
        if (pr.first != null) {
            first = pr.first.getValue(STRING);
            first = first.replace("\\", "");
        }
        if (pr.last != null) {
            last = pr.last.getValue(STRING);
            last = last.replace("\\", "");
        }

        if (isLike) {
            first = first.replace('%', WildcardQuery.WILDCARD_STRING);
            first = first.replace('_', WildcardQuery.WILDCARD_CHAR);

            int indexOfWS = first.indexOf(WildcardQuery.WILDCARD_STRING);
            int indexOfWC = first.indexOf(WildcardQuery.WILDCARD_CHAR);
            int len = first.length();

            if (indexOfWS == len || indexOfWC == len) {
                // remove trailing "*" for prefixquery
                first = first.substring(0, first.length() - 1);
                if (JCR_PATH.equals(name)) {
                    qs.add(new PrefixQuery(newPathTerm(first)));
                } else {
                    qs.add(new PrefixQuery(new Term(name, first)));
                }
            } else {
                if (JCR_PATH.equals(name)) {
                    qs.add(new WildcardQuery(newPathTerm(first)));
                } else {
                    qs.add(new WildcardQuery(new Term(name, first)));
                }
            }
            continue;
        }

        if (first != null && first.equals(last) && pr.firstIncluding && pr.lastIncluding) {
            if (JCR_PATH.equals(name)) {
                qs.add(new TermQuery(newPathTerm(first)));
            } else {
                if ("*".equals(name)) {
                    addReferenceConstraint(first, qs, reader);
                } else {
                    for (String t : tokenize(first, analyzer)) {
                        qs.add(new TermQuery(new Term(name, t)));
                    }
                }
            }
            continue;
        }

        first = tokenizeAndPoll(first, analyzer);
        last = tokenizeAndPoll(last, analyzer);
        qs.add(TermRangeQuery.newStringRange(name, first, last, pr.firstIncluding, pr.lastIncluding));
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LucenePropertyIndex.java

License:Apache License

private static Query createLikeQuery(String name, String first) {
    first = first.replace('%', WildcardQuery.WILDCARD_STRING);
    first = first.replace('_', WildcardQuery.WILDCARD_CHAR);

    int indexOfWS = first.indexOf(WildcardQuery.WILDCARD_STRING);
    int indexOfWC = first.indexOf(WildcardQuery.WILDCARD_CHAR);
    int len = first.length();

    if (indexOfWS == len || indexOfWC == len) {
        // remove trailing "*" for prefixquery
        first = first.substring(0, first.length() - 1);
        if (JCR_PATH.equals(name)) {
            return new PrefixQuery(newPathTerm(first));
        } else {//from   ww  w  .jav a 2  s  . c om
            return new PrefixQuery(new Term(name, first));
        }
    } else {
        if (JCR_PATH.equals(name)) {
            return new WildcardQuery(newPathTerm(first));
        } else {
            return new WildcardQuery(new Term(name, first));
        }
    }
}

From source file:Others.SampleLuceneClassifier.java

/**
 * count the number of documents in the index having at least a value for the 'class' field
 *
 * @return the no. of documents having a value for the 'class' field
 * @throws IOException if accessing to term vectors or search fails
 *//* w w w  . j  av a  2 s  . c om*/
protected int countDocsWithClass() throws IOException {
    Terms terms = MultiFields.getTerms(this.indexReader, this.classFieldName);
    int docCount;
    if (terms == null || terms.getDocCount() == -1) { // in case codec doesn't support getDocCount
        TotalHitCountCollector classQueryCountCollector = new TotalHitCountCollector();
        BooleanQuery.Builder q = new BooleanQuery.Builder();
        q.add(new BooleanClause(
                new WildcardQuery(new Term(classFieldName, String.valueOf(WildcardQuery.WILDCARD_STRING))),
                BooleanClause.Occur.MUST));
        if (query != null) {
            q.add(query, BooleanClause.Occur.MUST);
        }
        indexSearcher.search(q.build(), classQueryCountCollector);
        docCount = classQueryCountCollector.getTotalHits();
    } else {
        docCount = terms.getDocCount();
    }
    return docCount;
}

From source file:se.inera.intyg.webcert.web.service.diagnos.repo.DiagnosRepositoryImpl.java

License:Open Source License

@Override
public List<Diagnos> searchDiagnosisByDescription(String searchString, int nbrOfResults) {
    if (Strings.isNullOrEmpty(searchString)) {
        return Collections.emptyList();
    }/*www  .j  a  va2s. c o  m*/
    BooleanQuery query = new BooleanQuery();
    try (StandardAnalyzer analyzer = new StandardAnalyzer()) {
        TokenStream tokenStream = analyzer.tokenStream(DESC, searchString);
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        tokenStream.reset();
        while (tokenStream.incrementToken()) {
            String term = WildcardQuery.WILDCARD_STRING + charTermAttribute.toString()
                    + WildcardQuery.WILDCARD_STRING;
            query.add(new WildcardQuery(new Term(DESC, term)), BooleanClause.Occur.MUST);
        }
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred in lucene index search", e);
    }
    return searchDiagnosisByQuery(query, nbrOfResults);
}