📅  最后修改于: 2023-12-03 15:20:11.102000             🧑  作者: Mango
当我们需要清空 Solr 中的所有文档时,可以使用 SolrJ
来实现该功能。在以下的代码示例中,我们将使用 SolrJ
中的 SolrClient
进行操作。
SolrClient
对象:SolrClient solrClient = new HttpSolrClient.Builder("http://localhost:8983/solr").build();
SolrClient
对象来构建 SolrQuery
,并将其设置为 :(即匹配所有文档):SolrQuery query = new SolrQuery();
query.setQuery("*:*");
SolrClient
对象来构建 DeleteByQueryRequest
对象,并将其设置为 query
(即匹配所有文档):DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest(query);
UpdateResponse response = deleteRequest.process(solrClient);
SolrClient
:solrClient.commit();
solrClient.close();
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.DeleteByQueryRequest;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.common.util.SuppressForbidden;
import org.apache.solr.common.util.Utils;
import org.apache.solr.search.SolrReturnFields;
import org.apache.solr.search.facet.FacetComponent;
import org.apache.solr.search.facet.FacetField;
import org.apache.solr.servlet.HttpSolrCall;
import org.apache.solr.servlet.SolrCall;
import org.apache.solr.servlet.Utils;
public class SolrDeleteAllDocs {
public static void main(String[] args) throws Exception {
SolrClient solrClient = new HttpSolrClient.Builder("http://localhost:8983/solr").build();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest(query);
UpdateResponse response = deleteRequest.process(solrClient);
solrClient.commit();
solrClient.close();
}
}