作者:董鹏飞80 | 来源:互联网 | 2023-07-24 17:08
目录一、写入索引文档二、删除索引文档一、写入索引文档***添加商品索引文档*OverridepublicvoidaddDocument()throwsException{查询出所
目录
一、写入索引文档
二、删除索引文档
一、写入索引文档
/*** 添加商品索引文档*/@Overridepublic void addDocument() throws Exception{// 查询出所有所需的商品信息List list = tbItemSolrMapper.selectTbItemSolrResultList();//获得SolrServer对象SolrServer httpSolrServer = new HttpSolrServer("http://localhost:8088/solr/");//遍历出list,将其加入document的索引域中for (TbItemSolrResult result : list) {//创建sorl的输入索引文档SolrInputDocument document = new SolrInputDocument();document.addField("id",result.getId());document.addField("item_title", result.getTitle());document.addField("item_sell_point", result.getSell_point());document.addField("item_price", result.getPrice());document.addField("item_image", result.getImage());document.addField("item_category_name", result.getCategory_name());//提交每个documenthttpSolrServer.add(document);//提交索引文档httpSolrServer.commit();}}
注意:每个document都必须有一个id域,这个是默认需要添加的,用以标识document
二、删除索引文档
@Testpublic void deleteDocument() throws SolrServerException, IOException {// 测试删除document//获得solrserver对象SolrServer httpSolrServer = new HttpSolrServer("http://localhost:8088/solr/");//根据document的id删除索引文档httpSolrServer.deleteById("doc01");//提交httpSolrServer.commit();}