Skip to content

Apache Lucene open-source search software

SOLR-13190 Surface Fuzzy term errors in Solr

Mike Drob93585ba1d33c
diff --git a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
index 5f17a103c39..a9d7d670d21 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
@@ -37,6 +37,7 @@ import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.FieldComparator;
+import org.apache.lucene.search.FuzzyTermsEnum;
import org.apache.lucene.search.LeafFieldComparator;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
@@ -1484,7 +1485,11 @@ public class QueryComponent extends SearchComponent
SolrIndexSearcher searcher = req.getSearcher();
- searcher.search(result, cmd);
+ try {
+ searcher.search(result, cmd);
+ } catch (FuzzyTermsEnum.FuzzyTermsException e) {
+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
+ }
rb.setResult(result);
ResultContext ctx = new BasicResultContext(rb);
diff --git a/solr/core/src/test/org/apache/solr/search/FuzzySearchTest.java b/solr/core/src/test/org/apache/solr/search/FuzzySearchTest.java
new file mode 100644
index 00000000000..f84cf10b422
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/search/FuzzySearchTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.search;
+
+import java.io.IOException;
+
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
+import org.apache.solr.client.solrj.impl.CloudSolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrInputDocument;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class FuzzySearchTest extends SolrCloudTestCase {
+ private final static String COLLECTION = "c1";
+ private CloudSolrClient client;
+
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(1).addConfig(COLLECTION, configset("cloud-minimal")).configure();
+ }
+
+ @Before
+ public void setupCollection() throws Exception {
+ client = cluster.getSolrClient();
+ client.setDefaultCollection(COLLECTION);
+
+ CollectionAdminRequest.createCollection(COLLECTION, 1, 1).process(client);
+ cluster.waitForActiveCollection(COLLECTION, 1, 1);
+ }
+
+ @Test
+ public void testTooComplex() throws IOException, SolrServerException {
+ SolrInputDocument doc = new SolrInputDocument();
+
+ doc.setField("id", "1");
+ doc.setField("text", "foo");
+ client.add(doc);
+ client.commit(); // Must have index files written, but the contents don't matter
+
+ SolrQuery query = new SolrQuery("text:headquarters\\(在日米海軍横須賀基地司令部庁舎\\/旧横須賀鎮守府会議所・横須賀海軍艦船部\\)~");
+
+ BaseHttpSolrClient.RemoteSolrException e = expectThrows(BaseHttpSolrClient.RemoteSolrException.class, () -> client.query(query));
+ assertTrue("Should be client error, not server error", e.code() >= 400 && e.code() < 500);
+ }
+}