lucene:索引維護(刪除、更新、查詢)
阿新 • • 發佈:2020-07-13
1、刪除全部索引
(1)刪除索引前執行查詢:
1.txt null null E:\test\1.txt 1.txt null null E:\test\1.txt
(2)執行刪除索引的程式碼:
@Test public void test() throws Exception { //指定一個分析器,對文件內容進行分析。 Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer);//指定索引庫的存放位置Directory物件,儲存索引到記憶體中 (記憶體索引庫) Directory directory = FSDirectory.open(new File("E:\\test1").toPath()); //建立一個indexwriter物件 IndexWriter indexWriter = new IndexWriter(directory, config); indexWriter.deleteAll(); indexWriter.close(); }
(3)再次執行查詢索引的程式碼,沒有查詢結果
2、根據條件刪除
(1)建立並查詢索引:
java a.txt null null E:\test\java a.txt java b.txt null null E:\test\java b.txt
(2)執行根據條件刪除索引的程式:
@Test public void test() throws Exception { //指定一個分析器,對文件內容進行分析。 Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer);//指定索引庫的存放位置Directory物件,儲存索引到記憶體中 (記憶體索引庫) Directory directory = FSDirectory.open(new File("E:\\test1").toPath()); //建立一個indexwriter物件 IndexWriter indexWriter = new IndexWriter(directory, config); Query query = new TermQuery(new Term("fileName","java")); indexWriter.deleteDocuments(query); indexWriter.close(); }
(3)再次查詢索引,無查詢結果
3、更新索引
@Test public void testUpdate() throws Exception { Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(analyzer); Directory directory = FSDirectory.open(new File("E:\\test1").toPath()); IndexWriter indexWriter = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new TextField("test_update_name", "test_name.txt", Field.Store.YES)); indexWriter.updateDocument(new Term("fileName","java"),doc); indexWriter.close(); }
先將檔名為java的索引刪除,再將名為test_name.txt的索引新增進去。
4、查詢所有
@Test public void testSelect() throws IOException { Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁碟 IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Query query = new MatchAllDocsQuery(); System.out.println(query); 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); } indexSearcher.getIndexReader().close(); }
查詢結果:
*:* 001.txt null null E:\test\001.txt 1.txt null null E:\test\1.txt 2.txt null null E:\test\2.txt 3.txt null null E:\test\3.txt abc.txt null null E:\test\abc.txt java.txt null null E:\test\java.txt js.txt null null E:\test\js.txt 2.txt null null E:\test\2.txt 3.txt null null E:\test\3.txt abc.txt null null E:\test\abc.txt
5、根據數值範圍查詢
@Test public void testSelect() throws IOException { Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁碟 IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); //false和true表示不包含47但是包含200 Query query = NumericRangeQuery.newLongRange("fileSize", 47L, 200L, false, true); System.out.println(query); 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); } indexSearcher.getIndexReader().close(); }
是根據fileSize域來查詢的
6、組合查詢
@Test public void testSelect() throws Exception { Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁碟 IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); Query query1 = new TermQuery(new Term("fileName","java")); Query query2 = new TermQuery(new Term("fileName","a")); // select * from user where id =1 or name = 'safdsa' booleanQuery.add(query1, BooleanClause.Occur.MUST); booleanQuery.add(query2, BooleanClause.Occur.SHOULD); TopDocs topDocs = indexSearcher.search(booleanQuery.build(), 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); } indexSearcher.getIndexReader().close(); }
測試結果:
java a.txt null null E:\test\java a.txt java b.txt null null E:\test\java b.txt
- MUST 必須 相當於and, 並且
- MUST_NOT 必須不滿足,相當於not, 非
- SHOULD 應該 相當於or,或者
- 可以類比於sq語句的多條件查詢
7、解析的方式查詢
(1)*:* 查詢所有:表示域和值都不新增限制條件,但是添加了預設的filename:
@Test public void testSelect() throws Exception { Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁碟 IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); QueryParser queryParser=new QueryParser("fileName",new IKAnalyzer()); Query query=queryParser.parse("*:*"); 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); } indexSearcher.getIndexReader().close(); }
001.txt null null E:\test\001.txt 1.txt null null E:\test\1.txt 2.txt null null E:\test\2.txt 3.txt null null E:\test\3.txt abc.txt null null E:\test\abc.txt java.txt null null E:\test\java.txt js.txt null null E:\test\js.txt 2.txt null null E:\test\2.txt 3.txt null null E:\test\3.txt abc.txt null null E:\test\abc.txt
(2)新增條件,不執行預設的條件:
Directory directory = FSDirectory.open(new File("E:\\test1").toPath());// 磁碟 IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); QueryParser queryParser=new QueryParser("fileName",new StandardAnalyzer()); Query query=queryParser.parse("fileName:java");
此時預設的條件已經不起作用了,執行的是自定義的條件
(3)多域預設查詢
String[] fields = {"fileName","fileContent"}; MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields,new IKAnalyzer());
- 引數1: 預設查詢的域
- 引數2:採用的分析器