標籤:dsa mib u3d mux [] iic mrp important mvpn
0.文法介紹:
1.公用部分代碼同七中一樣
// IndexReader IndexSearcher public IndexSearcher getIndexSearcher() throws Exception { // 第一步:建立一個Directory對象,也就是索引庫存放的位置。 Directory directory = FSDirectory.open(new File("E:\\lucene&solr\\index"));// 磁碟 // 第二步:建立一個indexReader對象,需要指定Directory對象。 IndexReader indexReader = DirectoryReader.open(directory); // 第三步:建立一個indexsearcher對象,需要指定IndexReader對象 return new IndexSearcher(indexReader); } // 執行查詢的結果 public void printResult(IndexSearcher indexSearcher, Query query) throws Exception { // 第五步:執行查詢。 TopDocs topDocs = indexSearcher.search(query, 10); // 第六步:返回查詢結果。遍曆查詢結果並輸出。 ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { int doc = scoreDoc.doc; Document document = indexSearcher.doc(doc); // 檔案名稱 String fileName = document.get("fileName"); System.out.println(fileName); // 檔案內容 String fileContent = document.get("fileContent"); System.out.println(fileContent); // 檔案大小 String fileSize = document.get("fileSize"); System.out.println(fileSize); // 檔案路徑 String filePath = document.get("filePath"); System.out.println(filePath); System.out.println("------------"); } }
2.查詢所有:(分析器會對查詢條件進行分詞)
文法: *:*
// 條件解釋的物件查詢 @Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 參數1: 預設查詢的域 // 參數2:採用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("* : *"); printResult(indexSearcher, query); // 關閉資源 indexSearcher.getIndexReader().close(); }
3.使用預設查詢的域
查詢名字帶有computer索引的文檔
@Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 參數1: 預設查詢的域 // 參數2:採用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("computer"); printResult(indexSearcher, query); // 關閉資源 indexSearcher.getIndexReader().close(); }
結果:
載入擴充詞典:ext.dic載入擴充停止詞典:stopword.diccomputer.txt??Computers are changing our life. You can do a lot of things with a computer. Such as, you can use a computer to write articles, watch video CDs, play games and do office work. But the most important use of a computer is to join the Internet.We don??t need to leave home to borrow books from a library or to do shopping in a supermarke336E:\lucene&solr\searchfiles\computer.txt------------
4.範圍查詢
不支援範圍查詢
5.組合查詢(組合查詢只用修改文法,+表示必須,-表示必須沒有,啥也沒有表示可有可無)
查詢fileName必須帶有Java,且必須不帶struts的文檔。
文法 +fileName:java -fileName:struts
// 條件解釋的物件查詢 @Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 參數1: 預設查詢的域 // 參數2:採用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("+fileName:java -fileName:struts"); printResult(indexSearcher, query); // 關閉資源 indexSearcher.getIndexReader().close(); }
結果:
載入擴充詞典:ext.dic載入擴充停止詞典:stopword.dicjava 基礎.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren??t happy, you can smile, and then you will feel happy. Someone may say, ??But I don??t feel happy.?? Then I would say, ??Please smile as you do when you are happy or play wit309E:\lucene&solr\searchfiles\java 基礎.txt------------java進階.txt????java?????????????????????32E:\lucene&solr\searchfiles\java進階.txt------------
6.複雜查詢
查詢 fileName 帶有javaweb 或者 computer的,或者是fileContent帶有javaweb 或者 computer的
IKAnalyzer分析器先對java is apach進行分詞,然後對javaweb is computer進行分詞,然後根據條件進行查詢帶有分析器分詞後的詞的文檔。
// 條件解釋的物件查詢 @Test public void testQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 參數1: 預設查詢的域 // 參數2:採用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("fileName:javaweb is computer OR fileContent:javaweb is computer "); printResult(indexSearcher, query); // 關閉資源 indexSearcher.getIndexReader().close(); }
結果:
載入擴充詞典:ext.dic載入擴充停止詞典:stopword.diccomputer.txt??Computers are changing our life. You can do a lot of things with a computer. Such as, you can use a computer to write articles, watch video CDs, play games and do office work. But the most important use of a computer is to join the Internet.We don??t need to leave home to borrow books from a library or to do shopping in a supermarke336E:\lucene&solr\searchfiles\computer.txt------------1javaweb .txtthis is javaweb dsbadfsabjkfsdf njdfndsj njaj spring 53E:\lucene&solr\searchfiles\1javaweb .txt------------
--------------------條件解析的物件查詢 多個默念域-----------------------
7.解析器用 MultiFieldQueryParser
查詢 fileName 帶有javaweb 或者 computer的,或者是fileContent帶有javaweb 或者 computer的(與6的作用等價)
作用不大,在查詢條件中一用上查詢域預設查詢域就廢了。
// 條件解析的物件查詢 多個默念域 @Test public void testMultiFieldQueryParser() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); String[] fields = { "fileName", "fileContent" }; // 參數1: 預設查詢的域 // 參數2:採用的分析器 MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("javaweb is computer"); printResult(indexSearcher, query); // 關閉資源 indexSearcher.getIndexReader().close(); }
總結:
這個可以將查詢條件設定為句子,IKAnalyzer分析器會對句子進行分詞處理,然後進行查詢索引。
// 參數1: 預設查詢的域 // 參數2:採用的分析器 QueryParser queryParser = new QueryParser("fileName", new IKAnalyzer()); // *:* 域:值 Query query = queryParser.parse("fileName:java is apache OR fileContent:Computers are changing our life ");
lucene查詢索引之解析查詢——(八)