【SSM分布式架构电商项目-24】Solr实现商品的搜索

导入数据

数据来源,京东:
这里写图片描述
数据:
这里写图片描述

修改表结构

这里写图片描述

导入图片数据

测试:
这里写图片描述

Solr的安装

下载地址: http://www.apache.org/dyn/closer.cgi/lucene/solr/

下载得到zip压缩包,下载的版本为4.10.2. 文件大小148MB左右。
这里写图片描述

1、 将solr-4.10.2.zip文件拷贝到C盘;(或者其他盘都可以,只要目录中不要出现中文就行。)
2、 解压solr-4.10.2.zip文件,得到solr-4.10.2目录。
3、 运行-> cmd 执行命(进入D:\solr\solr-4.10.2\example):
这里写图片描述
4、 执行命令:java -jar start.jar
这里写图片描述
5、 打开浏览器,输入地址:http://localhost:8983/solr/
这里写图片描述
至此,Solr已经安装成功。

插入数据

solr安装完成后是没有数据的,需要导入一些数据进去方便我们学习。

运行->cmd -> 输入命令:cd D:\solr\solr-4.10.2\example\exampledocs

再输入命令:java -jar post.jar solr.xml monitor.xml
这里写图片描述

这里写图片描述

已经查询到数据了。

通过域名访问solr服务

通过solr.taotao.com访问。
这里写图片描述
配置nginx:
这里写图片描述
测试:
这里写图片描述
发现,请求域名后面依然得有/solr.

默认启动的web应用服务器是jetty。

修改配置文件:

这里写图片描述

这里写图片描述
测试:
这里写图片描述

Solrj

这里写图片描述

创建taotao core

1、 在example目录下创建taotao-solr文件夹;
这里写图片描述
2、 将./solr下的solr.xml拷贝到taotao-solr目录下;
这里写图片描述
这里写图片描述
3、 在taotao-solr下创建taotao目录,并且在taotao目录下创建conf和data目录;
这里写图片描述
4、 将example\solr\collection1\core.properties文件拷贝到example\taotao-solr\taotao下,并且修改name=taotao;
这里写图片描述
这里写图片描述
5、 将example\solr\collection1\conf下的schema.xml、solrconfig.xml拷贝到example\taotao-solr\taotao\conf下;
这里写图片描述

这里写图片描述
6、 修改schema.xml文件,使其配置最小化:

<?xml version="1.0" encoding="UTF-8" ?>
<schema name="example" version="1.5">

   <field name="_version_" type="long" indexed="true" stored="true"/>
   <field name="_root_" type="string" indexed="true" stored="false"/>
   <field name="id" type="long" indexed="true" stored="true" required="true" multiValued="false" />        
   <field name="title" type="string" indexed="true" stored="true"/>
   <field name="sellPoint" type="string" indexed="false" stored="true"/>
   <field name="price" type="long" indexed="true" stored="true"/>
   <field name="image" type="string" indexed="false" stored="true"/>
    <field name="cid" type="long" indexed="true" stored="true"/>
   <field name="status" type="int" indexed="true" stored="false"/>
   <field name="created" type="long" indexed="true" stored="false"/>
   <field name="updated" type="long" indexed="true" stored="false"/>
   <uniqueKey>id</uniqueKey>

    <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/> 
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>  

</schema>

7、 修改solrconfig.xml文件,修改一些配置,大部分配置先保持默认:
a) 将所有的<lib>标签注释掉;
这里写图片描述
b) 搜索<str name="df">text</str>替换成<str name="df">title</str>,搜索,然后全部替换掉。
这里写图片描述
c) 将<searchComponent name="elevator" class="solr.QueryElevationComponent" >注释掉(这个的功能类似百度的竞价排名):
这里写图片描述

8、 启动solr:
java -Dsolr.solr.home=taotao-solr -jar start.jar
这里写图片描述

测试:
这里写图片描述

添加IK中文分词器的支持

