Hi Andy,
here's a short example in both Java and Python that do the same. The Python
example raises the error as mentioned by Daniel. I've also added a patch to
extend the current test cases to include tests for setAllowLeadingWildcard().
-Christoph
import org.apache.lucene.search.Query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.util.Version;
import org.apache.lucene.queryParser.ParseException;
public class LeadingWildcardExample {
public static void main(String[] args) throws ParseException {
String queryString = "*foo* bar";
String[] fields = {"one", "two"};
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, analyzer);
queryParser.setAllowLeadingWildcard(true);
Query query = queryParser.parse(queryString);
System.out.println(query.toString());
}
}
#!/usr/bin/python
from lucene import *
initVM()
queryString = "*foo* bar"
fields = ["one", "two"]
analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
queryParser = MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, analyzer)
queryParser.setAllowLeadingWildcard(True)
#query = queryParser.parse(queryString)
query = queryParser.parse(Version.LUCENE_CURRENT, queryString, fields,
[BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD],
analyzer)
print str(query)
Index: test/test_PythonQueryParser.py
===================================================================
--- test/test_PythonQueryParser.py (Revision 986634)
+++ test/test_PythonQueryParser.py (Arbeitskopie)
@@ -41,7 +41,14 @@
q = qp.parse("foo bar")
self.assertEquals(str(q), "all:foo all:bar all:extra_clause")
+ def testLeadingWildcard(self):
+ qp = QueryParser(Version.LUCENE_CURRENT, 'all',
+ StandardAnalyzer(Version.LUCENE_CURRENT))
+ qp.setAllowLeadingWildcard(True)
+ q = qp.parse("*foo* bar")
+ self.assertEquals(str(q), "all:*foo* all:bar")
+
class PythonMultiFieldQueryParserTestCase(TestCase):
def testOverrideBooleanQuery(self):
@@ -57,7 +64,16 @@
StandardAnalyzer(Version.LUCENE_CURRENT))
self.assertEquals(str(q), "(one:foo one:bar) (two:foo two:bar)")
+ def testLeadingWildcard(self):
+ qp = MultiFieldQueryParser(Version.LUCENE_CURRENT, ['one', 'two'],
+ StandardAnalyzer(Version.LUCENE_CURRENT))
+ qp.setAllowLeadingWildcard(True)
+ q = qp.parse(Version.LUCENE_CURRENT, "*foo* bar", ['one', 'two'],
+ [BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD],
+ StandardAnalyzer(Version.LUCENE_CURRENT))
+ self.assertEquals(str(q), "(one:*foo* one:bar) (two:*foo* two:bar)")
+
if __name__ == "__main__":
import sys
initVM()