solr框架中索引的创建和删除

创建索引

使用SolrJ创建索引,通过调用SolrJ提供的API请求Solr服务,Document通过 SolrInputDocument进行构建。
// 创建索引
public void testCreateIndex() throws SolrServerException, IOException {
SolrServer solrServer = new HttpSolrServer(urlString);
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "c0001"); //商品id
document.addField("product_name", "solr全文检索");//商品名称
document.addField("product_price", 86.5f);//商品价格
document.addField("product_picture", "382782828.jpg");//商品图片
document.addField("product_description", "这是一本关于solr的书籍!");//商品描述
document.addField("product_catalog_name", "javabook");//商品分类
UpdateResponse response = solrServer.add(document); // 提交 solrServer.commit(); }
说明:根据id(唯一约束)域来更新Document的内容,如果根据id值搜索不 到id域则会执行添加操作,如果找到则更新。

删除索引

public void testDeleteIndex() throws SolrServerException, IOException {
SolrServer solrServer = new HttpSolrServer(urlString);
//根据id删除
UpdateResponse response = solrServer.deleteById("c0001");
//根据多个id删除
// solrServer.deleteById(ids);
//自动查询条件删除
// solrServer.deleteByQuery("product_keywords:教程");
// 提交
solrServer.commit();
}
说明:deleteById(String id)根据id删除索引,此方法为重载方法,也可以传个多个id批量删除,也可 以调用deleteByQuery() 根据查询条件删除

猜你喜欢

转载自blog.csdn.net/qq_40792869/article/details/88759386