SolrJ客户端的使用

来源:互联网 发布:怎么查看当前linux版本 编辑:程序博客网 时间:2024/05/22 08:05

一、环境

1、solr的搭建,参考http://blog.csdn.net/qq_31634461/article/details/78745156
2、所需jar包solr-solrj-4.10.3.jar,导入到项目中

二、添加依赖

<!-- solr客户端 -->        <dependency>            <groupId>org.apache.solr</groupId>            <artifactId>solr-solrj</artifactId>            <version>4.10.1</version>        </dependency>

三、测试

public class SolrJTest {//添加    @Test    public void addDocument() throws Exception {        //创建一连接        SolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr");        //创建一个文档对象        SolrInputDocument document = new SolrInputDocument();        document.addField("id", "10001");        document.addField("title", "测试");        //把文档对象写入索引库        solrServer.add(document);        //提交        solrServer.commit();    }    //删除    @Test    public void deleteDocument() throws Exception {        //创建一连接        SolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr");        //solrServer.deleteById("test001");        solrServer.deleteByQuery("*:*");        solrServer.commit();    }}
原创粉丝点击