KickJava   Java API By Example, From Geeks To Geeks.

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


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 quoted token in user search query words
20  */

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

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