Apache Solr 简明教程
Apache Solr - Deleting Documents
Deleting the Document
要从 Apache Solr 索引中删除文档,我们需要指定要在 <delete></delete> 标签之间删除的文档的 ID。
To delete documents from the index of Apache Solr, we need to specify the ID’s of the documents to be deleted between the <delete></delete> tags.
<delete>
<id>003</id>
<id>005</id>
<id>004</id>
<id>002</id>
</delete>
这里,此 XML 代码用来删除 ID 为 003 和 005 的文档。将此代码另存为文件,文件名 delete.xml 。
Here, this XML code is used to delete the documents with ID’s 003 and 005. Save this code in a file with the name delete.xml.
如果你想从属于名为 my_core 的内核的索引中删除文档,那么你可以使用 post 工具发布 delete.xml 文件,如下所示。
If you want to delete the documents from the index which belongs to the core named my_core, then you can post the delete.xml file using the post tool, as shown below.
[Hadoop@localhost bin]$ ./post -c my_core delete.xml
执行以上命令,您将获得以下输出。
On executing the above command, you will get the following output.
/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 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.179
Verification
访问 Apache Solr Web 界面主页并将内核选为 my_core 。尝试通过在文本区域 q 中传递查询“:”来检索所有文档并执行查询。在执行时,你可以观察到已删除指定的文档。
Visit the homepage of the of Apache Solr web interface and select the core as my_core. Try to retrieve all the documents by passing the query “:” in the text area q and execute the query. On executing, you can observe that the specified documents are deleted.

Deleting a Field
有时我们需要根据非 ID 字段来删除文档。例如,我们可能需要删除城市是 Chennai 的文档。
Sometimes we need to delete documents based on fields other than ID. For example, we may have to delete the documents where the city is Chennai.
在这种情况下,你需要在 <query></query> 标记对内指定字段的名称和值。
In such cases, you need to specify the name and value of the field within the <query></query> tag pair.
<delete>
<query>city:Chennai</query>
</delete>
将其另存为 delete_field.xml ,并使用 Solr 的 post 工具对名为 my_core 的内核执行删除操作。
Save it as delete_field.xml and perform the delete operation on the core named my_core using the post tool of Solr.
[Hadoop@localhost bin]$ ./post -c my_core delete_field.xml
在执行上述命令后,会生成以下输出。
On executing the above command, it produces the following output.
/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 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.084
Verification
访问 Apache Solr Web 界面主页并将内核选为 my_core 。尝试通过在文本区域 q 中传递查询“:”来检索所有文档并执行查询。在执行时,你可以观察到包含指定字段值对的文档已删除。
Visit the homepage of the of Apache Solr web interface and select the core as my_core. Try to retrieve all the documents by passing the query “:” in the text area q and execute the query. On executing, you can observe that the documents containing the specified field value pair are deleted.

Deleting All Documents
就像删除特定字段一样,如果你想从某个索引中删除所有文档,你只需要在 <query></query> 标记之间传递符号“:”,如下所示。
Just like deleting a specific field, if you want to delete all the documents from an index, you just need to pass the symbol “:” between the tags <query></ query>, as shown below.
<delete>
<query>*:*</query>
</delete>
将该文件另存为 delete_all.xml ,然后使用 Solr 的 post 工具针对名为 my_core 的核心执行删除操作。
Save it as delete_all.xml and perform the delete operation on the core named my_core using the post tool of Solr.
[Hadoop@localhost bin]$ ./post -c my_core delete_all.xml
在执行上述命令后,会生成以下输出。
On executing the above command, it produces the following output.
/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 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 deleteAll.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.138
Verification
访问 Apache Solr Web 界面主页,然后选择核心为 my_core 。尝试在文本区域 q 中传递查询“:”来检索所有文档,然后执行该查询。在执行时,你可以观察到包含指定的字段值对的文档已被删除。
Visit the homepage of Apache Solr web interface and select the core as my_core. Try to retrieve all the documents by passing the query “:” in the text area q and execute the query. On executing, you can observe that the documents containing the specified field value pair are deleted.

Deleting all the documents using Java (Client API)
以下是将文档添加到 Apache Solr 索引的 Java 程序。将此代码另存为文件,文件名 UpdatingDocument.java 。
Following is the Java program to add documents to Apache Solr index. Save this code in a file with the name 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 = "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");
}
}
通过在终端中执行以下命令编译上述代码 -
Compile the above code by executing the following commands in the terminal −
[Hadoop@localhost bin]$ javac DeletingAllDocuments
[Hadoop@localhost bin]$ java DeletingAllDocuments
执行以上命令,您将获得以下输出。
On executing the above command, you will get the following output.
Documents deleted