From 3c0ff1de91193e1ffbb53dcb16d6d2a83d2e6bf5 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Wed, 8 Apr 2015 15:28:13 +0000 Subject: [PATCH] SOLR-1387: Move contains() method to SimpleFacets git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1672106 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/util/StringHelper.java | 31 +------------------ .../apache/lucene/util/TestStringHelper.java | 30 ------------------ .../demo/facet/TestSimpleFacetsExample.java | 5 +-- .../apache/solr/request/DocValuesFacets.java | 21 ++++++------- .../PerSegmentSingleValuedFaceting.java | 23 ++++++++------ .../org/apache/solr/request/SimpleFacets.java | 31 +++++++++++++------ .../apache/solr/request/SimpleFacetsTest.java | 20 ++++++++++++ 7 files changed, 69 insertions(+), 92 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java b/lucene/core/src/java/org/apache/lucene/util/StringHelper.java index 584129a0b8b..f38321fa63c 100644 --- a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java +++ b/lucene/core/src/java/org/apache/lucene/util/StringHelper.java @@ -22,7 +22,6 @@ import java.math.BigInteger; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; -import java.util.Locale; import java.util.Properties; /** @@ -134,35 +133,7 @@ public abstract class StringHelper { public static boolean endsWith(BytesRef ref, BytesRef suffix) { return sliceEquals(ref, suffix, ref.length - suffix.length); } - - /** - * Returns true iff the ref contains the given slice. Otherwise - * false. - * - * @param ref - * the {@link BytesRef} to test - * @param slice - * the slice to look for - * @param ignoreCase - * whether the comparison should be case-insensitive - * @return Returns true iff the ref contains the given slice. - * Otherwise false. - */ - public static boolean contains(BytesRef ref, BytesRef slice, boolean ignoreCase) { - if (ignoreCase) { - String s1 = ref.utf8ToString(); - String s2 = slice.utf8ToString(); - return s1.toLowerCase(Locale.ENGLISH).contains(s2.toLowerCase(Locale.ENGLISH)); - } else { - for (int pos = 0; pos <= ref.length - slice.length; ++pos) { - if (sliceEquals(ref, slice, pos)) { - return true; - } - } - } - return false; - } - + private static boolean sliceEquals(BytesRef sliceToTest, BytesRef other, int pos) { if (pos < 0 || sliceToTest.length - pos < other.length) { return false; diff --git a/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java b/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java index 033c3250d58..20bf2a494fd 100644 --- a/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java +++ b/lucene/core/src/test/org/apache/lucene/util/TestStringHelper.java @@ -56,36 +56,6 @@ public class TestStringHelper extends LuceneTestCase { assertTrue(StringHelper.endsWith(ref, slice)); } - public void testContainsAtStart() { - BytesRef ref = new BytesRef("foobar"); - BytesRef slice = new BytesRef("foo"); - assertTrue(StringHelper.contains(ref, slice, false)); - } - - public void testContains() { - BytesRef ref = new BytesRef("foobar"); - BytesRef slice = new BytesRef("ooba"); - assertTrue(StringHelper.contains(ref, slice, false)); - } - - public void testContainsAtEnd() { - BytesRef ref = new BytesRef("foobar"); - BytesRef slice = new BytesRef("bar"); - assertTrue(StringHelper.contains(ref, slice, false)); - } - - public void testContainsWhole() { - BytesRef ref = new BytesRef("foobar"); - BytesRef slice = new BytesRef("foobar"); - assertTrue(StringHelper.contains(ref, slice, false)); - } - - public void testContainsIgnoreCase() { - BytesRef ref = new BytesRef("FooBar"); - BytesRef slice = new BytesRef("bar"); - assertTrue(StringHelper.contains(ref, slice, true)); - } - public void testMurmurHash3() throws Exception { // Hashes computed using murmur3_32 from https://code.google.com/p/pyfasthash assertEquals(0xf6a5c420, StringHelper.murmurhash3_x86_32(new BytesRef("foo"), 0)); diff --git a/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java index cddd42c9e96..9b6087b57e5 100644 --- a/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java +++ b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java @@ -17,12 +17,12 @@ package org.apache.lucene.demo.facet; * limitations under the License. */ -import java.util.List; - import org.apache.lucene.facet.FacetResult; import org.apache.lucene.util.LuceneTestCase; import org.junit.Test; +import java.util.List; + public class TestSimpleFacetsExample extends LuceneTestCase { @Test @@ -54,4 +54,5 @@ public class TestSimpleFacetsExample extends LuceneTestCase { assertEquals("dim=Publish Date path=[] value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", result.get(0).toString()); assertEquals("dim=Author path=[] value=2 childCount=2\n Bob (1)\n Lisa (1)\n", result.get(1).toString()); } + } diff --git a/solr/core/src/java/org/apache/solr/request/DocValuesFacets.java b/solr/core/src/java/org/apache/solr/request/DocValuesFacets.java index cfd3e05636e..1efd8f33ba5 100644 --- a/solr/core/src/java/org/apache/solr/request/DocValuesFacets.java +++ b/solr/core/src/java/org/apache/solr/request/DocValuesFacets.java @@ -17,11 +17,8 @@ package org.apache.solr.request; * limitations under the License. */ -import java.io.IOException; -import java.util.List; - -import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.DocValues; +import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.MultiDocValues.MultiSortedDocValues; import org.apache.lucene.index.MultiDocValues.MultiSortedSetDocValues; import org.apache.lucene.index.MultiDocValues.OrdinalMap; @@ -34,7 +31,6 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefBuilder; import org.apache.lucene.util.CharsRefBuilder; import org.apache.lucene.util.LongValues; -import org.apache.lucene.util.StringHelper; import org.apache.lucene.util.UnicodeUtil; import org.apache.solr.common.params.FacetParams; import org.apache.solr.common.util.NamedList; @@ -44,6 +40,9 @@ import org.apache.solr.search.DocSet; import org.apache.solr.search.SolrIndexSearcher; import org.apache.solr.util.LongPriorityQueue; +import java.io.IOException; +import java.util.List; + /** * Computes term facets for docvalues field (single or multivalued). *

@@ -99,8 +98,6 @@ public class DocValuesFacets { prefixRef.copyChars(prefix); } - final BytesRef containsBR = contains != null ? new BytesRef(contains) : null; - int startTermIndex, endTermIndex; if (prefix!=null) { startTermIndex = (int) si.lookupTerm(prefixRef.get()); @@ -173,9 +170,9 @@ public class DocValuesFacets { int min=mincount-1; // the smallest value in the top 'N' values for (int i=(startTermIndex==-1)?1:0; itrue if a String contains the given substring. Otherwise + * false. + * + * @param ref + * the {@link String} to test + * @param substring + * the substring to look for + * @param ignoreCase + * whether the comparison should be case-insensitive + * @return Returns true iff the String contains the given substring. + * Otherwise false. + */ + public static boolean contains(String ref, String substring, boolean ignoreCase) { + if (ignoreCase) + return StringUtils.containsIgnoreCase(ref, substring); + return StringUtils.contains(ref, substring); + } + protected void parseParams(String type, String param) throws SyntaxError, IOException { localParams = QueryParsing.getLocalParams(param, req.getParams()); @@ -494,7 +514,6 @@ public class SimpleFacets { } BytesRef prefixBytesRef = prefix != null ? new BytesRef(prefix) : null; - BytesRef containsRef = contains != null ? new BytesRef(contains) : null; final TermGroupFacetCollector collector = TermGroupFacetCollector.createTermGroupFacetCollector(groupField, field, multiToken, prefixBytesRef, 128); SchemaField sf = searcher.getSchema().getFieldOrNull(groupField); @@ -526,7 +545,7 @@ public class SimpleFacets { = result.getFacetEntries(offset, limit < 0 ? Integer.MAX_VALUE : limit); for (TermGroupFacetCollector.FacetEntry facetEntry : scopedEntries) { //:TODO:can we do contains earlier than this to make it more efficient? - if (containsRef != null && !StringHelper.contains(facetEntry.getValue(), containsRef, ignoreCase)) { + if (contains != null && !contains(facetEntry.getValue().utf8ToString(), contains, ignoreCase)) { continue; } facetFieldType.indexedToReadable(facetEntry.getValue(), charsRef); @@ -730,12 +749,6 @@ public class SimpleFacets { String indexedPrefix = ft.toInternal(prefix); prefixTermBytes = new BytesRef(indexedPrefix); } - - BytesRef containsTermBytes = null; - if (contains != null) { - String indexedContains = ft.toInternal(contains); - containsTermBytes = new BytesRef(indexedContains); - } Fields fields = r.fields(); Terms terms = fields==null ? null : fields.terms(field); @@ -769,7 +782,7 @@ public class SimpleFacets { if (prefixTermBytes != null && !StringHelper.startsWith(term, prefixTermBytes)) break; - if (containsTermBytes == null || StringHelper.contains(term, containsTermBytes, ignoreCase)) { + if (contains == null || contains(term.utf8ToString(), contains, ignoreCase)) { int df = termsEnum.docFreq(); // If we are sorting, we can use df>min (rather than >=) since we diff --git a/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java b/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java index 5b60a26eb7c..785eee8d10d 100644 --- a/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java +++ b/solr/core/src/test/org/apache/solr/request/SimpleFacetsTest.java @@ -2258,4 +2258,24 @@ public class SimpleFacetsTest extends SolrTestCaseJ4 { 400); } + public void testContainsAtStart() { + assertTrue(SimpleFacets.contains("foobar", "foo", false)); + } + + public void testContains() { + assertTrue(SimpleFacets.contains("foobar", "ooba", false)); + } + + public void testContainsAtEnd() { + assertTrue(SimpleFacets.contains("foobar", "bar", false)); + } + + public void testContainsWhole() { + assertTrue(SimpleFacets.contains("foobar", "foobar", false)); + } + + public void testContainsIgnoreCase() { + assertTrue(SimpleFacets.contains("FooBar", "bar", true)); + } + }