From 5d042d3a49dfcf654b8bf8a96521d5404bfd3a7b Mon Sep 17 00:00:00 2001 From: Mikhail Khludnev Date: Fri, 30 Dec 2016 00:42:51 +0300 Subject: [PATCH] SOLR-9900: fix false positives on range queries with ReversedWildcardFilterFactory --- solr/CHANGES.txt | 2 ++ .../apache/solr/parser/SolrQueryParserBase.java | 14 ++++++++++++++ .../TestReversedWildcardFilterFactory.java | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index b509e236001..7f83de01cf3 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -298,6 +298,8 @@ Bug Fixes * SOLR-9901: Implement move in HdfsDirectoryFactory. (Mark Miller) +* SOLR-9900: fix false positives on range queries with ReversedWildcardFilterFactory (Yonik Seeley via Mikhail Khludnev) + Other Changes ---------------------- diff --git a/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java b/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java index 168bd494fdf..f54e9e98ae4 100644 --- a/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java +++ b/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.reverse.ReverseStringFilter; import org.apache.lucene.analysis.util.TokenFilterFactory; import org.apache.lucene.index.Term; import org.apache.lucene.search.AutomatonQuery; @@ -894,6 +895,19 @@ public abstract class SolrQueryParserBase extends QueryBuilder { protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws SyntaxError { checkNullField(field); SchemaField sf = schema.getField(field); + + if (part1 == null) { + ReversedWildcardFilterFactory factory = getReversedWildcardFilterFactory(sf.getType()); + if (factory != null) { + // There will be reversed tokens starting with u0001 that we want to exclude, so + // lets start at u0002 inclusive instead. + char[] buf = new char[1]; + buf[0] = ReverseStringFilter.START_OF_HEADING_MARKER + 1; + part1 = new String(buf); + startInclusive = true; + } + } + return sf.getType().getRangeQuery(parser, sf, part1, part2, startInclusive, endInclusive); } diff --git a/solr/core/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java b/solr/core/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java index 3ccc352d46d..f7a49ace265 100644 --- a/solr/core/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java +++ b/solr/core/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.java @@ -182,6 +182,18 @@ public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 { assertQ("false positive", req("+id:1 +one:*zemog*"), "//result[@numFound=0]"); + + assertQ("no reverse, no false positive", + req("q", "+id:1 +three:[* TO a]", + "debugQuery", "true"), + "//result[@numFound=0]"); + { + String reverseField = random().nextBoolean() ? "one":"two"; + assertQ("false positive", + req("q", "+id:1 +"+reverseField+":[* TO a]", + "debugQuery", "true"), + "//result[@numFound=0]"); + } assertQ("false positive", req("+id:1 +two:*zemog*"), "//result[@numFound=0]");