KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.lucene.index.*;
13 import org.apache.lucene.search.*;
14 /**
15  * Represents a token in user search query words
16  */

17 public class QueryWordsToken {
18     public static final int AND = 0;
19     public static final int OR = 1;
20     public static final int NOT = 2;
21     public static final int EXACT_PHRASE = 3;
22     public static final int PHRASE = 4;
23     public static final int WORD = 5;
24     private static final QueryWordsToken fAND = new QueryWordsToken(AND, "AND"); //$NON-NLS-1$
25
private static final QueryWordsToken fOR = new QueryWordsToken(OR, "OR"); //$NON-NLS-1$
26
private static final QueryWordsToken fNOT = new QueryWordsToken(NOT, "NOT"); //$NON-NLS-1$
27
public int type;
28     public String JavaDoc value;
29     protected QueryWordsToken(int type, String JavaDoc value) {
30         this.type = type;
31         this.value = value;
32     }
33     /**
34      * Creates a lucene query for a field
35      */

36     public Query createLuceneQuery(String JavaDoc field, float boost) {
37         Query q;
38         int questionPos = value.indexOf('?');
39         int starPos = value.indexOf('*');
40         if (questionPos >= 0 || starPos >= 0) {
41             if (questionPos == -1 && starPos == value.length() - 1) {
42                 Term t = new Term("exact_" + field, value.substring(0, starPos)); //$NON-NLS-1$
43
q = new PrefixQuery(t);
44                 ((PrefixQuery) q).setBoost(boost);
45             } else {
46                 Term t = new Term("exact_" + field, value); //$NON-NLS-1$
47
q = new WildcardQuery(t);
48                 ((WildcardQuery) q).setBoost(boost);
49             }
50         } else {
51             Term t = new Term(field, value);
52             q = new TermQuery(t);
53             ((TermQuery) q).setBoost(boost);
54         }
55         // after updating Lucene, set boost on a Query class
56
return q;
57     }
58     public static QueryWordsToken AND() {
59         return fAND;
60     }
61     public static QueryWordsToken OR() {
62         return fOR;
63     }
64     public static QueryWordsToken NOT() {
65         return fNOT;
66     }
67     public static QueryWordsToken word(String JavaDoc word) {
68         return new QueryWordsToken(QueryWordsToken.WORD, word);
69     }
70     public static QueryWordsPhrase phrase() {
71         return new QueryWordsPhrase();
72     }
73     public static QueryWordsExactPhrase exactPhrase() {
74         return new QueryWordsExactPhrase();
75     }
76     public static QueryWordsExactPhrase exactPhrase(String JavaDoc word) {
77         QueryWordsExactPhrase token = new QueryWordsExactPhrase();
78         token.addWord(word);
79         return token;
80     }
81 }
82
Popular Tags