SOLR-13190 Surface Fuzzy term errors in Solr

Use newly added exceptions in Lucene for too complex fuzzy terms to
provide better error reporting for Solr queries.
This commit is contained in:
Mike Drob 2019-12-17 15:18:55 -06:00
parent a4c884a22f
commit 93585ba1d3
2 changed files with 73 additions and 1 deletions

View File

@ -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);

View File

@ -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;
@LuceneTestCase.Slow
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);
}
}