solr环境的搭建以及基本的使用方法

134 篇文章 1 订阅
55 篇文章 0 订阅

1. Solr服务搭建

Solr是java开发。需要安装jdk。安装环境Linux。需要安装Tomcat
第一步:把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文件。

指明solrhome的位置
第九步:启动Tomcat
http://192.168.25.154:8080/solr/
和windows下的配置完全一样。

以商城为例进行配置业务域

schema.xml中定义
1、商品Id
2、商品标题
3、商品卖点
4、商品价格
5、商品图片
6、分类名称

创建对应的业务域。需要制定中文分析器。
创建步骤:
第一步:把中文分析器添加到工程中。
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_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"/>

第四步:重启tomcat
输入localhost:xxxx/solr就可以访问到

代码测试

第六步:把/root/solr-4.10.3/example/lib/ext目录下的所有的jar包,添加到solr工程中。(不可忘记)
测试一

@Test
    public void queryIndex() throws Exception {
        // 创建一个SolrServer对象
        SolrServer solrServer = new HttpSolrServer("http://localhost:8009/solr/collection1");
        // 创建SolrQuery对象
        SolrQuery query = new SolrQuery();
        // 设置查询条件
        // query.setQuery("*:*");
        query.set("q", "*:*");
        // 执行查询,QueryResponse对象
        QueryResponse queryResponse = solrServer.query(query);
        // 取文档列表,取查询结果的总记录数
        SolrDocumentList results = queryResponse.getResults();
        System.out.println("查询结果的总记录数目" + results.getNumFound());
        // 遍历文档列表,从文档中取域的值
        for (SolrDocument solrDocument : results) {
            System.out.println(solrDocument.get("id"));
            System.out.println(solrDocument.get("item_title"));
            System.out.println(solrDocument.get("item_sell_point"));
            System.out.println(solrDocument.get("item_price"));
            System.out.println(solrDocument.get("item_image"));
            System.out.println(solrDocument.get("itme_category_name"));
        }
    }

测试二

@Test
    public void queryIndexFuza() throws Exception {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8009/solr/collection1");
        SolrQuery solrQuery = new SolrQuery();
        // 设置查询条件
        solrQuery.setQuery("手机");
        solrQuery.setStart(0);
        solrQuery.setRows(20);
        solrQuery.set("df", "item_title");
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("item_title");
        solrQuery.setHighlightSimplePre("<em>");
        solrQuery.setHighlightSimplePost("</em>");
        QueryResponse queryResponse = solrServer.query(solrQuery);
        // 取文档列表,取查询结果的总记录数
        SolrDocumentList results = queryResponse.getResults();
        System.out.println("获取的结构数目" + results.getNumFound());
        for (SolrDocument solrDocument : results) {

            // 取高亮显示
            Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
            List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
            String title="";
            if (list != null && list.size() > 0) {
                title=list.get(0);
            }
            else{
                title=(String) solrDocument.get("item_title");
            }
            System.out.println("title为"+title);
            System.out.println(solrDocument.get("id"));
            System.out.println(solrDocument.get("item_title"));
            System.out.println(solrDocument.get("item_sell_point"));
            System.out.println(solrDocument.get("item_price"));
            System.out.println(solrDocument.get("item_image"));
            System.out.println(solrDocument.get("itme_category_name"));
        }
    }

至此搭建solr的任务全部完成,如果能帮助你还请给予关注,后期继续写关于redis,zookeper,dubbo内容.如有疑问可以评论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uniquewdl

匆忙的人生,总有你喜欢的文章

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值