搜索引擎solr的简单使用步骤

Solr服务搭建

1.Solr的环境:
    需要安装jdk。
    安装环境Linux。
    需要安装Tomcat。
2.搭建步骤
    第一步:把solr 的压缩包上传到Linux系统
    第二步:解压solr。
    第三步:安装Tomcat,解压缩即可。
    第四步:把solr部署到Tomcat下。
    第五步:解压缩war包。启动Tomcat解压。
    第六步:把/root/solr-4.10.3/example/lib/ext目录下的所有的jar包,添加到solr工程中。
        [root@localhost ext]# pwd
        /root/solr-4.10.3/example/lib/ext
        [root@localhost ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
    第七步:创建一个solrhome。/example/solr目录就是一个solrhome。复制此目录到/usr/local/solr/solrhome
        [root@localhost example]# pwd
        /root/solr-4.10.3/example
        [root@localhost example]# cp -r solr /usr/local/solr/solrhome
        [root@localhost example]#
    第八步:关联solr及solrhome。需要修改solr工程的web.xml文件。
    第九步:启动Tomcat
    http://192.168.25.154:8080/solr/
3.Solr的使用
    添加文档必须有id域,其他域 必须在solr的schema.xml中定义。
4.配置业务域
    
    1>schema.xml中定义
    2>创建对应的业务域。需要制定中文分析器。
     
    创建步骤:
        第一步:把中文分析器添加到工程中。
            1、把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目录下
            2、把扩展词典、配置文件放到solr工程的WEB-INF/classes目录下。
        第二步:配置一个FieldType,制定使用IKAnalyzer
            修改schema.xml文件
            修改Solr的schema.xml文件,添加FieldType:
            <fieldType name="text_ik" class="solr.TextField">
              <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
            </fieldType>
        第三步:配置业务域,type制定使用自定义的FieldType。
            设置业务系统Field
            <field name="item_title" type="text_ik" indexed="true" stored="true"/>
            <field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
            <field name="item_price"  type="long" indexed="true" stored="true"/>
            <field name="item_image" type="string" indexed="false" stored="true" />
            <field name="item_category_name" type="string" indexed="true" stored="true" />
            <field name="item_desc" type="text_ik" indexed="true" stored="false" />
    
            <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
            <copyField source="item_title" dest="item_keywords"/>
            <copyField source="item_sell_point" dest="item_keywords"/>
            <copyField source="item_category_name" dest="item_keywords"/>
            <copyField source="item_desc" dest="item_keywords"/>

        第四步:重启tomcat
5.使用SolrJ可以实现索引库的增删改查操作。
     1>添加文档
        第一步:把solrJ的jar包添加到工程中。
        第二步:创建一个SolrServer,使用HttpSolrServer创建对象。
        第三步:创建一个文档对象SolrInputDocument对象。
        第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。
        第五步:把文档添加到索引库中。
        第六步:提交。

例如:
    public void addDocument() throws Exception {
        // 第一步:把solrJ的jar包添加到工程中。
        // 第二步:创建一个SolrServer,使用HttpSolrServer创建对象。
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
        // 第三步:创建一个文档对象SolrInputDocument对象。
        SolrInputDocument document = new SolrInputDocument();
        // 第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。
        document.addField("id", "test001");
        document.addField("item_title", "测试商品");
        document.addField("item_price", "199");
        // 第五步:把文档添加到索引库中。
        solrServer.add(document);
        // 第六步:提交。
        solrServer.commit();
    }
    2>删除文档
        1:根据id删除
            第一步:创建一个SolrServer对象。
            第二步:调用SolrServer对象的根据id删除的方法。
            第三步:提交。
            public void deleteDocumentById() throws Exception {
            // 第一步:创建一个SolrServer对象。
                SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
                // 第二步:调用SolrServer对象的根据id删除的方法。
                solrServer.deleteById("1");
                // 第三步:提交。
                solrServer.commit();
        }

       2: 根据查询删除
        public void deleteDocumentByQuery() throws Exception {
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
            solrServer.deleteByQuery("title:change.me");
            solrServer.commit();
        }
     3>查询索引库
    查询步骤:
    第一步:创建一个SolrServer对象
    第二步:创建一个SolrQuery对象。
    第三步:向SolrQuery中添加查询条件、过滤条件。。。
    第四步:执行查询。得到一个Response对象。
    第五步:取查询结果。
    第六步:遍历结果并打印。
    public void queryDocument() throws Exception {
        // 第一步:创建一个SolrServer对象
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
        // 第二步:创建一个SolrQuery对象。
        SolrQuery query = new SolrQuery();
        // 第三步:向SolrQuery中添加查询条件、过滤条件。。。
        query.setQuery("*:*");
        // 第四步:执行查询。得到一个Response对象。
        QueryResponse response = solrServer.query(query);
        // 第五步:取查询结果。
        SolrDocumentList solrDocumentList = response.getResults();
        System.out.println("查询结果的总记录数:" + solrDocumentList.getNumFound());
        // 第六步:遍历结果并打印。
        for (SolrDocument solrDocument : solrDocumentList) {
            System.out.println(solrDocument.get("id"));
            System.out.println(solrDocument.get("item_title"));
            System.out.println(solrDocument.get("item_price"));
        }
    }
    4>带高亮显示
    public void queryDocumentWithHighLighting() throws Exception {
        // 第一步:创建一个SolrServer对象
        SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
        // 第二步:创建一个SolrQuery对象。
        SolrQuery query = new SolrQuery();
        // 第三步:向SolrQuery中添加查询条件、过滤条件。。。
        query.setQuery("测试");
        //指定默认搜索域
        query.set("df", "item_keywords");
        //开启高亮显示
        query.setHighlight(true);
        //高亮显示的域
        query.addHighlightField("item_title");
        query.setHighlightSimplePre("<em>");
        query.setHighlightSimplePost("</em>");
        // 第四步:执行查询。得到一个Response对象。
        QueryResponse response = solrServer.query(query);
        // 第五步:取查询结果。
        SolrDocumentList solrDocumentList = response.getResults();
        System.out.println("查询结果的总记录数:" + solrDocumentList.getNumFound());
        // 第六步:遍历结果并打印。
        for (SolrDocument solrDocument : solrDocumentList) {
            System.out.println(solrDocument.get("id"));
            //取高亮显示
            Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
            List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
            String itemTitle = null;
            if (list != null && list.size() > 0) {
                itemTitle = list.get(0);
            } else {
            itemTitle = (String) solrDocument.get("item_title");
            }
            System.out.println(itemTitle);
            System.out.println(solrDocument.get("item_price"));
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值