zoukankan      html  css  js  c++  java
  • Solr入门介绍

    solr入门案例

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

    部署步骤:
        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页面后说明部署成功

     

    开发入门程序:

      依赖包:Solr服务的依赖包solrexamplelibext ;solrj依赖包,solr-4.10.3distsolrj-lib;junit

    1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。

    2、 创建SolrInputDocument对象,然后通过它来添加域。

    3、 通过HttpSolrServer对象将SolrInputDocument添加到索引库。

    4、 提交。

    代码实现:

        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"));
            }
            
        }

  • 相关阅读:
    [HAOI2015]按位或——Min-Max容斥+FWT
    HDU 4773 Problem of Apollonius——圆反演
    类欧几里得小结
    线性规划(单纯形法)知识整理
    奇怪的数学题(51nod1847)——min_25筛+杜教筛+第二类斯特林数
    CSP-S 2019 游记
    2019.11.11~2019.11.12考试总结
    2019.11.6~2019.11.7考试总结
    1019.11.1~2019.11.2考试总结
    Final-阶段站立会议4
  • 原文地址:https://www.cnblogs.com/sjzxs/p/9519524.html
Copyright © 2011-2022 走看看