Lucene基本的查詢語句:
Searcher searcher = new IndexSearcher(dbpath);
Query query = QueryParser.parse(searchkey, searchfield,
new StandardAnalyzer());
Hits hits = searcher.search(query);
下面是Query的各種子查詢,他們斗魚QueryParser都有對應關系。
1.TermQuery常用,對一個Term(最小的索引塊,包含一個field名字和值)進行索引查詢。
Term直接與QueryParser.parse里面的key和field直接對應。
IndexSearcher searcher = new IndexSearcher(directory);
Term t = new Term("isbn", "1930110995");
Query query = new TermQuery(t);
Hits hits = searcher.search(query);
2.RangeQuery用于區間查詢,RangeQuery的第三個參數表示是開區間還是閉區間。
QueryParser會構建從begin到end之間的N個查詢進行查詢。
Term begin, end;
Searcher searcher = new IndexSearcher(dbpath);
begin = new Term("pubmonth","199801");
end = new Term("pubmonth","199810");
RangeQuery query = new RangeQuery(begin, end, true);
RangeQuery本質是比較大小。所以如下查詢也是可以的,但是意義就于上面不大一樣了,總之是大小的比較
設定了一個區間,在區間內的都能夠搜索出來,這里就存在一個比較大小的原則,比如字符串會首先比較第一個字符,這樣與字符長度沒有關系。
begin = new Term("pubmonth","19");
end = new Term("pubmonth","20");
RangeQuery query = new RangeQuery(begin, end, true);
3.PrefixQuery.對于TermQuery,必須完全匹配(用Field.Keyword生成的字段)才能夠查詢出來。
這就制約了查詢的靈活性,PrefixQuery只需要匹配value的前面任何字段即可。如Field為name,記錄
中那么有jackliu,jackwu,jackli,那么使用jack就可以查詢出所有的記錄。QueryParser creates a PrefixQuery
for a term when it ends with an asterisk (*) in query expressions.
IndexSearcher searcher = new IndexSearcher(directory);
Term term = new Term("category", "/technology/computers/programming");
PrefixQuery query = new PrefixQuery(term);
Hits hits = searcher.search(query);
4.BooleanQuery.上面所有的查詢都是基于單個field的查詢,多個field怎么查詢呢,BooleanQuery
就是解決多個查詢的問題。通過add(Query query, boolean required, boolean prohibited)加入
多個查詢.通過BooleanQuery的嵌套可以組合非常復雜的查詢。
IndexSearcher searcher = new IndexSearcher(directory);
TermQuery searchingBooks =
new TermQuery(new Term("subject","search"));
RangeQuery currentBooks =
new RangeQuery(new Term("pubmonth","200401"),
new Term("pubmonth","200412"),true);
BooleanQuery currentSearchingBooks = new BooleanQuery();
currentSearchingBooks.add(searchingBook s, true, false);
currentSearchingBooks.add(currentBooks, true, false);
Hits hits = searcher.search(currentSearchingBooks);
BooleanQuery的add方法有兩個boolean參數:
true&false:表明當前加入的子句是必須要滿足的;
false&true:表明當前加入的子句是不可以被滿足的;
false&false:表明當前加入的子句是可選的;
true&true:錯誤的情況。
QueryParser handily constructs BooleanQuerys when multiple terms are specified.
Grouping is done with parentheses, and the prohibited and required flags are
set when the –, +, AND, OR, and NOT operators are specified.
5.PhraseQuery進行更為精確的查找。它能夠對索引文本中的兩個或更多的關鍵詞的位置進行
限定。如搜查包含A和B并且A、B之間還有一個文字。Terms surrounded by double quotes in
QueryParser parsed expressions are translated into a PhraseQuery.
The slop factor defaults to zero, but you can adjust the slop factor
by adding a tilde (~) followed by an integer.
For example, the expression "quick fox"~3
6.WildcardQuery.WildcardQuery比PrefixQuery提供了更細的控制和更大的靈活性,這個最容易
理解和使用。
7.FuzzyQuery.這個Query比較特別,它會查詢與關鍵字長得很像的其他記錄。QueryParser
supports FuzzyQuery by suffixing a term with a tilde (~),for exmaple wuzza~.
public void testFuzzy() throws Exception {
indexSingleFieldDocs(new Field[] {
Field.Text("contents", "fuzzy"),
Field.Text("contents", "wuzzy")
});
IndexSearcher searcher = new IndexSearcher(directory);
Query query = new FuzzyQuery(new Term("contents", "wuzza"));
Hits hits = searcher.search(query);
assertEquals("both close enough", 2, hits.length());
assertTrue("wuzzy closer than fuzzy",
hits.score(0) != hits.score(1));
assertEquals("wuzza bear","wuzzy", hits.doc(0).get("contents"));
}