diff --git a/contrib/xml-query-parser/LuceneCoreQuery.dtd b/contrib/xml-query-parser/LuceneCoreQuery.dtd index 09f10bf1d36..6978209f6c6 100644 --- a/contrib/xml-query-parser/LuceneCoreQuery.dtd +++ b/contrib/xml-query-parser/LuceneCoreQuery.dtd @@ -145,6 +145,8 @@ Passes content directly through to the standard LuceneQuery parser see "Lucene Q + + diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/UserInputQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/UserInputQueryBuilder.java index 48fb23c68ef..22fd60b9969 100644 --- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/UserInputQueryBuilder.java +++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/UserInputQueryBuilder.java @@ -40,6 +40,7 @@ public class UserInputQueryBuilder implements QueryBuilder { private String defaultField; /** + * This constructor has the disadvantage of not being able to change choice of default field name * @param parser thread un-safe query parser */ public UserInputQueryBuilder(QueryParser parser) { @@ -68,8 +69,9 @@ public class UserInputQueryBuilder implements QueryBuilder { } else { + String fieldName=DOMUtils.getAttribute(e, "fieldName", defaultField); //Create new parser - QueryParser parser=new QueryParser(defaultField,analyzer); + QueryParser parser=createQueryParser(fieldName, analyzer); q = parser.parse(text); } q.setBoost(DOMUtils.getAttribute(e,"boost",1.0f)); @@ -78,5 +80,16 @@ public class UserInputQueryBuilder implements QueryBuilder { throw new ParserException(e1.getMessage()); } } + + /** + * Method to create a QueryParser - designed to be overridden + * @param fieldName + * @param analyzer + * @return + */ + protected QueryParser createQueryParser(String fieldName, Analyzer analyzer) + { + return new QueryParser(fieldName,analyzer); + } } diff --git a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java index 4f483c97d05..e7c72cb7f11 100644 --- a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java +++ b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java @@ -117,6 +117,14 @@ public class TestParser extends TestCase { Query q=parse("UserInputQuery.xml"); dumpResults("UserInput with Filter", q, 5); } + + public void testCustomFieldUserQueryXML() throws ParserException, IOException + { + Query q=parse("UserInputQueryCustomField.xml"); + Hits h = searcher.search(q); + assertEquals("UserInputQueryCustomField should produce 0 result ", 0,h.length()); + } + public void testLikeThisQueryXML() throws Exception { Query q=parse("LikeThisQuery.xml"); diff --git a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/UserInputQueryCustomField.xml b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/UserInputQueryCustomField.xml new file mode 100644 index 00000000000..ec8be2e58f8 --- /dev/null +++ b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/UserInputQueryCustomField.xml @@ -0,0 +1,2 @@ + +Bank