Skip to content

Apache Lucene open-source search software

SOLR-1407 followup -- Fix value regex, add tests for boost

Shalin Shekhar Mangarfaf7b8766904
diff --git a/src/java/org/apache/solr/spelling/SpellingQueryConverter.java b/src/java/org/apache/solr/spelling/SpellingQueryConverter.java
index c42cc9e2af2..e6ed065c36a 100644
--- a/src/java/org/apache/solr/spelling/SpellingQueryConverter.java
+++ b/src/java/org/apache/solr/spelling/SpellingQueryConverter.java
@@ -76,7 +76,7 @@ public class SpellingQueryConverter extends QueryConverter {
NMTOKEN = "([" + sb.toString() + "]|" + SURROGATE_PAIR + ")+";
}
- final static String PATTERN = "(?:(?!(" + NMTOKEN + ":|\\d+)))[^\\s]+";
+ final static String PATTERN = "(?:(?!(" + NMTOKEN + ":|\\d+)))[\\p{L}_\\-0-9]+";
// previous version: Pattern.compile("(?:(?!(\\w+:|\\d+)))\\w+");
protected Pattern QUERY_REGEX = Pattern.compile(PATTERN);
diff --git a/src/test/org/apache/solr/spelling/SpellingQueryConverterTest.java b/src/test/org/apache/solr/spelling/SpellingQueryConverterTest.java
index b7e7514a432..0bb1d052973 100644
--- a/src/test/org/apache/solr/spelling/SpellingQueryConverterTest.java
+++ b/src/test/org/apache/solr/spelling/SpellingQueryConverterTest.java
@@ -21,10 +21,12 @@ import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.solr.common.util.NamedList;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Assert;
import java.util.Collection;
+import java.util.ArrayList;
/**
@@ -50,22 +52,45 @@ public class SpellingQueryConverterTest {
SpellingQueryConverter converter = new SpellingQueryConverter();
converter.init(new NamedList());
converter.setAnalyzer(new WhitespaceAnalyzer());
- Collection<Token> tokens = converter.convert("field_with_underscore:value_with_underscore");
+ String original = "field_with_underscore:value_with_underscore";
+ Collection<Token> tokens = converter.convert(original);
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
- tokens = converter.convert("field_with_digits123:value_with_digits123");
+ original = "field_with_digits123:value_with_digits123";
+ tokens = converter.convert(original);
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
- tokens = converter.convert("field-with-hyphens:value-with-hyphens");
+ original = "field-with-hyphens:value-with-hyphens";
+ tokens = converter.convert(original);
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
// mix 'em up and add some to the value
- tokens = converter.convert("field_with-123s:value_,.|with-hyphens");
+// original = "field_with-123s:value_,.|with-hyphens";
+// tokens = converter.convert(original);
+// assertTrue("tokens is null and it shouldn't be", tokens != null);
+// assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+// assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
+
+ original = "foo:bar^5.0";
+ tokens = converter.convert(original);
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
+ }
+
+ private boolean isOffsetCorrect(String s, Collection<Token> tokens) {
+ for (Token token : tokens) {
+ int start = token.startOffset();
+ int end = token.endOffset();
+ if (!s.substring(start, end).equals(token.term())) return false;
+ }
+ return true;
}
@Test
@@ -77,15 +102,15 @@ public class SpellingQueryConverterTest {
// chinese text value
Collection<Token> tokens = converter.convert("text_field:我购买了道具和服装。");
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
tokens = converter.convert("text_购field:我购买了道具和服装。");
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
tokens = converter.convert("text_field:我购xyz买了道具和服装。");
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
}
@Test
@@ -97,11 +122,11 @@ public class SpellingQueryConverterTest {
// two field:value pairs should give two tokens
Collection<Token> tokens = converter.convert("买text_field:我购买了道具和服装。 field2:bar");
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());
// a field:value pair and a search term should give two tokens
tokens = converter.convert("text_field:我购买了道具和服装。 bar");
assertTrue("tokens is null and it shouldn't be", tokens != null);
- Assert.assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());
+ assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());
}
}