Search in sources :

Example 11 with FullTextExpression

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression in project jackrabbit-oak by apache.

the class IndexPlannerTest method fullTextQuery_DisjointPropertyPaths.

@Test
public void fullTextQuery_DisjointPropertyPaths() throws Exception {
    NodeBuilder defn = newLucenePropertyIndexDefinition(builder, "test", of("foo", "bar"), "async");
    // Index all props and then perform fulltext
    defn = IndexDefinition.updateDefinition(defn.getNodeState().builder());
    NodeBuilder foob = getNode(defn, "indexRules/nt:base/properties/foo");
    foob.setProperty(LuceneIndexConstants.PROP_NAME, "foo");
    foob.setProperty(LuceneIndexConstants.PROP_ANALYZED, true);
    NodeBuilder barb = getNode(defn, "indexRules/nt:base/properties/bar");
    barb.setProperty(LuceneIndexConstants.PROP_NAME, "bar");
    barb.setProperty(LuceneIndexConstants.PROP_ANALYZED, true);
    FullTextExpression fooExp = FullTextParser.parse("metadata/bar", "mountain OR valley");
    FullTextExpression barExp = FullTextParser.parse("jcr:content/foo", "mountain OR valley");
    FullTextExpression exp = new FullTextAnd(Arrays.asList(fooExp, barExp));
    IndexPlanner planner = createPlannerForFulltext(defn.getNodeState(), exp);
    // No plan for unindex property
    assertNull(planner.getPlan());
}
Also used : FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 12 with FullTextExpression

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression in project jackrabbit-oak by apache.

the class IndexPlannerTest method relativeProperty_FullText.

@Test
public void relativeProperty_FullText() throws Exception {
    IndexDefinitionBuilder defnb = new IndexDefinitionBuilder();
    defnb.indexRule("nt:base").property("foo").propertyIndex();
    defnb.aggregateRule("nt:base").include("*");
    IndexDefinition defn = new IndexDefinition(root, defnb.build(), "/foo");
    IndexNode node = createIndexNode(defn);
    FilterImpl filter = createFilter("nt:base");
    filter.restrictProperty("jcr:content/foo", Operator.EQUAL, PropertyValues.newString("/bar"));
    FullTextExpression ft = FullTextParser.parse("jcr:content/*", "mountain OR valley");
    filter.setFullTextConstraint(ft);
    IndexPlanner planner = new IndexPlanner(node, "/foo", filter, Collections.<OrderEntry>emptyList());
    QueryIndex.IndexPlan plan = planner.getPlan();
    IndexPlanner.PlanResult pr = pr(plan);
    assertFalse(pr.hasProperty("jcr:content/foo"));
}
Also used : FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) LuceneIndexHelper.newLucenePropertyIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLucenePropertyIndexDefinition) LuceneIndexHelper.newLuceneIndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition) IndexDefinitionBuilder(org.apache.jackrabbit.oak.plugins.index.lucene.util.IndexDefinitionBuilder) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression) QueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex) Test(org.junit.Test)

Example 13 with FullTextExpression

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression in project jackrabbit-oak by apache.

the class FullTextSearchImpl method getFullTextConstraint.

@Override
public FullTextExpression getFullTextConstraint(SelectorImpl s) {
    if (!s.equals(selector)) {
        return null;
    }
    PropertyValue v = fullTextSearchExpression.currentValue();
    try {
        String p = propertyName;
        if (relativePath != null) {
            if (p == null) {
                p = "*";
            }
            p = PathUtils.concat(relativePath, p);
        }
        String p2 = normalizePropertyName(p);
        String rawText = getRawText(v);
        FullTextExpression e = FullTextParser.parse(p2, rawText);
        return new FullTextContains(p2, rawText, e);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Invalid expression: " + fullTextSearchExpression, e);
    }
}
Also used : FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression) PropertyValue(org.apache.jackrabbit.oak.api.PropertyValue) ParseException(java.text.ParseException)

Example 14 with FullTextExpression

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression in project jackrabbit-oak by apache.

the class FilterQueryParser method parseFullTextExpression.

