Commit 8b6473b5 authored by Jeroen Hoffman's avatar Jeroen Hoffman

CMS-10098 parameterize wildcard postfix "enabled" and "length" settings

(taken manually from bugfix/CMS-10098 branch into feautir/CMS-10098)
parent 9d17a155
/*
* Copyright 2012-2013 Hippo B.V. (http://www.onehippo.com)
* Copyright 2012-2016 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -24,6 +24,7 @@ import java.util.List;
import javax.jcr.Session;
import org.hippoecm.repository.util.DateTools;
import org.onehippo.cms7.services.search.jcr.service.HippoJcrSearchService;
import org.onehippo.cms7.services.search.query.constraint.DateConstraint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -36,9 +37,9 @@ public class Filter {
private boolean negated = false;
private final static boolean DEFAULT_FULLTEXT_WILDCARD_POSTFIXED = true;
private final boolean fulltextWildcardPostfixEnabled;
private boolean fulltextWildcardPostfixed = DEFAULT_FULLTEXT_WILDCARD_POSTFIXED;
private final int fulltextWildcardPostfixMinLength;
private final Session session;
......@@ -54,7 +55,13 @@ public class Filter {
private ChildFilterType firstAddedType;
public Filter(final Session session) {
this(session, HippoJcrSearchService.DEFAULT_WILDCARD_POSTFIX_ENABLED, HippoJcrSearchService.DEFAULT_WILDCARD_POSTFIX_MINLENGTH);
}
public Filter(final Session session, final boolean wildcardPostfixEnabled, final int wildcardPostfixMinLength) {
this.session = session;
this.fulltextWildcardPostfixEnabled = wildcardPostfixEnabled;
this.fulltextWildcardPostfixMinLength = wildcardPostfixMinLength;
}
private enum ChildFilterType {
......@@ -87,23 +94,23 @@ public class Filter {
} else {
String parsedText = FullTextSearchParser.fullTextParseCmsSimpleSearchMode(fullTextSearch, false);
if (fulltextWildcardPostfixed) {
String parsedTextWildCardPostFixed = FullTextSearchParser.fullTextParseCmsSimpleSearchMode(fullTextSearch, true);
if (parsedTextWildCardPostFixed.length() > 0) {
if (fulltextWildcardPostfixEnabled) {
final String parsedTextWildcarded = FullTextSearchParser.fullTextParseCmsSimpleSearchMode(fullTextSearch, true, fulltextWildcardPostfixMinLength);
if (parsedTextWildcarded.length() > 0) {
if (parsedText.length() > 0) {
whereClauseBuilder.append("(");
addContainsToBuilder(whereClauseBuilder, scope, parsedText);
whereClauseBuilder.append(" or ");
addContainsToBuilder(whereClauseBuilder, scope, parsedTextWildCardPostFixed);
addContainsToBuilder(whereClauseBuilder, scope, parsedTextWildcarded);
whereClauseBuilder.append(")");
} else {
addContainsToBuilder(whereClauseBuilder, scope, parsedTextWildCardPostFixed);
addContainsToBuilder(whereClauseBuilder, scope, parsedTextWildcarded);
}
} else if (whereClauseBuilder.length() > 0) {
addContainsToBuilder(whereClauseBuilder, scope, whereClauseBuilder.toString());
} else if (parsedText.length() > 0) {
addContainsToBuilder(whereClauseBuilder, scope, parsedText);
}
} else if (whereClauseBuilder.length() > 0) {
addContainsToBuilder(whereClauseBuilder, scope, whereClauseBuilder.toString());
} else if (parsedText.length() > 0) {
addContainsToBuilder(whereClauseBuilder, scope, parsedText);
}
log.info("Translated fullTextSearch '{}' to where clause '{}'.", fullTextSearch, whereClauseBuilder.toString());
}
......
/*
* Copyright 2012-2013 Hippo B.V. (http://www.onehippo.com)
* Copyright 2012-2016 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,12 +18,12 @@ package org.onehippo.cms7.services.search.jcr.query;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
import org.onehippo.cms7.services.search.jcr.service.HippoJcrSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Input utilities for user searches.
*
*/
public final class FullTextSearchParser {
......@@ -35,8 +35,6 @@ public final class FullTextSearchParser {
private final static String ignoredChars = DEFAULT_IGNORED_CHARS;
private final static int minimalLength = 3;
private static final String WHITESPACE_PATTERN = "\\s+";
private FullTextSearchParser() {
......@@ -63,6 +61,11 @@ public final class FullTextSearchParser {
}
public static String fullTextParseCmsSimpleSearchMode(String value, final boolean wildcardPostfix) {
return fullTextParseCmsSimpleSearchMode(value, wildcardPostfix, HippoJcrSearchService.DEFAULT_WILDCARD_POSTFIX_MINLENGTH);
}
public static String fullTextParseCmsSimpleSearchMode(String value, final boolean wildcardPostfix, final int minimalLength) {
value = foldToASCIIReplacer(value.trim());
StringBuilder whereClauseBuilder = new StringBuilder();
boolean isOperatorToken;
......@@ -108,8 +111,9 @@ public final class FullTextSearchParser {
isOperatorToken = false;
}
if (wildcardPostfix && tb.length() < getMinimalLength() && !isOperatorToken) {
// for wildcard postfixing we demand the term to be at least as long as #getMinimalLength()
if (wildcardPostfix && tb.length() < minimalLength && !isOperatorToken) {
// for wildcard postfixing we demand the term to be at least as long as minimal length
continue;
}
......@@ -144,15 +148,20 @@ public final class FullTextSearchParser {
// 1: WildcardSearch is set to true
// 2: The term length is at least equal to minimal length: This is to avoid expensive kind of a* searches
// 3: The term is not an operator token like AND or OR
if (wildcardPostfix && tb.length() >= getMinimalLength() && !isOperatorToken) {
if (wildcardPostfix && tb.length() >= minimalLength && !isOperatorToken) {
whereClauseBuilder.append('*');
}
}
return whereClauseBuilder.toString();
}
/**
* Return the default minimal length
*
* @deprecated please use HippoJcrSearchService.DEFAULT_WILDCARD_POSTFIX_MINLENGTH
*/
public static int getMinimalLength() {
return minimalLength;
return HippoJcrSearchService.DEFAULT_WILDCARD_POSTFIX_MINLENGTH;
}
......
/*
* Copyright 2012-2015 Hippo B.V. (http://www.onehippo.com)
* Copyright 2012-2016 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -41,13 +41,18 @@ public class JcrQueryBuilder {
private List<String> excludeScopes = new ArrayList<String>();
private List<String> orderByList = new ArrayList<String>();
private final boolean wildcardPostfixEnabled;
private final int wildcardPostfixMinLength;
private Filter filter;
private String nodeType;
private List<String> selected = new ArrayList<String>();
public JcrQueryBuilder(Session session) {
public JcrQueryBuilder(final Session session, final boolean wildcardPostfixEnabled, final int wildcardPostfixMinLength) {
this.session = session;
this.wildcardPostfixEnabled = wildcardPostfixEnabled;
this.wildcardPostfixMinLength = wildcardPostfixMinLength;
}
public String getNodeType() {
......@@ -226,4 +231,11 @@ public class JcrQueryBuilder {
this.limit = limit;
}
public boolean isWildcardPostfixEnabled() {
return wildcardPostfixEnabled;
}
public int getWildcardPostfixMinLength() {
return wildcardPostfixMinLength;
}
}
/*
* Copyright 2012-2015 Hippo B.V. (http://www.onehippo.com)
* Copyright 2012-2016 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -197,7 +197,8 @@ public class JcrQueryVisitor implements QueryVisitor {
}
private Filter getFilter(final TextConstraint constraint) throws JcrQueryException {
Filter filter = new Filter(session);
final Filter filter = new Filter(session, builder.isWildcardPostfixEnabled(), builder.getWildcardPostfixMinLength());
String property = constraint.getProperty();
if (property == null) {
property = ".";
......
/*
* Copyright 2012-2013 Hippo B.V. (http://www.onehippo.com)
* Copyright 2012-2016 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -40,10 +40,31 @@ import org.slf4j.LoggerFactory;
public class HippoJcrSearchService implements SearchService, QueryPersistService {
static final Logger log = LoggerFactory.getLogger(HippoJcrSearchService.class);
private static final Logger log = LoggerFactory.getLogger(HippoJcrSearchService.class);
public static final boolean DEFAULT_WILDCARD_POSTFIX_ENABLED = true;
public static final int DEFAULT_WILDCARD_POSTFIX_MINLENGTH = 3;
private boolean wildcardPostfixEnabled = DEFAULT_WILDCARD_POSTFIX_ENABLED;
private int wildcardPostfixMinLength = DEFAULT_WILDCARD_POSTFIX_MINLENGTH;
private Session session;
/**
* Default constructor
*/
public HippoJcrSearchService() {
}
/**
* Parameterized constructor
*/
public HippoJcrSearchService(final Session session, final boolean wildcardPostfixEnabled, final int wildcardPostfixMinLength) {
this.session = session;
this.wildcardPostfixEnabled = wildcardPostfixEnabled;
this.wildcardPostfixMinLength = wildcardPostfixMinLength;
}
public Session getSession() {
return session;
}
......@@ -68,7 +89,7 @@ public class HippoJcrSearchService implements SearchService, QueryPersistService
throw new IllegalArgumentException("Search service only accepts queries created by itself");
}
QueryNode query = (QueryNode) searchQuery;
JcrQueryBuilder queryBuilder = new JcrQueryBuilder(session);
final JcrQueryBuilder queryBuilder = new JcrQueryBuilder(session, this.isWildcardPostfixEnabled(), this.getWildcardPostfixMinLength());
query.accept(new JcrQueryVisitor(queryBuilder, session));
final String queryString = queryBuilder.getQueryString();
try {
......@@ -122,4 +143,13 @@ public class HippoJcrSearchService implements SearchService, QueryPersistService
throw new SearchServiceException(re);
}
}
public boolean isWildcardPostfixEnabled() {
return wildcardPostfixEnabled;
}
public int getWildcardPostfixMinLength() {
return wildcardPostfixMinLength;
}
}
/*
* Copyright 2015 Hippo B.V. (http://www.onehippo.com)
* Copyright 2015-2016 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -20,10 +20,12 @@ import javax.jcr.Session;
import org.onehippo.cms7.services.search.commons.query.QueryImpl;
import org.onehippo.cms7.services.search.jcr.query.JcrQueryBuilder;
import org.onehippo.cms7.services.search.jcr.query.JcrQueryVisitor;
import org.onehippo.cms7.services.search.jcr.service.HippoJcrSearchService;
public class TestUtils {
public static String getQueryAsString(final QueryImpl query, final Session session) {
final JcrQueryBuilder builder = new JcrQueryBuilder(session);
final JcrQueryBuilder builder = new JcrQueryBuilder(session,
HippoJcrSearchService.DEFAULT_WILDCARD_POSTFIX_ENABLED, HippoJcrSearchService.DEFAULT_WILDCARD_POSTFIX_MINLENGTH);
JcrQueryVisitor visitor = new JcrQueryVisitor(builder, session);
query.accept(visitor);
return builder.getQueryString();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment