1 package org.apache.lucene.xmlparser.builders;
3 import org.apache.lucene.analysis.Analyzer;
4 import org.apache.lucene.search.FuzzyLikeThisQuery;
5 import org.apache.lucene.search.Query;
6 import org.apache.lucene.xmlparser.DOMUtils;
7 import org.apache.lucene.xmlparser.ParserException;
8 import org.apache.lucene.xmlparser.QueryBuilder;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.NodeList;
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
20 * http://www.apache.org/licenses/LICENSE-2.0
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.
32 public class FuzzyLikeThisQueryBuilder implements QueryBuilder
34 int defaultMaxNumTerms=50;
35 float defaultMinSimilarity=0.5f;
36 int defaultPrefixLength=1;
37 boolean defaultIgnoreTF=false;
38 private Analyzer analyzer;
40 public FuzzyLikeThisQueryBuilder(Analyzer analyzer)
42 this.analyzer=analyzer;
45 public Query getQuery(Element e) throws ParserException
47 NodeList nl = e.getElementsByTagName("Field");
48 int maxNumTerms=DOMUtils.getAttribute(e,"maxNumTerms",defaultMaxNumTerms);
49 FuzzyLikeThisQuery fbq=new FuzzyLikeThisQuery(maxNumTerms,analyzer);
50 fbq.setIgnoreTF(DOMUtils.getAttribute(e,"ignoreTF",defaultIgnoreTF));
51 for(int i=0;i<nl.getLength();i++)
53 Element fieldElem=(Element) nl.item(i);
54 float minSimilarity=DOMUtils.getAttribute(fieldElem,"minSimilarity",defaultMinSimilarity);
55 int prefixLength=DOMUtils.getAttribute(fieldElem,"prefixLength",defaultPrefixLength);
56 String fieldName=DOMUtils.getAttributeWithInheritance(fieldElem,"fieldName");
58 String value=DOMUtils.getText(fieldElem);
59 fbq.addTerms(value,fieldName,minSimilarity,prefixLength);
61 fbq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));