Lucence內建的Query物件lucene

2021-05-23 01:02:32 字數 3021 閱讀 7958

lucence內建的query物件lucene

1.termquery 詞條搜尋

解析:對某個固定詞條的指定,實現搜尋索引中存在該詞條的文件

示例:indexsearcher searcher = new indexsearcher(index_store_path);

term t = new term("bookname", "鋼);

query query = new termquery(t);

system.out.println(query);

hits hits = searcher.search(query);

2.booleanquery 布林搜尋

解析:lucence各種複雜的查詢,最終都表示成boolean型,用法相當於sql查詢語句中條件的 and ,or 作用

booleanclause.occur.should  or的關係

booleanclause.occur.must  and 的關係

booleanclause.occur.must_not  not的關係

示例:indexsearcher searcher = new indexsearcher(index_store_path);

term t = new term("bookname", "一);

term t2 = new term("bookname", "三);

termquery q1 = new termquery(t);

termquery q2 = new termquery(t2);

booleanquery query = new booleanquery();

query.add(q1, booleanclause.occur.should);

query.add(q2, booleanclause.occur.should);

system.out.println(query);

hits hits = searcher.search(query);

3.rangequery範圍搜尋

解析:用於查詢一定範圍內的文件

示例:term begin = new term("booknumber", "0000004");

term end = new term("booknumber", "0000008");

query query = new rangequery(begin,end,inclusive);

system.out.println(query);

hits hits = searcher.search(query);

4.prefixquery 字首搜尋

解析:字首搜尋,相當於sql查詢的 like 'xx%' 

示例:indexsearcher searcher = new indexsearcher(index_store_path);

term prefix = new term("bookname",searchvalue);

prefixquery query = new prefixquery(prefix);

system.out.println(query);

hits hits = searcher.search(query);

5.phrasequery 短語搜尋

解析:將輸入的詞條當短語來查詢 

示例:indexsearcher searcher = new indexsearcher(index_store_path);

phrasequery query = new phrasequery();

query.add(new term("bookname","鋼) );

query.add(new term("bookname","鐵));

query.setslop(2);

system.out.println(query);

//搜尋含有鋼和鐵關鍵字

hits hits = searcher.search(query);

6.multiphrasequery 多短語搜尋

解析:示例:

indexsearcher searcher = new indexsearcher(index_store_path);

multiphrasequery query = new multiphrasequery();

term t =new term("bookname","鋼);

term t2 = new term("bookname","和);

query.add(new term);

query.add(new term("bookname","鐵));

term t4 = new term("bookname","的);

term t3 = new term("bookname","戰);

query.add(new term);

system.out.println(query);

hits hits = searcher.search(query);

7.fuzzyquery 模糊搜尋

解析:對單字的模糊查詢

示例:indexsearcher searcher  = new indexsearcher(index_store_path);

term t =new term("bookname","鋼);

fuzzyquery query = new fuzzyquery(t,0.3f,1);

system.out.println(query);

hits hits = searcher.search(query);

8.wildcardquery 萬用字元搜尋

解析:在查詢時借助萬用字元,來匹配

示例:term t = new term("bookname","鋼");

wildcardquery query = new wildcardquery(t);

system.out.println(query);

hits hits = searcher.search(query);

理解Lucene中的Query

query是乙個介面,它有很多實現類。queryparser是query解析器,用於將乙個字串解析為乙個query物件,這個query物件可能屬於termquery,也可能屬於phrasequery termquery booleanquery等。可以通過query物件的getclass 方法來檢視...

JPA中 Query的使用

在使用 query中,需要使用以下幾個註解 transactional 註解用於提交事務,若沒有帶上這句,會報事務異常提示 modifying clearautomatically true 自動清除實體裡儲存的資料 query value update t user set user title ...

SQLAlchemy的查詢操作Query

查詢操作 查詢子句使用session的.query 方法來獲取query查詢物件。查詢物件能夠使用一些方法來對應一些查詢子句,比如.order by limit filter 等。查詢物件有這麼幾種方法.one all scalar one or none get 以及.first 等。下面對這幾個...