1 11 package org.eclipse.help.internal.search; 12 import org.apache.lucene.index.*; 13 import org.apache.lucene.search.*; 14 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"); private static final QueryWordsToken fOR = new QueryWordsToken(OR, "OR"); private static final QueryWordsToken fNOT = new QueryWordsToken(NOT, "NOT"); public int type; 28 public String value; 29 protected QueryWordsToken(int type, String value) { 30 this.type = type; 31 this.value = value; 32 } 33 36 public Query createLuceneQuery(String 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)); q = new PrefixQuery(t); 44 ((PrefixQuery) q).setBoost(boost); 45 } else { 46 Term t = new Term("exact_" + field, value); 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 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 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 word) { 77 QueryWordsExactPhrase token = new QueryWordsExactPhrase(); 78 token.addWord(word); 79 return token; 80 } 81 } 82 | Popular Tags |