01: package org.apache.lucene.xmlparser.builders;
02:
03: import org.apache.lucene.analysis.Analyzer;
04: import org.apache.lucene.search.FuzzyLikeThisQuery;
05: import org.apache.lucene.search.Query;
06: import org.apache.lucene.xmlparser.DOMUtils;
07: import org.apache.lucene.xmlparser.ParserException;
08: import org.apache.lucene.xmlparser.QueryBuilder;
09: import org.w3c.dom.Element;
10: import org.w3c.dom.NodeList;
11:
12: /**
13: * Licensed to the Apache Software Foundation (ASF) under one or more
14: * contributor license agreements. See the NOTICE file distributed with
15: * this work for additional information regarding copyright ownership.
16: * The ASF licenses this file to You under the Apache License, Version 2.0
17: * (the "License"); you may not use this file except in compliance with
18: * the License. You may obtain a copy of the License at
19: *
20: * http://www.apache.org/licenses/LICENSE-2.0
21: *
22: * Unless required by applicable law or agreed to in writing, software
23: * distributed under the License is distributed on an "AS IS" BASIS,
24: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25: * See the License for the specific language governing permissions and
26: * limitations under the License.
27: */
28: public class FuzzyLikeThisQueryBuilder implements QueryBuilder {
29: int defaultMaxNumTerms = 50;
30: float defaultMinSimilarity = 0.5f;
31: int defaultPrefixLength = 1;
32: boolean defaultIgnoreTF = false;
33: private Analyzer analyzer;
34:
35: public FuzzyLikeThisQueryBuilder(Analyzer analyzer) {
36: this .analyzer = analyzer;
37: }
38:
39: public Query getQuery(Element e) throws ParserException {
40: NodeList nl = e.getElementsByTagName("Field");
41: int maxNumTerms = DOMUtils.getAttribute(e, "maxNumTerms",
42: defaultMaxNumTerms);
43: FuzzyLikeThisQuery fbq = new FuzzyLikeThisQuery(maxNumTerms,
44: analyzer);
45: fbq.setIgnoreTF(DOMUtils.getAttribute(e, "ignoreTF",
46: defaultIgnoreTF));
47: for (int i = 0; i < nl.getLength(); i++) {
48: Element fieldElem = (Element) nl.item(i);
49: float minSimilarity = DOMUtils.getAttribute(fieldElem,
50: "minSimilarity", defaultMinSimilarity);
51: int prefixLength = DOMUtils.getAttribute(fieldElem,
52: "prefixLength", defaultPrefixLength);
53: String fieldName = DOMUtils.getAttributeWithInheritance(
54: fieldElem, "fieldName");
55:
56: String value = DOMUtils.getText(fieldElem);
57: fbq.addTerms(value, fieldName, minSimilarity, prefixLength);
58: }
59: fbq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
60:
61: return fbq;
62: }
63:
64: }
|