Solr刪除文檔數據

刪除文檔

要從Apache Solr的索引中刪除文檔,我們需要在<delete> </ delete>標記之間指定要刪除的文檔的ID

<delete>   
   <id>003</id>   
   <id>005</id> 
</delete>

這裏,此XML代碼用於刪除ID003005的文檔。將此代碼保存在名稱爲delete.xml的文件中。

如果要從屬於名稱爲my_core的核心的索引中刪除文檔,則可以使用post工具發佈delete.xml文件,如下所示。

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete.xml

執行上述命令後,將得到以下輸出 -

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core delete.xml
/usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.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 http://localhost: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 http://localhost:8983/solr/my_core/update...
Time spent: 0:00:00.124

驗證執行結果

訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試通過在文本區域q中傳遞查詢「」來檢索所有文檔,並執行查詢。 執行時可以觀察到指定的文檔(ID003005)已刪除。

Solr刪除文檔數據

刪除字段

有時,需要基於除ID以外的字段來刪除文檔。例如,可能需要刪除城市是Chennai的文檔。

在這種情況下,需要在<query> </ query>標記對中指定字段的名稱和值。

<delete> 
   <query>city:Chennai</query> 
</delete>

將上面代碼保存到delete_field.xml文件中,並使用Solr的post工具在覈心my_core上執行刪除操作。

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete_field.xml

執行上述命令後,將產生以下輸出。

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core delete_field.xml
/usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.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 http://localhost: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 http://localhost:8983/solr/my_core/update...
Time spent: 0:00:00.225

驗證執行結果

訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試通過在文本區域q中傳遞查詢「」來檢索所有文檔,並執行查詢。 執行時可以觀察到包含指定字段值對的文檔被刪除。
Solr刪除文檔數據

刪除所有文檔

類似刪除一個指定刪除某個字段一樣,如果想刪除索引中的所有文檔,只需要在標籤<query> </ query>之間傳遞符號「」,如下所示。

<delete> 
   <query>*:*</query> 
</delete>

將上面代碼保存到delete_all.xml文件中,並使用Solr的post工具對核心my_core執行刪除操作。

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete_all.xml

執行上述命令後,將產生以下輸出。

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core delete_all.xml
/usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.0.jar -Dauto=yes -Dc=my_core -Ddata=files org.apache.solr.util.SimplePostTool delete_all.xml
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost: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_all.xml (application/xml) to [base]
1 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/my_core/update...
Time spent: 0:00:00.114

驗證執行結果

訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試通過在文本區域q中傳遞查詢「」來檢索所有文檔,並執行查詢。執行時您可以觀察到包含指定字段值對的文檔全被刪除了。

Solr刪除文檔數據

使用Java(客戶端API)刪除所有文檔

以下是使用Java程序向Apache Solr索引刪除文檔。將此代碼保存在名稱爲DeletingAllDocuments.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 = "http://localhost: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"); 
   } 
}

通過在終端中執行以下命令編譯上述代碼 -

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ javac DeletingAllDocuments.java
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ java DeletingAllDocuments

執行上述命令後,將得到以下輸出。

Documents deleted