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

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

Introduction

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

Prototype

char WILDCARD_CHAR

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

Click Source Link

Document

Char equality with support for wildcards

Usage

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);
    }/*w  w w  .j  a v a  2  s.  c  om*/

    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   w  w  w  .  ja  va2 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));
        }
    }
}