linux下安装solr及使用

Solr的环境

Solr是java开发。

需要安装jdk。https://blog.csdn.net/kxj19980524/article/details/84976803

安装环境Linux

需要安装Tomcat

solr下载地址https://download.csdn.net/download/kxj19980524/10873569

tomcat下载地址https://download.csdn.net/download/kxj19980524/10853555

拖拽上传功能https://blog.csdn.net/kxj19980524/article/details/85246778

下载好后上传到linux下,然后解压缩

然后在/usr/local下创建solr,并且复制一份tomcat到solr下

然后进入到dist下面找到solr的war包

把这个war包部署到tomcat的webapps下面

 然后启动tomcat来解压缩这个war包,进入到tomcat的bin目录下,执行startup.sh

查看日志,说明启动成功了,就解压完毕了,然后关闭它.

然后关闭tomcat,并且删除solr.war,关闭tomcat执行bin下面的shutdown.sh

然后进入到这个目录下,把这个目录下所有jar包复制到tomcat的solr的lib下面去

然后退到example目录下.复制solr到那个目录下,起个名为solrhome

 

然后编辑这个目录下的web.xml文件

先把原来的注释去掉,然后把value改为solrhome的路径  然后保存,启动solr下的tomcat,关闭防火墙,访问路径就可以了

然后配置IK分词器,下载地址https://download.csdn.net/download/kxj19980524/10867301,这个东西的压缩包不是linux版的,在window里面解压缩成文件夹,然后使用我上面拖拽功能的第二种上传到linux上

上传上去后进入到目录中,把jar包复制到这个目录下

然后在这个目录下创建classes文件夹

然后把这三个配置文件复制到classes文件夹下

然后进入到核1的conf里面配置IK分词器

进入到schema.xml里在最下面配置IK,和业务域,业务域的话,根据自己项目中的表做适当修改,不能直接抄,配置好后退出,重启就好了.

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


<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"/>

<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>4.10.4</version>
</dependency>

这下面就是使用代码来建立索引库,从数据库表中获取信息,把相应的信息建立成索引,自己稍加修改就可以了 

package cn.e3mall.search.service.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.e3mall.common.pojo.SearchItem;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.search.mapper.ItemMapper;
import cn.e3mall.search.service.SearchItemService;

/**
 * 索引库维护Service
 * <p>Title: SearchItemServiceImpl</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Service
public class SearchItemServiceImpl implements SearchItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private SolrServer solrServer;
    
    @Override
    public E3Result importAllItems() {
        try {
            //查询商品列表
            List<SearchItem> itemList = itemMapper.getItemList();
            //遍历商品列表
            for (SearchItem searchItem : itemList) {
                //创建文档对象
                SolrInputDocument document = new SolrInputDocument();
                //向文档对象中添加域
                document.addField("id", searchItem.getId());
                document.addField("item_title", searchItem.getTitle());
                document.addField("item_sell_point", searchItem.getSell_point());
                document.addField("item_price", searchItem.getPrice());
                document.addField("item_image", searchItem.getImage());
                document.addField("item_category_name", searchItem.getCategory_name());
                //把文档对象写入索引库
                solrServer.add(document);
            }
            //提交
            solrServer.commit();
            //返回导入成功
            return E3Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return E3Result.build(500, "数据导入时发生异常");
                    
        }
    }

}

然后在spring配置文件中注入一个solrserver对象,因为最好是让spring管理,并且是单例的节省性能,所以注入一下 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg index="0" value="http://192.168.25.163:8080/solr/collection1"/>
    </bean>
    
</beans>

不明白的话看window版的安装,都一样https://blog.csdn.net/kxj19980524/article/details/85202324

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值