KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > QueryWordsPhrase


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.search;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.apache.lucene.index.*;
17 import org.apache.lucene.search.*;
18 /**
19  * Represents a phrase (not quoted) token in user search query words It consists
20  * of several words created by an analyzer
21  */

22 public class QueryWordsPhrase extends QueryWordsToken {
23     private List JavaDoc words;
24     public QueryWordsPhrase() {
25         super(QueryWordsToken.PHRASE, ""); //$NON-NLS-1$
26
words = new ArrayList JavaDoc();
27     }
28     public void addWord(String JavaDoc word) {
29         words.add(word);
30         if (words.size() <= 1)
31             value = word;
32         else
33             value += " " + word; //$NON-NLS-1$
34
}
35     public List JavaDoc getWords() {
36         return words;
37     }
38     /**
39      * Creates a lucene query for a field
40      */

41     public Query createLuceneQuery(String JavaDoc field, float boost) {
42         PhraseQuery q = new PhraseQuery();
43         for (Iterator JavaDoc it = getWords().iterator(); it.hasNext();) {
44             String JavaDoc word = (String JavaDoc) it.next();
45             Term t = new Term(field, word);
46             q.add(t);
47             q.setBoost(boost);
48         }
49         return q;
50     }
51 }
52
Popular Tags