Solr入门介绍

solr入门案例web

 solr是apache下的一个全文检索引擎系统. 咱们须要在服务器上单独去部署solr, 经过它的客户端工具包solrJ, 就是一个
    jar包, 集成到咱们项目中来调用服务器中的solr.
    solr底层使用lucene开发apache

部署步骤:
    1. 准备一个干净的Tomcat, 没有任何项目均可以运行的.
    2. 将solr/example/webapps/solr.war复制到Tomcat/webapps/目录下
    3. 运行Tomcat,运行日志会报错, 没关系, 目的是对solr.war解压
    4. 关闭Tomcat并删除Tomcat/wabapps下的solr.war
    5. 将solr/example/lib/ext下的全部jar包复制到tomcat/webapps/solr/WEB-INF/lib文件夹下
    6. 复制solr/example/solr文件夹到硬盘根目录并更名为solrhome(solrhome: 就是solr的家, 一个solr服务器只能有一个solrhome, 一个solrhome中能够都多个solrcore, 一个solrcore就是一个
    solr实例.实例和实例之间是互相隔离的.)
    7. 将solrhome的位置复制到Tomcat/webapps/solr/WEB-INf/web.xml中
    8. 启动Tomcat, 访问http://localhost:8080/solr看到solr页面后说明部署成功tomcat

 

开发入门程序:服务器

  依赖包:Solr服务的依赖包\solr\example\lib\ext ;solrj依赖包,\solr-4.10.3\dist\solrj-lib;junitapp

一、 建立HttpSolrServer对象,经过它和Solr服务器创建链接。webapp

二、 建立SolrInputDocument对象,而后经过它来添加域。工具

三、 经过HttpSolrServer对象将SolrInputDocument添加到索引库。spa

四、 提交。日志

代码实现:xml

    public class TestIndexManager {
    @Test
    public void testIndexCreat throws Exception, IOException {
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
        // 建立文档对象
        SolrInputDocument solrDocument = new SolrInputDocument();
        solrDocument.addField("id", "001");
        solrDocument.addField("title", "xxx");
        solrServer.add(solrDocument);
        solrServer.commit();
      }
    }

 

  修改:

    @Test
    public void testUpdate() throws Exception, IOException{
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr");
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        
        solrInputDocument.addField("id", "001");
        solrInputDocument.addField("title", "yyy");
        httpSolrServer.add(solrInputDocument);
        httpSolrServer.commit();
    }

  删除:

    @Test
    public void testDelect() throws Exception, IOException{
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
        solrServer.deleteByQuery("id:001");
        solrServer.commit();
    }

  查询:

    @Test    public void testSelect() throws Exception{        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr");                 SolrQuery solrQuery = new SolrQuery();        solrQuery.setQuery("*:*");        QueryResponse query = httpSolrServer.query(solrQuery);        SolrDocumentList results = query.getResults();        for (SolrDocument solrDocument : results) {            System.out.println(solrDocument.get("id"));            System.out.println(solrDocument.get("title"));        }            }

相关文章
相关标签/搜索