pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / builders / FuzzyLikeThisQueryBuilder.java
1 package org.apache.lucene.xmlparser.builders;
2
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;
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
29 /**
30  * 
31  */
32 public class FuzzyLikeThisQueryBuilder implements QueryBuilder
33 {
34         int defaultMaxNumTerms=50;
35         float defaultMinSimilarity=0.5f;
36         int defaultPrefixLength=1;
37         boolean defaultIgnoreTF=false;
38         private Analyzer analyzer;
39         
40         public FuzzyLikeThisQueryBuilder(Analyzer analyzer)
41         {
42                 this.analyzer=analyzer;
43         }
44
45         public Query getQuery(Element e) throws ParserException
46         {
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++)
52                 {
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");
57                         
58                         String value=DOMUtils.getText(fieldElem);
59                         fbq.addTerms(value,fieldName,minSimilarity,prefixLength);
60                 }
61                 fbq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
62
63                 return fbq;
64         }
65
66 }