1、Lucene3.5索引搜索之前缀搜索

/**
* Lucene搜索之PrefixQuery(前缀搜索)
*/
public void searchByPrefixQuery(String field,String value){
IndexSearcher indexSearcher = createIndexSearcher();
Query query = new PrefixQuery(new Term(field,value));
excuteQuery(indexSearcher, query);
}


2、Lucene3.5索引搜索之通配符搜索

/**
* Lucene搜索之PrefixQuery(通配符搜索)
* eg:searchUtil.searchByWildcardQuery("name","a*");
*/
public void searchByWildcardQuery(String field,String value){
IndexSearcher indexSearcher = createIndexSearcher();
//传入的value中,可以输入通配符:?和*
Query query = new WildcardQuery(new Term(field,value));
excuteQuery(indexSearcher, query);
}


3、Lucene3.5索引搜索之BooleanQuery

/**
* Lucene搜索之BooleanQuery:
* BooleanQuery可以连接多个子查询
* BooleanClause.Occur.MUST 必须出现
* BooleanClause.Occur.SHOULD 可出现可不出现
* BooleanClause.Occur.MUST_NOT 禁止出现
*/
public void searchByBooleanQuery(){
IndexSearcher indexSearcher = createIndexSearcher();
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(new TermQuery(new Term("name","aa")), BooleanClause.Occur.MUST);
booleanQuery.add(new TermQuery(new Term("content","sister's")), BooleanClause.Occur.SHOULD);
excuteQuery(indexSearcher, booleanQuery);
}


4、Lucene3.5索引搜索之短语搜索

/**
* Lucene搜索之短语查询:
* 注意:对中文搜索没设么作用,对英文搜索效果很好(主要原因在于,英文分词一般用"空格")
*/
public void searchByPhraseQuery(){
IndexSearcher indexSearcher = createIndexSearcher();
PhraseQuery phraseQuery = new PhraseQuery();
//设置跳数(就是单词和单词之间的单词数)
phraseQuery.setSlop(1);
//设置第一个Term
phraseQuery.add(new Term("content","aa"));
//产生距离后的第二个Term
phraseQuery.add(new Term("content","my"));
excuteQuery(indexSearcher, phraseQuery);
}


5、Lucene3.5索引搜索之模糊搜索

/**
* Lucene搜索之模糊查询
*/
public void searchByFuzzyQuery(){
IndexSearcher indexSearcher = createIndexSearcher();
//FuzzyQuery(Term term, float minimumSimilarity):第二个参数为最小相似度(相似度越小,越能多的匹配)
FuzzyQuery fuzzyQuery = new FuzzyQuery(new Term("name","ab"),0.1f);
excuteQuery(indexSearcher, fuzzyQuery);
}