Apache Lucene open-source search software
SOLR-1316: update SuggesterTest
Yonik Seeley5204bd9c42fd
diff --git a/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java b/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.javaindex 5fce8c45251..32c00b93dee 100644--- a/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java+++ b/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java@@ -1,130 +1,72 @@-package org.apache.solr.spelling.suggest;+/**+ * 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.+ */-import java.io.StringWriter;-import java.util.HashMap;-import java.util.List;-import java.util.Map;-import java.util.Random;+package org.apache.solr.spelling.suggest;import org.apache.lucene.util.RamUsageEstimator;-import org.apache.solr.common.params.CommonParams;-import org.apache.solr.common.params.ModifiableSolrParams;+import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.common.params.SpellingParams;-import org.apache.solr.core.SolrCore;-import org.apache.solr.request.LocalSolrQueryRequest;-import org.apache.solr.request.SolrQueryRequest;-import org.apache.solr.request.SolrRequestHandler;-import org.apache.solr.response.QueryResponseWriter;-import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.spelling.suggest.Lookup.LookupResult;import org.apache.solr.spelling.suggest.jaspell.JaspellLookup;import org.apache.solr.spelling.suggest.tst.TSTLookup;-import org.apache.solr.util.AbstractSolrTestCase;import org.apache.solr.util.RefCounted;import org.apache.solr.util.TermFreqIterator;import org.apache.solr.util.TestHarness;+import org.junit.BeforeClass;+import org.junit.Test;+import static org.junit.Assert.*;-public class SuggesterTest extends AbstractSolrTestCase {- SolrRequestHandler handler;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import java.util.Random;- @Override- public String getSchemaFile() {- return "schema-spellchecker.xml";+public class SuggesterTest extends SolrTestCaseJ4 {+ @BeforeClass+ public static void beforeClass() throws Exception {+ initCore("solrconfig-spellchecker.xml","schema-spellchecker.xml");}- @Override- public String getSolrConfigFile() {- return "solrconfig-spellchecker.xml";+ public static void addDocs() throws Exception {+ assertU(adoc("id", "1",+ "text", "acceptable accidentally accommodate acquire"+ ));+ assertU(adoc("id", "2",+ "text", "believe bellwether accommodate acquire"+ ));+ assertU(adoc("id", "3",+ "text", "cemetery changeable conscientious consensus acquire bellwether"+ ));}- @Override- public void setUp() throws Exception {- super.setUp();- // empty- h.validateUpdate("<delete><query>*:*</query></delete>");- // populate- h.validateAddDoc(- "id", "1",- "text", "acceptable accidentally accommodate acquire"- );- h.validateAddDoc(- "id", "2",- "text", "believe bellwether accommodate acquire"- );- h.validateAddDoc(- "id", "3",- "text", "cemetery changeable conscientious consensus acquire bellwether"- );- h.validateUpdate("<commit/>");- handler = h.getCore().getRequestHandler("/suggest");- // build- ModifiableSolrParams params = new ModifiableSolrParams();- params.set(SpellingParams.SPELLCHECK_BUILD, true);- LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), params);- SolrQueryResponse rsp = new SolrQueryResponse();- handler.handleRequest(req, rsp);- }-- private String assertXPath(SolrCore core, SolrQueryRequest req, SolrQueryResponse rsp, String... tests) throws Exception {- StringWriter sw = new StringWriter(32000);- QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);- responseWriter.write(sw,req,rsp);- req.close();- System.out.println(sw.toString());- return h.validateXPath(sw.toString(), tests);- }-+ @Testpublic void testSuggestions() throws Exception {- ModifiableSolrParams params = new ModifiableSolrParams();- params.set(CommonParams.Q, "ac");- params.set(SpellingParams.SPELLCHECK_COUNT, 2);- params.set(SpellingParams.SPELLCHECK_ONLY_MORE_POPULAR, true);- LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), params);- SolrQueryResponse rsp = new SolrQueryResponse();- handler.handleRequest(req, rsp);- String res = assertXPath(h.getCore(), req, rsp,- "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/int[@name='numFound'][.='2']",- "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/arr[@name='suggestion']/str[1][.='acquire']",- "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/arr[@name='suggestion']/str[2][.='accommodate']"- );- assertNull(res, res);- }-- public void testReload() throws Exception {- String coreName = h.getCore().getName();- RefCounted<SolrIndexSearcher> searcher = h.getCore().getSearcher();- SolrIndexSearcher indexSearcher = searcher.get();- log.info("Core " + coreName + ", NumDocs before reload: " + indexSearcher.getIndexReader().numDocs());- log.info("Directory: " + indexSearcher.getIndexDir());- searcher.decref();- h.close();- solrConfig = TestHarness.createConfig(getSolrConfigFile());- h = new TestHarness( dataDir.getAbsolutePath(),- solrConfig,- getSchemaFile());- searcher = h.getCore().getSearcher();- indexSearcher = searcher.get();- log.info("Core " + coreName + ", NumDocs now: " + indexSearcher.getIndexReader().numDocs());- log.info("Directory: " + indexSearcher.getIndexDir());- searcher.decref();- // rebuilds on commit- h.validateUpdate("<commit/>");- handler = h.getCore().getRequestHandler("/suggest");- ModifiableSolrParams params = new ModifiableSolrParams();- params.set(CommonParams.Q, "ac");- params.set(SpellingParams.SPELLCHECK_COUNT, 2);- params.set(SpellingParams.SPELLCHECK_ONLY_MORE_POPULAR, true);- LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), params);- SolrQueryResponse rsp = new SolrQueryResponse();- handler.handleRequest(req, rsp);- String res = assertXPath(h.getCore(), req, rsp,- "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/int[@name='numFound'][.='2']",- "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/arr[@name='suggestion']/str[1][.='acquire']",- "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/arr[@name='suggestion']/str[2][.='accommodate']"- );- assertNull(res, res);+ addDocs();++ assertU(commit()); // configured to do a rebuild on commit++ assertQ(req("qt","/suggest", "q","ac", SpellingParams.SPELLCHECK_COUNT, "2", SpellingParams.SPELLCHECK_ONLY_MORE_POPULAR, "true"),+ "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/int[@name='numFound'][.='2']",+ "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/arr[@name='suggestion']/str[1][.='acquire']",+ "//lst[@name='spellcheck']/lst[@name='suggestions']/lst[@name='ac']/arr[@name='suggestion']/str[2][.='accommodate']"+ );}+private TermFreqIterator getTFIT() {final int count = 100000;@@ -133,23 +75,19 @@ public class SuggesterTest extends AbstractSolrTestCase {Random r1 = new Random(1234567890L);int pos;- @Overridepublic float freq() {return r1.nextInt(4);}- @Overridepublic boolean hasNext() {return pos < count;}- @Overridepublic String next() {pos++;return Long.toString(r.nextLong());}- @Overridepublic void remove() {throw new UnsupportedOperationException();}@@ -200,7 +138,8 @@ public class SuggesterTest extends AbstractSolrTestCase {long buildTime;long lookupTime;}-++ @Testpublic void testBenchmark() throws Exception {// this benchmark is very time consumingboolean doTest = false;