- Apache Solr 教程
- Apache Solr - 首頁
- Apache Solr - 概述
- Apache Solr - 搜尋引擎基礎
- Apache Solr - Windows 環境
- Apache Solr - 在 Hadoop 上
- Apache Solr - 架構
- Apache Solr - 術語
- Apache Solr - 基本命令
- Apache Solr - Core
- Apache Solr - 索引資料
- Apache Solr - 新增文件 (XML)
- Apache Solr - 更新資料
- Apache Solr - 刪除文件
- Apache Solr - 檢索資料
- Apache Solr - 查詢資料
- Apache Solr - 分面搜尋
- Apache Solr 有用資源
- Apache Solr - 快速指南
- Apache Solr - 有用資源
- Apache Solr - 討論
Apache Solr - 刪除文件
刪除文件
要從 Apache Solr 的索引中刪除文件,我們需要在 <delete></delete> 標籤之間指定要刪除的文件的 ID。
<delete> <id>003</id> <id>005</id> <id>004</id> <id>002</id> </delete>
此處,此 XML 程式碼用於刪除 ID 為 003 和 005 的文件。將此程式碼儲存在名為 delete.xml 的檔案中。
如果要刪除屬於名為 my_core 的 core 的索引中的文件,則可以使用 post 工具釋出 delete.xml 檔案,如下所示。
[Hadoop@localhost bin]$ ./post -c my_core delete.xml
執行上述命令後,您將獲得以下輸出。
/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core 6.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files org.apache.Solr.util.SimplePostTool delete.xml SimplePostTool version 5.0.0 Posting files to [base] url https://:8983/Solr/my_core/update... Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots, rtf,htm,html,txt,log POSTing file delete.xml (application/xml) to [base] 1 files indexed. COMMITting Solr index changes to https://:8983/Solr/my_core/update... Time spent: 0:00:00.179
驗證
訪問 Apache Solr Web 介面的主頁,並將 core 選擇為 my_core。嘗試透過在文字區域 q 中傳遞查詢“:”來檢索所有文件並執行查詢。執行後,您可以觀察到指定的文件已被刪除。
刪除欄位
有時我們需要根據 ID 以外的欄位刪除文件。例如,我們可能必須刪除城市為 Chennai 的文件。
在這種情況下,您需要在 <query></query> 標籤對中指定欄位的名稱和值。
<delete> <query>city:Chennai</query> </delete>
將其另存為 delete_field.xml,並使用 Solr 的 post 工具對名為 my_core 的 core 執行刪除操作。
[Hadoop@localhost bin]$ ./post -c my_core delete_field.xml
執行上述命令後,它會產生以下輸出。
/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core 6.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files org.apache.Solr.util.SimplePostTool delete_field.xml SimplePostTool version 5.0.0 Posting files to [base] url https://:8983/Solr/my_core/update... Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots, rtf,htm,html,txt,log POSTing file delete_field.xml (application/xml) to [base] 1 files indexed. COMMITting Solr index changes to https://:8983/Solr/my_core/update... Time spent: 0:00:00.084
驗證
訪問 Apache Solr Web 介面的主頁,並將 core 選擇為 my_core。嘗試透過在文字區域 q 中傳遞查詢“:”來檢索所有文件並執行查詢。執行後,您可以觀察到包含指定欄位值對的文件已被刪除。
刪除所有文件
就像刪除特定欄位一樣,如果要從索引中刪除所有文件,只需在 <query></query> 標籤之間傳遞符號“:”即可,如下所示。
<delete> <query>*:*</query> </delete>
將其另存為 delete_all.xml,並使用 Solr 的 post 工具對名為 my_core 的 core 執行刪除操作。
[Hadoop@localhost bin]$ ./post -c my_core delete_all.xml
執行上述命令後,它會產生以下輸出。
/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core 6.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files org.apache.Solr.util.SimplePostTool deleteAll.xml SimplePostTool version 5.0.0 Posting files to [base] url https://:8983/Solr/my_core/update... Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf, htm,html,txt,log POSTing file deleteAll.xml (application/xml) to [base] 1 files indexed. COMMITting Solr index changes to https://:8983/Solr/my_core/update... Time spent: 0:00:00.138
驗證
訪問 Apache Solr Web 介面的主頁,並將 core 選擇為 my_core。嘗試透過在文字區域 q 中傳遞查詢“:”來檢索所有文件並執行查詢。執行後,您可以觀察到包含指定欄位值對的文件已被刪除。
使用 Java (客戶端 API) 刪除所有文件
以下是將文件新增到 Apache Solr 索引的 Java 程式。將此程式碼儲存在名為 UpdatingDocument.java 的檔案中。
import java.io.IOException;
import org.apache.Solr.client.Solrj.SolrClient;
import org.apache.Solr.client.Solrj.SolrServerException;
import org.apache.Solr.client.Solrj.impl.HttpSolrClient;
import org.apache.Solr.common.SolrInputDocument;
public class DeletingAllDocuments {
public static void main(String args[]) throws SolrServerException, IOException {
//Preparing the Solr client
String urlString = "https://:8983/Solr/my_core";
SolrClient Solr = new HttpSolrClient.Builder(urlString).build();
//Preparing the Solr document
SolrInputDocument doc = new SolrInputDocument();
//Deleting the documents from Solr
Solr.deleteByQuery("*");
//Saving the document
Solr.commit();
System.out.println("Documents deleted");
}
}
透過在終端中執行以下命令來編譯上述程式碼:
[Hadoop@localhost bin]$ javac DeletingAllDocuments [Hadoop@localhost bin]$ java DeletingAllDocuments
執行上述命令後,您將獲得以下輸出。
Documents deleted