LUCENE-4004: add DisjunctionMaxQuery support to the xml query parser

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1328981 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-04-22 21:28:15 +00:00
parent a20aa3e0c9
commit 36bc3d70bb
6 changed files with 117 additions and 1 deletions

View File

@ -843,6 +843,9 @@ New features
analysis/ module. The 'smartcn' and 'stempel' components now depend on 'common'.
(Chris Male, Robert Muir)
* LUCENE-4004: Add DisjunctionMaxQuery support to the xml query parser.
(Benson Margulies via Robert Muir)
Optimizations
* LUCENE-2588: Don't store unnecessary suffixes when writing the terms

View File

@ -78,16 +78,31 @@ public class DisjunctionMaxQuery extends Query implements Iterable<Query> {
/** Add a collection of disjuncts to this disjunction
* via Iterable<Query>
* @param disjuncts a collection of queries to add as disjuncts.
*/
public void add(Collection<Query> disjuncts) {
this.disjuncts.addAll(disjuncts);
}
/** An Iterator<Query> over the disjuncts */
/** @return An Iterator<Query> over the disjuncts */
public Iterator<Query> iterator() {
return disjuncts.iterator();
}
/**
* @return the disjuncts.
*/
public ArrayList<Query> getDisjuncts() {
return disjuncts;
}
/**
* @return tie breaker value for multiple matches.
*/
public float getTieBreakerMultiplier() {
return tieBreakerMultiplier;
}
/**
* Expert: the Weight for DisjunctionMaxQuery, used to
* normalize, score and explain these queries.
@ -279,4 +294,5 @@ public class DisjunctionMaxQuery extends Query implements Iterable<Query> {
+ disjuncts.hashCode();
}
}

View File

@ -75,6 +75,7 @@ public class CoreParser implements QueryBuilder {
queryFactory.addBuilder("MatchAllDocsQuery", new MatchAllDocsQueryBuilder());
queryFactory.addBuilder("BooleanQuery", new BooleanQueryBuilder(queryFactory));
queryFactory.addBuilder("NumericRangeQuery", new NumericRangeQueryBuilder());
queryFactory.addBuilder("DisjunctionMaxQuery", new DisjunctionMaxQueryBuilder(queryFactory));
if (parser != null) {
queryFactory.addBuilder("UserQuery", new UserInputQueryBuilder(parser));
} else {

View File

@ -0,0 +1,60 @@
package org.apache.lucene.queryparser.xml.builders;
/**
* 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.
*/
import org.apache.lucene.queryparser.xml.DOMUtils;
import org.apache.lucene.queryparser.xml.ParserException;
import org.apache.lucene.queryparser.xml.QueryBuilder;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.Query;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Build a DisjunctionMaxQuery.
*/
public class DisjunctionMaxQueryBuilder implements QueryBuilder {
private final QueryBuilder factory;
public DisjunctionMaxQueryBuilder(QueryBuilder factory) {
this.factory = factory;
}
/* (non-Javadoc)
* @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
*/
public Query getQuery(Element e) throws ParserException {
float tieBreaker = DOMUtils.getAttribute(e, "tieBreaker", 0.0f);
DisjunctionMaxQuery dq = new DisjunctionMaxQuery(tieBreaker);
dq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
NodeList nl = e.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) { // all elements are disjuncts.
Element queryElem = (Element) node;
Query q = factory.getQuery(queryElem);
dq.add(q);
}
}
return dq;
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<DisjunctionMaxQuery>
<TermQuery fieldName="a">merger</TermQuery>
<DisjunctionMaxQuery tieBreaker="1.2">
<TermQuery fieldName="b">verger</TermQuery>
</DisjunctionMaxQuery>
</DisjunctionMaxQuery>

View File

@ -27,6 +27,7 @@ import org.apache.lucene.document.TextField;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
@ -105,6 +106,17 @@ public class TestParser extends LuceneTestCase {
dumpResults("BooleanQuery", q, 5);
}
public void testDisjunctionMaxQueryXML() throws ParserException, IOException {
Query q = parse("DisjunctionMaxQuery.xml");
assertTrue(q instanceof DisjunctionMaxQuery);
DisjunctionMaxQuery d = (DisjunctionMaxQuery)q;
assertEquals(0.0f, d.getTieBreakerMultiplier(), 0.0001f);
assertEquals(2, d.getDisjuncts().size());
DisjunctionMaxQuery ndq = (DisjunctionMaxQuery) d.getDisjuncts().get(1);
assertEquals(1.2f, ndq.getTieBreakerMultiplier(), 0.0001f);
assertEquals(1, ndq.getDisjuncts().size());
}
public void testRangeFilterQueryXML() throws ParserException, IOException {
Query q = parse("RangeFilterQuery.xml");
dumpResults("RangeFilter", q, 5);