lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mharw...@apache.org
Subject svn commit: r417593 - in /lucene/java/trunk/contrib/xml-query-parser/src: java/org/apache/lucene/xmlparser/ java/org/apache/lucene/xmlparser/builders/ test/org/apache/lucene/xmlparser/
Date Tue, 27 Jun 2006 21:56:48 GMT
Author: mharwood
Date: Tue Jun 27 14:56:47 2006
New Revision: 417593

URL: http://svn.apache.org/viewvc?rev=417593&view=rev
Log:
Added new TermsQueryBuilder to simply build boolean queries from text without having concern
over clashing reserved words/special characters/legal syntax that is demanded by normal query
parser syntax. Added new xml attributes to BooleanQueryBuilder to control disableCoord and
minimumNumberShouldMatch.

Added:
    lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsQueryBuilder.java
    lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TermsQuery.xml
Modified:
    lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java
    lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java
    lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java

Modified: lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java?rev=417593&r1=417592&r2=417593&view=diff
==============================================================================
--- lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java
(original)
+++ lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java
Tue Jun 27 14:56:47 2006
@@ -21,6 +21,7 @@
 import org.apache.lucene.xmlparser.builders.SpanQueryBuilderFactory;
 import org.apache.lucene.xmlparser.builders.SpanTermBuilder;
 import org.apache.lucene.xmlparser.builders.TermQueryBuilder;
+import org.apache.lucene.xmlparser.builders.TermsQueryBuilder;
 import org.apache.lucene.xmlparser.builders.UserInputQueryBuilder;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -48,6 +49,7 @@
 		
 		queryFactory = new QueryBuilderFactory();
 		queryFactory.addBuilder("TermQuery",new TermQueryBuilder());
+		queryFactory.addBuilder("TermsQuery",new TermsQueryBuilder(analyzer));
 		queryFactory.addBuilder("MatchAllDocsQuery",new MatchAllDocsQueryBuilder());
 		queryFactory.addBuilder("BooleanQuery",new BooleanQueryBuilder(queryFactory));
 		queryFactory.addBuilder("UserQuery",new UserInputQueryBuilder(parser));

Modified: lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java?rev=417593&r1=417592&r2=417593&view=diff
==============================================================================
--- lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java
(original)
+++ lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BooleanQueryBuilder.java
Tue Jun 27 14:56:47 2006
@@ -29,7 +29,8 @@
 	 * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
 	 */
 	public Query getQuery(Element e) throws ParserException {
-		BooleanQuery bq=new BooleanQuery();
+		BooleanQuery bq=new BooleanQuery(DOMUtils.getAttribute(e,"disableCoord",false));
+		bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,"minimumNumberShouldMatch",0));
 		bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
 		NodeList nl = e.getElementsByTagName("Clause");
 		for(int i=0;i<nl.getLength();i++)

Added: lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsQueryBuilder.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsQueryBuilder.java?rev=417593&view=auto
==============================================================================
--- lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsQueryBuilder.java
(added)
+++ lucene/java/trunk/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/TermsQueryBuilder.java
Tue Jun 27 14:56:47 2006
@@ -0,0 +1,76 @@
+/*
+ * Created on 25-Jan-2006
+ */
+package org.apache.lucene.xmlparser.builders;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.Token;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.xmlparser.DOMUtils;
+import org.apache.lucene.xmlparser.ParserException;
+import org.apache.lucene.xmlparser.QueryBuilder;
+import org.w3c.dom.Element;
+
+
+/**
+ * Builds a BooleanQuery from all of the terms found in the XML element using the choice
of analyzer
+ * @author maharwood
+ */
+public class TermsQueryBuilder implements QueryBuilder {
+
+	Analyzer analyzer;
+
+		
+	public TermsQueryBuilder(Analyzer analyzer)
+	{
+		this.analyzer = analyzer;
+	}
+
+
+
+	public Query getQuery(Element e) throws ParserException {
+		
+        String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
+ 		String text=DOMUtils.getNonBlankTextOrFail(e);
+ 		
+		BooleanQuery bq=new BooleanQuery(DOMUtils.getAttribute(e,"disableCoord",false));
+		bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,"minimumNumberShouldMatch",0));
+		TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
+		try
+		{
+			Token token = ts.next();
+			Term term = null;
+			while (token != null)
+			{
+				if (term == null)
+				{
+					term = new Term(fieldName, token.termText());
+				} else
+				{
+//					 create from previous to save fieldName.intern overhead
+					term = term.createTerm(token.termText()); 
+				}
+				bq.add(new BooleanClause(new TermQuery(term),BooleanClause.Occur.SHOULD));
+				token = ts.next();
+			}
+		} 
+		catch (IOException ioe)
+		{
+			throw new RuntimeException("Error constructing terms from index:"
+					+ ioe);
+		}
+  		bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
+
+  		return bq;
+		
+	}
+
+}

Added: lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TermsQuery.xml
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TermsQuery.xml?rev=417593&view=auto
==============================================================================
--- lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TermsQuery.xml
(added)
+++ lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TermsQuery.xml
Tue Jun 27 14:56:47 2006
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+		<!-- TermsQuery uses an analyzer to tokenize text and creates a BooleanQuery with nested
+			"should" TermQueries for each of the tokens encountered. This can be used for user input
+			which may include content or characters that would otherwise be illegal query syntax when
+			using the standard lucene query parser. Of course the downside is that none of the query

+			operators (AND NOT ~ ^ : etc) will have an effect. For some scenarios queries are
+			not formed by people familiar with Lucene query syntax and they can inadvertently type
illegal
+			query syntax so in these cases this is an appropriate and simple alternative
+		--> 
+<TermsQuery fieldName="contents">sumitomo bank</TermsQuery>
\ No newline at end of file

Modified: lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java?rev=417593&r1=417592&r2=417593&view=diff
==============================================================================
--- lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java
(original)
+++ lucene/java/trunk/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/TestParser.java
Tue Jun 27 14:56:47 2006
@@ -85,6 +85,11 @@
 			Query q=parse("TermQuery.xml");
 			dumpResults("TermQuery", q, 5);
 	}
+	public void testSimpleTermsQueryXML() throws ParserException, IOException
+	{
+			Query q=parse("TermsQuery.xml");
+			dumpResults("TermsQuery", q, 5);
+	}
 	public void testBooleanQueryXML() throws ParserException, IOException
 	{
 			Query q=parse("BooleanQuery.xml");



Mime
View raw message