private static String parseFullTextExpression(FullTextExpression ft, final OakSolrConfiguration configuration) {
    final StringBuilder fullTextString = new StringBuilder();
    ft.accept(new FullTextVisitor() {

        @Override
        public boolean visit(FullTextOr or) {
            fullTextString.append('(');
            for (int i = 0; i < or.list.size(); i++) {
                FullTextExpression e = or.list.get(i);
                if (e.toString().contains("\"OR\"")) {
                    continue;
                }
                if (i > 0 && i < or.list.size()) {
                    fullTextString.append(" OR ");
                }
                String orTerm = parseFullTextExpression(e, configuration);
                fullTextString.append(orTerm);
            }
            fullTextString.append(')');
            fullTextString.append(' ');
            return true;
        }

        @Override
        public boolean visit(FullTextContains contains) {
            return contains.getBase().accept(this);
        }

        @Override
        public boolean visit(FullTextAnd and) {
            fullTextString.append('(');
            for (int i = 0; i < and.list.size(); i++) {
                FullTextExpression e = and.list.get(i);
                if (e.toString().contains("\"AND\"")) {
                    continue;
                }
                if (i > 0 && i < and.list.size()) {
                    fullTextString.append(" AND ");
                }
                String andTerm = parseFullTextExpression(e, configuration);
                fullTextString.append(andTerm);
            }
            fullTextString.append(')');
            fullTextString.append(' ');
            return true;
        }

        @Override
        public boolean visit(FullTextTerm term) {
            if (term.isNot()) {
                fullTextString.append('-');
            }
            String p = term.getPropertyName();
            if (p != null && p.indexOf('/') >= 0) {
                p = getName(p);
            }
            if (p == null || "*".equals(p)) {
                p = configuration.getCatchAllField();
            }
            fullTextString.append(partialEscape(p));
            fullTextString.append(':');
            String termText = term.getText();
            if (termText.indexOf(' ') > 0) {
                fullTextString.append('"');
            }
            fullTextString.append(termText.replace("/", "\\/").replace(":", "\\:"));
            if (termText.indexOf(' ') > 0) {
                fullTextString.append('"');
            }
            String boost = term.getBoost();
            if (boost != null) {
                fullTextString.append('^');
                fullTextString.append(boost);
            }
            fullTextString.append(' ');
            return true;
        }
    });
    return fullTextString.toString();
}
Also used : FullTextVisitor(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor) FullTextTerm(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm) FullTextOr(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr) FullTextContains(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains) FullTextAnd(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression)

Example 15 with FullTextExpression

use of org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression in project jackrabbit-oak by apache.

the class FilterQueryParser method getQuery.

