Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-1531 Added support for BoostingTermQuery to XML query parser.

Karl-Johan Wettin6e692d38ec21
diff --git a/contrib/CHANGES.txt b/contrib/CHANGES.txt
index dc4d38db796..53f1af96d9a 100644
--- a/contrib/CHANGES.txt
+++ b/contrib/CHANGES.txt
@@ -31,6 +31,9 @@ New features
RangeQuery at the expense of added space (additional indexed
tokens) consumed in the index. (Uwe Schindler via Mike McCandless)
+ 2. LUCENE-1531: Added support for BoostingTermQuery to XML query parser. (Karl Wettin)
+
+
Documentation
(None)
diff --git a/contrib/xml-query-parser/LuceneCoreQuery.dtd b/contrib/xml-query-parser/LuceneCoreQuery.dtd
index 6978209f6c6..4208d434573 100644
--- a/contrib/xml-query-parser/LuceneCoreQuery.dtd
+++ b/contrib/xml-query-parser/LuceneCoreQuery.dtd
@@ -53,8 +53,8 @@
-->
<!-- @hidden Define core types of XML elements -->
-<!ENTITY % coreSpanQueries "SpanOr|SpanNear|SpanOrTerms|SpanFirst|SpanNot|SpanTerm" >
-<!ENTITY % coreQueries "BooleanQuery|UserQuery|FilteredQuery|TermQuery|TermsQuery|MatchAllDocsQuery|ConstantScoreQuery" >
+<!ENTITY % coreSpanQueries "SpanOr|SpanNear|SpanOrTerms|SpanFirst|SpanNot|SpanTerm|BoostingTermQuery" >
+<!ENTITY % coreQueries "BooleanQuery|UserQuery|FilteredQuery|TermQuery|TermsQuery|MatchAllDocsQuery|ConstantScoreQuery|BoostingTermQuery" >
<!ENTITY % coreFilters "RangeFilter|CachedFilter" >
<!-- @hidden Allow for extensions -->
@@ -186,6 +186,29 @@ Passes content directly through to the standard LuceneQuery parser see "Lucene Q
<!ATTLIST TermQuery fieldName CDATA #IMPLIED>
+<!--
+ A boosted term query - no analysis is done of the child text. Also a span member.
+
+ (Text below is copied from the javadocs of BoostingTermQuery)
+
+ The BoostingTermQuery is very similar to the {@link org.apache.lucene.search.spans.SpanTermQuery} except
+ that it factors in the value of the payload located at each of the positions where the
+ {@link org.apache.lucene.index.Term} occurs.
+
+ In order to take advantage of this, you must override {@link org.apache.lucene.search.Similarity#scorePayload(String, byte[],int,int)}
+ which returns 1 by default.
+
+ Payload scores are averaged across term occurrences in the document.
+
+ @see org.apache.lucene.search.Similarity#scorePayload(String, byte[], int, int)
+-->
+<!ELEMENT BoostingTermQuery (#PCDATA)>
+<!-- Optional boost for matches on this query. Values > 1 -->
+<!ATTLIST TermQuery boost CDATA "1.0">
+<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->
+<!ATTLIST TermQuery fieldName CDATA #IMPLIED>
+
+
<!--
The equivalent of a BooleanQuery with multiple optional TermQuery clauses.
diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java
index b0fba5f3a68..fa2f61738cb 100644
--- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java
+++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/CoreParser.java
@@ -8,22 +8,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
-import org.apache.lucene.xmlparser.builders.BooleanQueryBuilder;
-import org.apache.lucene.xmlparser.builders.ConstantScoreQueryBuilder;
-import org.apache.lucene.xmlparser.builders.FilteredQueryBuilder;
-import org.apache.lucene.xmlparser.builders.MatchAllDocsQueryBuilder;
-import org.apache.lucene.xmlparser.builders.CachedFilterBuilder;
-import org.apache.lucene.xmlparser.builders.RangeFilterBuilder;
-import org.apache.lucene.xmlparser.builders.SpanFirstBuilder;
-import org.apache.lucene.xmlparser.builders.SpanNearBuilder;
-import org.apache.lucene.xmlparser.builders.SpanNotBuilder;
-import org.apache.lucene.xmlparser.builders.SpanOrBuilder;
-import org.apache.lucene.xmlparser.builders.SpanOrTermsBuilder;
-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.apache.lucene.xmlparser.builders.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -113,7 +98,11 @@ public class CoreParser implements QueryBuilder
sqof.addBuilder("SpanNear",snb);
queryFactory.addBuilder("SpanNear",snb);
- SpanTermBuilder snt=new SpanTermBuilder();
+ BoostingTermBuilder btb=new BoostingTermBuilder();
+ sqof.addBuilder("BoostingTermQuery",btb);
+ queryFactory.addBuilder("BoostingTermQuery",btb);
+
+ SpanTermBuilder snt=new SpanTermBuilder();
sqof.addBuilder("SpanTerm",snt);
queryFactory.addBuilder("SpanTerm",snt);
diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BoostingTermBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BoostingTermBuilder.java
new file mode 100644
index 00000000000..f4d979b9770
--- /dev/null
+++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/BoostingTermBuilder.java
@@ -0,0 +1,41 @@
+package org.apache.lucene.xmlparser.builders;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.spans.SpanQuery;
+import org.apache.lucene.search.spans.SpanTermQuery;
+import org.apache.lucene.search.payloads.BoostingTermQuery;
+import org.apache.lucene.xmlparser.DOMUtils;
+import org.apache.lucene.xmlparser.ParserException;
+import org.w3c.dom.Element;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class BoostingTermBuilder extends SpanBuilderBase
+{
+
+ public SpanQuery getSpanQuery(Element e) throws ParserException
+ {
+ String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
+ String value=DOMUtils.getNonBlankTextOrFail(e);
+ BoostingTermQuery btq = new BoostingTermQuery(new Term(fieldName,value));
+
+ btq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
+ return btq;
+
+ }
+
+}
\ No newline at end of file
diff --git a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/BoostingTermQuery.xml b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/BoostingTermQuery.xml
new file mode 100644
index 00000000000..efeb301a3c3
--- /dev/null
+++ b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/BoostingTermQuery.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<BoostingTermQuery fieldName="contents">sumitomo</BoostingTermQuery>
diff --git a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/SpanQuery.xml b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/SpanQuery.xml
index eaaa9703e5d..896d0291f77 100644
--- a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/SpanQuery.xml
+++ b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/SpanQuery.xml
@@ -15,7 +15,8 @@
<SpanNear slop="6" inOrder="false">
<SpanTerm>mine</SpanTerm>
<SpanOrTerms>worker workers</SpanOrTerms>
- </SpanNear>
+ <BoostingTermQuery>heavy</BoostingTermQuery>
+ </SpanNear>
</SpanOr>
</SpanNear>
<SpanFirst end="10">
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 87c9679e24b..88dc76217a8 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
@@ -1,9 +1,6 @@
package org.apache.lucene.xmlparser;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
+import java.io.*;
import junit.framework.TestCase;
@@ -74,7 +71,7 @@ public class TestParser extends TestCase {
line=d.readLine();
}
d.close();
- writer.close();
+ writer.close();
}
reader=IndexReader.open(dir);
searcher=new IndexSearcher(reader);
@@ -148,7 +145,12 @@ public class TestParser extends TestCase {
Query q=parse("TermsFilterQuery.xml");
dumpResults("Terms Filter",q, 5);
}
- public void testSpanTermXML() throws Exception
+ public void testBoostingTermQueryXML() throws Exception
+ {
+ Query q=parse("BoostingTermQuery.xml");
+ dumpResults("BoostingTermQuery",q, 5);
+ }
+ public void testSpanTermXML() throws Exception
{
Query q=parse("SpanQuery.xml");
dumpResults("Span Query",q, 5);