Bläddra i källkod

improve query handling

Shinsuke Sugaya 9 år sedan
förälder
incheckning
24d6c85d35

+ 9 - 1
src/main/java/org/codelibs/fess/es/client/FessEsClient.java

@@ -49,6 +49,7 @@ import org.codelibs.fess.entity.GeoInfo;
 import org.codelibs.fess.entity.PingResponse;
 import org.codelibs.fess.entity.PingResponse;
 import org.codelibs.fess.entity.QueryContext;
 import org.codelibs.fess.entity.QueryContext;
 import org.codelibs.fess.exception.FessSystemException;
 import org.codelibs.fess.exception.FessSystemException;
+import org.codelibs.fess.exception.InvalidQueryException;
 import org.codelibs.fess.exception.ResultOffsetExceededException;
 import org.codelibs.fess.exception.ResultOffsetExceededException;
 import org.codelibs.fess.exception.SearchQueryException;
 import org.codelibs.fess.exception.SearchQueryException;
 import org.codelibs.fess.helper.QueryHelper;
 import org.codelibs.fess.helper.QueryHelper;
@@ -121,6 +122,7 @@ import org.elasticsearch.action.search.ClearScrollResponse;
 import org.elasticsearch.action.search.MultiSearchRequest;
 import org.elasticsearch.action.search.MultiSearchRequest;
 import org.elasticsearch.action.search.MultiSearchRequestBuilder;
 import org.elasticsearch.action.search.MultiSearchRequestBuilder;
 import org.elasticsearch.action.search.MultiSearchResponse;
 import org.elasticsearch.action.search.MultiSearchResponse;
+import org.elasticsearch.action.search.SearchPhaseExecutionException;
 import org.elasticsearch.action.search.SearchRequest;
 import org.elasticsearch.action.search.SearchRequest;
 import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.action.search.SearchResponse;
@@ -162,6 +164,7 @@ import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuil
 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
 import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
 import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
 import org.elasticsearch.threadpool.ThreadPool;
 import org.elasticsearch.threadpool.ThreadPool;
+import org.lastaflute.core.message.UserMessages;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
@@ -535,7 +538,12 @@ public class FessEsClient implements Client {
                 }
                 }
             }
             }
 
 
-            searchResponse = searchRequestBuilder.execute().actionGet();
+            try {
+                searchResponse = searchRequestBuilder.execute().actionGet();
+            } catch (SearchPhaseExecutionException e) {
+                throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryParseError(UserMessages.GLOBAL_PROPERTY_KEY),
+                        "Invalid query: " + searchRequestBuilder, e);
+            }
         }
         }
         final long execTime = System.currentTimeMillis() - startTime;
         final long execTime = System.currentTimeMillis() - startTime;
 
 

+ 3 - 1
src/main/java/org/codelibs/fess/helper/QueryHelper.java

@@ -395,7 +395,9 @@ public class QueryHelper implements Serializable {
     protected QueryBuilder convertTermQuery(final QueryContext context, final TermQuery termQuery) {
     protected QueryBuilder convertTermQuery(final QueryContext context, final TermQuery termQuery) {
         final String field = termQuery.getTerm().field();
         final String field = termQuery.getTerm().field();
         final String text = termQuery.getTerm().text();
         final String text = termQuery.getTerm().text();
-        if (Constants.DEFAULT_FIELD.equals(field)) {
+        if (fessConfig.getQueryReplaceTermWithPrefixQueryAsBoolean() && text.length() > 1 && text.endsWith("*")) {
+            return convertPrefixQuery(context, new PrefixQuery(new Term(field, text.substring(0, text.length() - 1))));
+        } else if (Constants.DEFAULT_FIELD.equals(field)) {
             context.addFieldLog(field, text);
             context.addFieldLog(field, text);
             context.addHighlightedQuery(text);
             context.addHighlightedQuery(text);
             return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, text).boost(b));
             return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, text).boost(b));

+ 25 - 0
src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java

@@ -302,6 +302,9 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
     /** The key of the configuration. e.g. 1000 */
     /** The key of the configuration. e.g. 1000 */
     String QUERY_MAX_LENGTH = "query.max.length";
     String QUERY_MAX_LENGTH = "query.max.length";
 
 
+    /** The key of the configuration. e.g. true */
+    String QUERY_REPLACE_TERM_WITH_PREFIX_QUERY = "query.replace.term.with.prefix.query";
+
     /** The key of the configuration. e.g. 1.6 */
     /** The key of the configuration. e.g. 1.6 */
     String QUERY_BOOST_TITLE = "query.boost.title";
     String QUERY_BOOST_TITLE = "query.boost.title";
 
 
@@ -1392,6 +1395,20 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
      */
      */
     Integer getQueryMaxLengthAsInteger();
     Integer getQueryMaxLengthAsInteger();
 
 
+    /**
+     * Get the value for the key 'query.replace.term.with.prefix.query'. <br>
+     * The value is, e.g. true <br>
+     * @return The value of found property. (NotNull: if not found, exception but basically no way)
+     */
+    String getQueryReplaceTermWithPrefixQuery();
+
+    /**
+     * Is the property for the key 'query.replace.term.with.prefix.query' true? <br>
+     * The value is, e.g. true <br>
+     * @return The determination, true or false. (if not found, exception but basically no way)
+     */
+    boolean isQueryReplaceTermWithPrefixQuery();
+
     /**
     /**
      * Get the value for the key 'query.boost.title'. <br>
      * Get the value for the key 'query.boost.title'. <br>
      * The value is, e.g. 1.6 <br>
      * The value is, e.g. 1.6 <br>
@@ -2734,6 +2751,14 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
             return getAsInteger(FessConfig.QUERY_MAX_LENGTH);
             return getAsInteger(FessConfig.QUERY_MAX_LENGTH);
         }
         }
 
 
+        public String getQueryReplaceTermWithPrefixQuery() {
+            return get(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY);
+        }
+
+        public boolean isQueryReplaceTermWithPrefixQuery() {
+            return is(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY);
+        }
+
         public String getQueryBoostTitle() {
         public String getQueryBoostTitle() {
             return get(FessConfig.QUERY_BOOST_TITLE);
             return get(FessConfig.QUERY_BOOST_TITLE);
         }
         }

+ 7 - 0
src/main/java/org/codelibs/fess/mylasta/direction/FessProp.java

@@ -367,4 +367,11 @@ public interface FessProp {
     public default String[] getSuggestPopularWordExcludesAsArray() {
     public default String[] getSuggestPopularWordExcludesAsArray() {
         return StreamUtil.of(getSuggestPopularWordExcludes().split("\n")).filter(s -> StringUtil.isNotBlank(s)).toArray(n -> new String[n]);
         return StreamUtil.of(getSuggestPopularWordExcludes().split("\n")).filter(s -> StringUtil.isNotBlank(s)).toArray(n -> new String[n]);
     }
     }
+
+    String getQueryReplaceTermWithPrefixQuery();
+
+    public default boolean getQueryReplaceTermWithPrefixQueryAsBoolean() {
+        return Boolean.valueOf(getQueryReplaceTermWithPrefixQuery());
+    }
+
 }
 }

+ 1 - 0
src/main/resources/fess_config.properties

@@ -149,6 +149,7 @@ index.document.type=doc
 
 
 # query
 # query
 query.max.length=1000
 query.max.length=1000
+query.replace.term.with.prefix.query=true
 
 
 # boost
 # boost
 query.boost.title=1.6
 query.boost.title=1.6