static SolrQuery getQuery(Filter filter, QueryIndex.IndexPlan plan, OakSolrConfiguration configuration) {
    SolrQuery solrQuery = new SolrQuery();
    setDefaults(solrQuery, configuration);
    StringBuilder queryBuilder = new StringBuilder();
    FullTextExpression ft = filter.getFullTextConstraint();
    if (ft != null) {
        queryBuilder.append(parseFullTextExpression(ft, configuration));
        queryBuilder.append(' ');
    } else if (filter.getFulltextConditions() != null) {
        Collection<String> fulltextConditions = filter.getFulltextConditions();
        for (String fulltextCondition : fulltextConditions) {
            queryBuilder.append(fulltextCondition).append(" ");
        }
    }
    List<QueryIndex.OrderEntry> sortOrder = plan.getSortOrder();
    if (sortOrder != null) {
        for (QueryIndex.OrderEntry orderEntry : sortOrder) {
            SolrQuery.ORDER order;
            if (QueryIndex.OrderEntry.Order.ASCENDING.equals(orderEntry.getOrder())) {
                order = SolrQuery.ORDER.asc;
            } else {
                order = SolrQuery.ORDER.desc;
            }
            String sortingField;
            if (JcrConstants.JCR_PATH.equals(orderEntry.getPropertyName())) {
                sortingField = partialEscape(configuration.getPathField()).toString();
            } else if (JcrConstants.JCR_SCORE.equals(orderEntry.getPropertyName())) {
                sortingField = "score";
            } else {
                if (orderEntry.getPropertyName().indexOf('/') >= 0) {
                    log.warn("cannot sort on relative properties, ignoring {} clause", orderEntry);
                    // sorting by relative properties not supported until index time aggregation is supported
                    continue;
                }
                sortingField = partialEscape(getSortingField(orderEntry.getPropertyType().tag(), orderEntry.getPropertyName())).toString();
            }
            solrQuery.addOrUpdateSort(sortingField, order);
        }
    }
    Collection<Filter.PropertyRestriction> propertyRestrictions = filter.getPropertyRestrictions();
    if (propertyRestrictions != null && !propertyRestrictions.isEmpty()) {
        for (Filter.PropertyRestriction pr : propertyRestrictions) {
            if (pr.isNullRestriction()) {
                // can not use full "x is null"
                continue;
            }
            // facets
            if (QueryConstants.REP_FACET.equals(pr.propertyName)) {
                solrQuery.setFacetMinCount(1);
                solrQuery.setFacet(true);
                String value = pr.first.getValue(Type.STRING);
                solrQuery.addFacetField(value.substring(QueryConstants.REP_FACET.length() + 1, value.length() - 1) + "_facet");
            }
            // native query support
            if (SolrQueryIndex.NATIVE_SOLR_QUERY.equals(pr.propertyName) || SolrQueryIndex.NATIVE_LUCENE_QUERY.equals(pr.propertyName)) {
                String nativeQueryString = String.valueOf(pr.first.getValue(pr.first.getType()));
                if (isSupportedHttpRequest(nativeQueryString)) {
                    // pass through the native HTTP Solr request
                    String requestHandlerString = nativeQueryString.substring(0, nativeQueryString.indexOf('?'));
                    if (!"select".equals(requestHandlerString)) {
                        if (requestHandlerString.charAt(0) != '/') {
                            requestHandlerString = "/" + requestHandlerString;
                        }
                        solrQuery.setRequestHandler(requestHandlerString);
                    }
                    String parameterString = nativeQueryString.substring(nativeQueryString.indexOf('?') + 1);
                    for (String param : parameterString.split("&")) {
                        String[] kv = param.split("=");
                        if (kv.length != 2) {
                            throw new RuntimeException("Unparsable native HTTP Solr query");
                        } else {
                            // more like this
                            if ("/mlt".equals(requestHandlerString)) {
                                if ("stream.body".equals(kv[0])) {
                                    kv[0] = "q";
                                    String mltFlString = "mlt.fl=";
                                    int mltFlIndex = parameterString.indexOf(mltFlString);
                                    if (mltFlIndex > -1) {
                                        int beginIndex = mltFlIndex + mltFlString.length();
                                        int endIndex = parameterString.indexOf('&', beginIndex);
                                        String fields;
                                        if (endIndex > beginIndex) {
                                            fields = parameterString.substring(beginIndex, endIndex);
                                        } else {
                                            fields = parameterString.substring(beginIndex);
                                        }
                                        kv[1] = "_query_:\"{!dismax qf=" + fields + " q.op=OR}" + kv[1] + "\"";
                                    }
                                }
                                if ("mlt.fl".equals(kv[0]) && ":path".equals(kv[1])) {
                                    // rep:similar passes the path of the node to find similar documents for in the :path
                                    // but needs its indexed content to find similar documents
                                    kv[1] = configuration.getCatchAllField();
                                }
                            }
                            if ("/spellcheck".equals(requestHandlerString)) {
                                if ("term".equals(kv[0])) {
                                    kv[0] = "spellcheck.q";
                                }
                                solrQuery.setParam("spellcheck", true);
                            }
                            if ("/suggest".equals(requestHandlerString)) {
                                if ("term".equals(kv[0])) {
                                    kv[0] = "suggest.q";
                                }
                                solrQuery.setParam("suggest", true);
                            }
                            solrQuery.setParam(kv[0], kv[1]);
                        }
                    }
                    return solrQuery;
                } else {
                    queryBuilder.append(nativeQueryString);
                }
            } else {
                if (SolrQueryIndex.isIgnoredProperty(pr, configuration)) {
                    continue;
                }
                String first = null;
                if (pr.first != null) {
                    first = partialEscape(String.valueOf(pr.first.getValue(pr.first.getType()))).toString();
                }
                String last = null;
                if (pr.last != null) {
                    last = partialEscape(String.valueOf(pr.last.getValue(pr.last.getType()))).toString();
                }
                String prField = configuration.getFieldForPropertyRestriction(pr);
                CharSequence fieldName = partialEscape(prField != null ? prField : pr.propertyName);
                if ("jcr\\:path".equals(fieldName.toString())) {
                    queryBuilder.append(configuration.getPathField());
                    queryBuilder.append(':');
                    queryBuilder.append(first);
                } else {
                    if (pr.first != null && pr.last != null && pr.first.equals(pr.last)) {
                        queryBuilder.append(fieldName).append(':');
                        queryBuilder.append(first);
                    } else if (pr.first == null && pr.last == null) {
                        if (!queryBuilder.toString().contains(fieldName + ":")) {
                            queryBuilder.append(fieldName).append(':');
                            queryBuilder.append('*');
                        }
                    } else if ((pr.first != null && pr.last == null) || (pr.last != null && pr.first == null) || (!pr.first.equals(pr.last))) {
                        // TODO : need to check if this works for all field types (most likely not!)
                        queryBuilder.append(fieldName).append(':');
                        queryBuilder.append(createRangeQuery(first, last, pr.firstIncluding, pr.lastIncluding));
                    } else if (pr.isLike) {
                        // TODO : the current parameter substitution is not expected to work well
                        queryBuilder.append(fieldName).append(':');
                        queryBuilder.append(partialEscape(String.valueOf(pr.first.getValue(pr.first.getType())).replace('%', '*').replace('_', '?')));
                    } else {
                        throw new RuntimeException("[unexpected!] not handled case");
                    }
                }
            }
            queryBuilder.append(" ");
        }
    }
    if (configuration.useForPrimaryTypes()) {
        String[] pts = filter.getPrimaryTypes().toArray(new String[filter.getPrimaryTypes().size()]);
        StringBuilder ptQueryBuilder = new StringBuilder();
        for (int i = 0; i < pts.length; i++) {
            String pt = pts[i];
            if (i == 0) {
                ptQueryBuilder.append("(");
            }
            if (i > 0 && i < pts.length) {
                ptQueryBuilder.append("OR ");
            }
            ptQueryBuilder.append("jcr\\:primaryType").append(':').append(partialEscape(pt)).append(" ");
            if (i == pts.length - 1) {
                ptQueryBuilder.append(")");
                ptQueryBuilder.append(' ');
            }
        }
        solrQuery.addFilterQuery(ptQueryBuilder.toString());
    }
    if (filter.getQueryStatement() != null && filter.getQueryStatement().contains(QueryConstants.REP_EXCERPT)) {
        if (!solrQuery.getHighlight()) {
            // enable highlighting
            solrQuery.setHighlight(true);
            // defaults
            solrQuery.set("hl.fl", "*");
            solrQuery.set("hl.encoder", "html");
            solrQuery.set("hl.mergeContiguous", true);
            solrQuery.setHighlightSimplePre("<strong>");
            solrQuery.setHighlightSimplePost("</strong>");
        }
    }
    if (configuration.useForPathRestrictions()) {
        Filter.PathRestriction pathRestriction = filter.getPathRestriction();
        if (pathRestriction != null) {
            String path = purgePath(filter, plan.getPathPrefix());
            String fieldName = configuration.getFieldForPathRestriction(pathRestriction);
            if (fieldName != null) {
                if (pathRestriction.equals(Filter.PathRestriction.ALL_CHILDREN)) {
                    solrQuery.addFilterQuery(fieldName + ':' + path);
                } else {
                    queryBuilder.append(fieldName);
                    queryBuilder.append(':');
                    queryBuilder.append(path);
                }
            }
        }
    }
    if (configuration.collapseJcrContentNodes()) {
        solrQuery.addFilterQuery("{!collapse field=" + configuration.getCollapsedPathField() + " min=" + configuration.getPathDepthField() + " hint=top_fc nullPolicy=expand}");
    }
    if (queryBuilder.length() == 0) {
        queryBuilder.append("*:*");
    }
    String escapedQuery = queryBuilder.toString();
    solrQuery.setQuery(escapedQuery);
    if (log.isDebugEnabled()) {
        log.debug("JCR query {} has been converted to Solr query {}", filter.getQueryStatement(), solrQuery.toString());
    }
    return solrQuery;
}
Also used : SolrQuery(org.apache.solr.client.solrj.SolrQuery) Filter(org.apache.jackrabbit.oak.spi.query.Filter) FullTextExpression(org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression) Collection(java.util.Collection) QueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex)

Aggregations

FullTextExpression (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextExpression)21 Filter (org.apache.jackrabbit.oak.spi.query.Filter)7 FullTextAnd (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextAnd)6 FullTextContains (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextContains)6 FullTextTerm (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextTerm)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 PrefixQuery (org.apache.lucene.search.PrefixQuery)5 Query (org.apache.lucene.search.Query)5 TermQuery (org.apache.lucene.search.TermQuery)5 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)5 WildcardQuery (org.apache.lucene.search.WildcardQuery)5 Test (org.junit.Test)5 PropertyRestriction (org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction)4 FullTextOr (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextOr)4 FullTextVisitor (org.apache.jackrabbit.oak.spi.query.fulltext.FullTextVisitor)4 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)3 PhraseQuery (org.apache.lucene.search.PhraseQuery)3