01: package org.apache.lucene.xmlparser.builders;
02:
03: import org.apache.lucene.search.BoostingQuery;
04: import org.apache.lucene.search.Query;
05: import org.apache.lucene.xmlparser.DOMUtils;
06: import org.apache.lucene.xmlparser.ParserException;
07: import org.apache.lucene.xmlparser.QueryBuilder;
08: import org.w3c.dom.Element;
09:
10: /**
11: * Licensed to the Apache Software Foundation (ASF) under one or more
12: * contributor license agreements. See the NOTICE file distributed with
13: * this work for additional information regarding copyright ownership.
14: * The ASF licenses this file to You under the Apache License, Version 2.0
15: * (the "License"); you may not use this file except in compliance with
16: * the License. You may obtain a copy of the License at
17: *
18: * http://www.apache.org/licenses/LICENSE-2.0
19: *
20: * Unless required by applicable law or agreed to in writing, software
21: * distributed under the License is distributed on an "AS IS" BASIS,
22: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23: * See the License for the specific language governing permissions and
24: * limitations under the License.
25: */
26:
27: public class BoostingQueryBuilder implements QueryBuilder {
28:
29: private QueryBuilder factory;
30: float defaultBoost = 0.01f;
31:
32: public BoostingQueryBuilder(QueryBuilder factory) {
33: this .factory = factory;
34: }
35:
36: public Query getQuery(Element e) throws ParserException {
37:
38: Element mainQueryElem = DOMUtils
39: .getChildByTagOrFail(e, "Query");
40: mainQueryElem = DOMUtils.getFirstChildOrFail(mainQueryElem);
41: Query mainQuery = factory.getQuery(mainQueryElem);
42:
43: Element boostQueryElem = DOMUtils.getChildByTagOrFail(e,
44: "BoostQuery");
45: float boost = DOMUtils.getAttribute(boostQueryElem, "boost",
46: defaultBoost);
47: boostQueryElem = DOMUtils.getFirstChildOrFail(boostQueryElem);
48: Query boostQuery = factory.getQuery(boostQueryElem);
49:
50: BoostingQuery bq = new BoostingQuery(mainQuery, boostQuery,
51: boost);
52:
53: bq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
54: return bq;
55:
56: }
57:
58: }
|