最近在學習Lucene的過程當中遇到了須要多域搜索並排序的問題,在網上找了找,資料不是不少,如今都列出來,又須要的能夠本身認真看看,都是從其餘網站粘貼過來的,因此比較亂,感謝原創的做者們!
使用MultiFieldQueryParser類便可。
示例代碼: javascript
- package com.lucene.search;
-
- import java.io.File;
- import java.io.IOException; 54com.cn
-
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.queryParser.MultiFieldQueryParser;
- import org.apache.lucene.search.BooleanClause;
- import org.apache.lucene.search.Hits;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
-
- public class Searcher {
-
- feedom.net
- public static void main(String[] args) throws Exception {
- File indexDir = new File("C:\\target\\index\\book");
- if (!indexDir.exists() || !indexDir.isDirectory()) {
- throw new IOException();
- }
- search(indexDir);
- }
-
- public static void search(File indexDir) throws Exception {
-
- Directory fsDir = FSDirectory.getDirectory(indexDir);
- IndexSearcher searcher = new IndexSearcher(fsDir);
-
- String[] queries = { "中文版", "8*" };
- String[] fields = { "name", "isbn" };
- BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
- Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());
-
- Hits hits = searcher.search(query);
- System.out.println("共有" + searcher.maxDoc() + "條索引,命中" + hits.length() + "條");
- for (int i = 0; i < hits.length(); i++) {
- int DocId = hits.id(i);
- String DocName = hits.doc(i).get("name");
- String DocIsbn = hits.doc(i).get("isbn");
- String DocPblDt = hits.doc(i).get("pbl_dt");
- System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);
- }
- }
- }
- package com.lucene.search;
-
- import java.io.File;
- import java.io.IOException;
-
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.queryParser.MultiFieldQueryParser;
- import org.apache.lucene.search.BooleanClause;
- import org.apache.lucene.search.Hits;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
-
- public class Searcher {
-
- public static void main(String[] args) throws Exception {
- File indexDir = new File("C:\\target\\index\\book");
- if (!indexDir.exists() || !indexDir.isDirectory()) {
- throw new IOException();
- }
- search(indexDir);
- }
-
- public static void search(File indexDir) throws Exception {
-
- Directory fsDir = FSDirectory.getDirectory(indexDir);
- IndexSearcher searcher = new IndexSearcher(fsDir);
-
- String[] queries = { "中文版", "8*" };
- String[] fields = { "name", "isbn" };
- BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
- Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());
-
- Hits hits = searcher.search(query);
- System.out.println("共有" + searcher.maxDoc() + "條索引,命中" + hits.length() + "條");
- for (int i = 0; i < hits.length(); i++) {
- int DocId = hits.id(i);
- String DocName = hits.doc(i).get("name");
- String DocIsbn = hits.doc(i).get("isbn");
- String DocPblDt = hits.doc(i).get("pbl_dt");
- System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);
- }
- }
- }
注意:BooleanClause.Occur[]數組,它表示多個條件之間的關係:
BooleanClause.Occur.MUST表示and, feedom.net
BooleanClause.Occur.MUST_NOT表示not, 54com.cn
BooleanClause.Occur.SHOULD表示or.
---------------------------------------------------------------------------------------------------------
多個關鍵字直接的關係是或,因此直接使用多域搜索對象查詢出來的結果就是這樣。
更靈活的控制方式爲:
java
- BooleanQuery booleanQuery = new BooleanQuery();
-
- QueryParser parser = new QueryParser("title",分詞器);
- Query titleQuery = parser .parser("中國人民共和國");
- booleanQuery.add(titleQuery,....SHOULD);
-
- QueryParser parser = new QueryParser("content",分詞器);
- Query contentQuery = parser .parser("中國人民共和國");
- booleanQuery.add(contentQuery ,....SHOULD);
-------------------------------------------------------------------------------------------------- web
- package com.lucene.search;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.queryParser.MultiFieldQueryParser;
- import org.apache.lucene.search.BooleanClause;
- import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.MultiSearcher;
- import org.apache.lucene.search.Query;
- public class Multisearcher {
- private static String INDEX_STORE_PATH1 = "C:\\multi\\1"; private static String INDEX_STORE_PATH2 = "C:\\multi\\2";
- public static void main(String[] args) throws Exception {
- Multisearcher.multisearcher();
- }
- public static void multisearcher() throws Exception {
- IndexWriter writer = new IndexWriter(INDEX_STORE_PATH1, new StandardAnalyzer(), true);
- writer.setUseCompoundFile(false);
- Document doc1 = new Document();
- Field f1 = new Field("bookname", "鋼鐵是怎樣煉成的", Field.Store.YES, Field.Index.TOKENIZED);
- Field f11 = new Field("price", "20.5", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc1.add(f1); doc1.add(f11);
- Document doc2 = new Document();
- Field f2 = new Field("bookname", "鋼鐵戰士", Field.Store.YES, Field.Index.TOKENIZED);
- Field f22 = new Field("price", "18.4", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc2.add(f2);
- doc2.add(f22);
- Document doc3 = new Document();
- Field f3 = new Field("bookname", "鋼和鐵是兩種不一樣的元素", Field.Store.YES, Field.Index.TOKENIZED);
- Field f33 = new Field("price", "7.6", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc3.add(f3);
- doc3.add(f33);
- writer.addDocument(doc1);
- writer.addDocument(doc2);
- writer.addDocument(doc3);
- writer.close();
- IndexWriter writer2 = new IndexWriter(INDEX_STORE_PATH2, new StandardAnalyzer(), true);
- writer2.setUseCompoundFile(false);
- Document doc4 = new Document();
- Field f4 = new Field("bookname", "鋼要比鐵有更多的元素", Field.Store.YES, Field.Index.TOKENIZED);
- Field f44 = new Field("price", "22.5", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc4.add(f4); doc4.add(f44);
- Document doc5 = new Document();
- Field f5 = new Field("bookname", "鋼和鐵是兩種重要的金屬", Field.Store.YES, Field.Index.TOKENIZED);
- Field f55 = new Field("price", "15.9", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc5.add(f5); doc5.add(f55); Document doc6 = new Document();
- Field f6 = new Field("bookname", "鋼鐵是兩種重要的金屬", Field.Store.YES, Field.Index.TOKENIZED);
- Field f66 = new Field("price", "19.00", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc6.add(f6);
- doc6.add(f66);
- writer2.addDocument(doc4);
- writer2.addDocument(doc5);
- writer2.addDocument(doc6);
- writer2.close();
- String query1 = "鋼";
- String query2 = "[10 TO 20]";
- String[] queries = { query1, query2 };
- Field String field1 = "bookname";
- String field2 = "price";
- String[] fields = { field1, field2 };
- BooleanClause.Occur[] clauses = {
- BooleanClause.Occur.MUST, BooleanClause.Occur.MUST
- };
- MultiFieldQuery Query q = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());
- IndexSearcher searcher1 = new IndexSearcher(INDEX_STORE_PATH1);
- IndexSearcher searcher2 = new IndexSearcher(INDEX_STORE_PATH2);
- IndexSearcher[] searchers = { searcher1, searcher2 };
- MultiSearcher searcher = new MultiSearcher(searchers);
- Hits hits = searcher.search(q);
- for (int i = 0; i < hits.length(); i++) {
- System.out.println(hits.doc(i));
- }
- }
- }
------------------------------------------------------------------------------------------------------------------------------------------
默認狀況下,IndexSearcher類的search方法返回查詢結果時,是按文檔的分值排序的,可使用重載的search方法對結果排序
IndexSearcher.search(Query,Sort);
new Sort() 和 Sort.RELEVANCE,以及null同樣,採用默認排序,要定義排序字段,方法是將字段傳入Sort對象 apache
- Sort sort = new Sort(String field);
也能夠對多個字段排序數組
- Sort sort = new Sort(String[] fields);
例:
app
- Sort sort = new Sort(new SortField[]{new SortField(「title」),new SortField(「name」)});
-
- Hits hits=searcher.search(query,Sort);
多字段查找MultiFieldQueryParser
只在某些Term中查找,不關心在哪一個字段
dom
- Query query = new MultiFieldQueryParser.parse(「word」,new String[]{「title」,」content」},analyzer);
//在title和content中找word
多字段時默認是OR關係,要改變它,使用如下方法:
學習
- Query query = MultiFieldQueryParser.parse(「word」,new String[]{「title」,」content」},new int[]{MultiFieldQueryParser.REQUIRED_FIELD,MultiFieldQueryParser.PROHIBITED_FIELD},analyzer);
其中:
REQUIRED_FIELD 表示該條件必須有
PROHIBITED_FIELD 表示必須不含
搜索多個索引文件MultiSearcher
1) 創建多個索引:使用不一樣的索引目錄,實例化不一樣的IndexWriter
2) 創建多索引搜索器:
Searcher[] searchers = new SEARCHER[2];
Searchers[0] = new IndexSearcher(dir1); //搜索索引目錄一
Searchers[1]= new IndexSearcher(dir2);//搜索索引目錄二
Searcher searcher = new MultiSearcher(serarchers);
3) 開始查詢:Hits hits = searcher.search(query);
--------------------------------------------------------------------------------------------------------------------------------------- 網站
- BooleanQuery typeNegativeSearch = new BooleanQuery();
- QueryParser parser = new QueryParser("contents", new Analyzer());
- parser.setDefaultOperator(QueryParser.AND_OPERATOR);
- query = parser.parse(queryString);
- QueryParser parser2 = new QueryParser("adISELL", new Analyzer());
-
-
- query2 = parser2.parse("\"2\"");
- QueryParser parser3 = new QueryParser("adISELL", new Analyzer());
- query3 = parser3.parse("\"2\"");
- QueryParser parser4 = new QueryParser("adISELL", new Analyzer());
- query4 = parser4.parse("\"2\"");
- QueryParser parser4 = new QueryParser("adISELL", new Analyzer());
- query4 = parser4.parse("\"2\"");
- 。。。。
- QueryParser parser..n = new QueryParser("adISELL", new Analyzer());
-
-
- query..n = parser..n.parse("\"2\"");
-
- typeNegativeSearch.add(query,Occur.MUST);
- typeNegativeSearch.add(query2,Occur.MUST);
- typeNegativeSearch.add(query3,Occur.MUST);
- typeNegativeSearch.add(query4,Occur.MUST);
- .....
- typeNegativeSearch.add(query..n,Occur.MUST);
-
- hits = searcher.search(typeNegativeSearch);
1, 幾種span的querySpanTermQuery:檢索效果徹底同TermQuery,但內部會記錄一些位置信息
,供SpanQuery的其它API使用,是其它屬於SpanQuery的Query的基礎。
SpanFirstQuery:查找方式爲從Field的內容起始位置開始,在一個固定的寬度內查找所指定的
詞條。
SpanNearQuery:功能相似PharaseQuery。SpanNearQuery查找所匹配的不必定是短語,還有可
能是另外一個SpanQuery的查詢結果做爲總體考慮,進行嵌套查詢。
SpanOrQuery:把全部SpanQuery查詢結果綜合起來,做爲檢索結果。
SpanNotQuery:從第一個SpanQuery查詢結果中,去掉第二個SpanQuery查詢結果,做爲檢索結
果。
2, 多條件索引關係
BooleanClause用於表示布爾查詢子句關係的類,包括:BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST_NOT,BooleanClause.Occur.SHOULD。有如下6種組合:
1.MUST和MUST:取得連個查詢子句的交集。
2.MUST和MUST_NOT:表示查詢結果中不能包含MUST_NOT所對應得查詢子句的檢索結果。
3.MUST_NOT和MUST_NOT:無心義,檢索無結果。
4.SHOULD與MUST、SHOULD與MUST_NOT:SHOULD與MUST連用時,無心義,結果爲MUST子句的檢索
結果。與MUST_NOT連用時,功能同MUST。
5.SHOULD與SHOULD:表示「或」關係,最終檢索結果爲全部檢索子句的並集。this