sorl维护索引库sorl4j的使用

1.维护索引库

添加:添加一个json格式的文件就可以。

修改:在solr中没有update,只需要添加一个新的文档,要求文档id和被修改文档的id一致。原理是先删除后添加。

删除:使用xml格式。

删除两种方法:

1、根据id删除:

<delete>

<id>test001</id>

</delete>

<commit/>

2、根据查询删除:

<delete>

<query>*:*</query>

</delete>

<commit/>

2.使用solrj

 

public class SolrJTest {

    @Test
    public void addDocument() throws Exception {
        //创建一连接
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
        //创建一个文档对象
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "test001");
        document.addField("item_title", "测试商品2");
        document.addField("item_price", 54321);
        //把文档对象写入索引库
        solrServer.add(document);
        //提交
        solrServer.commit();
    }
    
    @Test
    public void deleteDocument() throws Exception {
        //创建一连接
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
        //solrServer.deleteById("test001");
        solrServer.deleteByQuery("*:*");
        solrServer.commit();
    }
}

 

3.把数据库信息导入到索引库

使用java程序读取mysql数据库中的信息,然后创建solr文档对象,把商品信息写入索引库。

 

posted @ 2016-10-27 11:27  atzuge  阅读(313)  评论(0编辑  收藏  举报