1、 将IKAnalyzer-2012-4x.jar拷贝到example\solr-webapp\webapp\WEB-INF\lib下;
这里写图片描述
2、 在schema.xml文件中添加fieldType:

<fieldType name="text_ik" class="solr.TextField">   
     <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
</fieldType>

这里写图片描述

测试:
这里写图片描述

这里写图片描述

导入商品数据到solr中

导入itcast-solrj

这里写图片描述

导入依赖

这里写图片描述

使用Solrj完成索引数据的CRUD

package cn.itcast.solrj.test;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.junit.Before;
import org.junit.Test;

import cn.itcast.solrj.pojo.Item;

public class ItemDataTest {

    private HttpSolrServer httpSolrServer;

    @Before
    public void setUp() throws Exception {
        // 在url中指定core名称:taotao
        //http://solr.taotao.com/#/taotao  -- 界面地址
        String url = "http://solr.taotao.com/taotao"; //服务地址
        HttpSolrServer httpSolrServer = new HttpSolrServer(url); //定义solr的server
        httpSolrServer.setParser(new XMLResponseParser()); // 设置响应解析器
        httpSolrServer.setMaxRetries(1); // 设置重试次数,推荐设置为1
        httpSolrServer.setConnectionTimeout(500); // 建立连接的最长时间

        this.httpSolrServer = httpSolrServer;
    }

    @Test
    public void testInsert() throws Exception{
        Item item = new Item();
        item.setCid(1L);
        item.setId(999L);
        item.setImage("image");
        item.setPrice(100L);
        item.setSellPoint("很好啊,赶紧来买吧.");
        item.setStatus(1);
        item.setTitle("飞利浦 老人手机 (X2560) 深情蓝 移动联通2G手机 双卡双待");
        this.httpSolrServer.addBean(item);
        this.httpSolrServer.commit();
    }

    @Test
    public void testUpdate() throws Exception{
        Item item = new Item();
        item.setCid(1L);
        item.setId(999L);
        item.setImage("image");
        item.setPrice(100L);
        item.setSellPoint("很好啊,赶紧来买吧. 豪啊");
        item.setStatus(1);
        item.setTitle("飞利浦 老人手机 (X2560) 深情蓝 移动联通2G手机 双卡双待");
        this.httpSolrServer.addBean(item);
        this.httpSolrServer.commit();
    }

    @Test
    public void testDelete() throws Exception{
        this.httpSolrServer.deleteById("999");
        this.httpSolrServer.commit();
    }

    @Test
    public void testQuery() throws Exception{
        int page = 2;
        int rows = 1;
        String keywords = "手机";
        SolrQuery solrQuery = new SolrQuery(); //构造搜索条件
        solrQuery.setQuery("title:" + keywords); //搜索关键词
        // 设置分页 start=0就是从0开始,,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
        solrQuery.setStart((Math.max(page, 1) - 1) * rows);
        solrQuery.setRows(rows);

        //是否需要高亮
        boolean isHighlighting = !StringUtils.equals("*", keywords) && StringUtils.isNotEmpty(keywords);

        if (isHighlighting) {
            // 设置高亮
            solrQuery.setHighlight(true); // 开启高亮组件
            solrQuery.addHighlightField("title");// 高亮字段
            solrQuery.setHighlightSimplePre("<em>");// 标记,高亮关键字前缀
            solrQuery.setHighlightSimplePost("</em>");// 后缀
        }

        // 执行查询
        QueryResponse queryResponse = this.httpSolrServer.query(solrQuery);
        List<Item> items = queryResponse.getBeans(Item.class);
        if (isHighlighting) {
            // 将高亮的标题数据写回到数据对象中
            Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
            for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) {
                for (Item item : items) {
                    if (!highlighting.getKey().equals(item.getId().toString())) {
                        continue;
                    }
                    item.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
                    break;
                }
            }
        }

        for (Item item : items) {
            System.out.println(item);
        }
    }

}

将商品数据到入到solr

这里写图片描述

这里写图片描述

这里写图片描述

测试:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/cckevincyh/article/details/80291948