solr的使用

来源:互联网 发布:黑暗之魂3男号捏脸数据 编辑:程序博客网 时间:2024/06/12 01:26

solr实现索引的增加,删除,更新操作

@Test    public void addDocumentTest() throws Exception{        //创建一个SOlrServer对象(抽象类),使用其实现类HttpSolrServer        //指定solr服务的地址        String url = "http://192.168.254.128:8080/solr/collection1";        SolrServer solrServer = new HttpSolrServer(url);        //创建一个文档对象SolrInputDucument        SolrInputDocument document = new SolrInputDocument();        //添加字段        document.addField("id", "121");        document.addField("item_title", "电脑");        document.addField("item_price", 1000);        //把文档对象写入索引库        solrServer.add(document);        //提交        solrServer.commit();    }    @Test    public void deleteById() throws Exception {        String url = "http://192.168.254.128:8080/solr/collection1";        SolrServer solrServer = new HttpSolrServer(url);        solrServer.deleteById("test01");        solrServer.commit();    }    @Test    public void deleteByQuery() throws Exception {        String url = "http://192.168.254.128:8080/solr/collection1";        SolrServer solrServer = new HttpSolrServer(url);        solrServer.deleteByQuery("item_title:手机");        solrServer.commit();    }
原创粉丝点击