单机版solr

单机版solr搭建

一、准备工作
  • 安装jdk:

    1. 将jdk安装包上传后解压(tar -zxvf 文件名)

    2. 配置jdk环境变量,vim /etc/profile,在文件末尾添加如下内容后重新初始化配置文件source /etc/profile

      export JAVA_HOME=/root/jdk1.7.0_55     //此处为jdk的文件路径
      export JRE_HOME=${JAVA_HOME}/jre
      export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
      export PATH=${JAVA_HOME}/bin:$PATH
      
    3. java - version检查是否安装成功

      报错处理/lib/ld-linux.so.2 目录不存在:yum install glibc.i686

  • 安装tomcat:

    1. 将安装包上传后解压,复制解压后的文件到/usr/local/solr,作为solr的安装目录

      cp 文件名 /usr/local/solr/tomcat -r
      
二、安装solr
  1. 将安装包上传后解压
  2. 部署war包: 把解压文件夹中的dist/solr-4.10.3.war复制到tomcat下
    bash cp solr-4.10.3.war /usr/local/solr/tomcat/webapps/solr.war
  3. 解压缩war包: 启动tomcat后关闭tomcat,删除war包
  4. 补全日志jar包: 将/root/solr-4.10.3/example/lib/ext目录下的所有的jar包添加到solr工程中
    bash cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
  5. 创建solrhome存放索引库和配置文件: 把/root/solr-4.10.3/example/solr文件夹复制一份到solr安装目录下作为solrhome
    bash cp -r solr /usr/local/solr/solrhome
  6. 配置关联,告诉solr服务solrhome的位置: 修改/usr/local/solr/tomcat/webapps/solr/WEB-INF中的web.xml在这里插入图片描述
  7. 安装完成,启动tomcat,访问: http:ip:8080/solr
三、配置中文分词器IKAnalyzer
  1. 将安装包解压后上传

  2. 把IKAnalyzer依赖的jar包添加到solr中: 将/root/IKAnalyzer目录下的IKAnalyzer2012FF_u1.jar包添加到solr中

    cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
    
  3. 把分析器使用的停用词词典,扩展词典,配置文件添加到classpath中: 在/usr/local/solr/tomcat/webapps下新建classes文件夹,将停用词词典和扩展词典,配置文件拷贝过去

    cp ext_stopword.dic mydict.dic IKAnalyzer.cfg.xml/usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
    
  4. 创建一个分词器为IKAnalyzer的FieldType: 在solrhome/collection1/conf文件夹下的Schema.xml中添加定义一个FieldType,在FieldType中指定中文分析器为IKAnalyzer

    <fieldType name="text_ik" class="solr.TextField">
      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>
    
  5. 为自定义的FieldType指定业务域: 在solrhome/collection1/conf文件夹下的Schema.xml中添加域

    <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"/>
    
  6. 重启tomcat

单机版solr导入数据

从数据库中根据sql语句查询数据,遍历数据创建文档对象,把文档对象写入索引库

  1. ItemMapper.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.taotao.search.mapper.ItemMapper" >
      <select id = "importItems" resultType = "com.taotao.search.pojo.SearchItem">
      	SELECT
    			a.id,
    			a.title,
    			a.sell_point,
    			a.price,
    			a.image,
    			b.`name` category_name,
    			c.item_desc
    		FROM
    			tb_item a
    		LEFT JOIN tb_item_cat b ON a.cid = b.id
    		LEFT JOIN tb_item_desc c ON a.id = c.item_id
    		WHERE
    			a.`status` = 1
      </select>
    </mapper>
    
    applicationContent-dao.xml
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<property name="basePackage" value="com.taotao.mapper,com.taotao.search.mapper" />
    </bean>
    
  2. service层
    applicationContent-service.xml
    <!-- 单机版solr -->
    <bean id = "httpSolrServer" class = "org.apache.solr.client.solrj.impl.HttpSolrServer">
    	<constructor-arg name = "baseURL" value = "http://192.168.134.131:8080/solr">
    	</constructor-arg>
    </bean>
    
    @Service
    public class ItemServiceImpl implements ItemService {
    	@Autowired
    	private ItemMapper itemMapper;
    	
    	@Autowired
    	private SolrServer solrServer;
    	
    	@Override
    	public TaotaoResult importItems() throws Exception{
    		
    		//从数据库中查询数据
    		List<SearchItem> items = itemMapper.importItems();
    		for (SearchItem item : items) {
    			//创建文档
    			SolrInputDocument document = new SolrInputDocument();
    			
    			//向文档中添加域
    			document.addField("id", item.getId());
    			document.addField("item_title", item.getTitle());
    			document.addField("item_sell_point", item.getSell_point());
    			document.addField("item_price", item.getPrice());
    			document.addField("item_image", item.getImage());
    			document.addField("item_category_name", item.getCategory_name());
    			document.addField("item_desc", item.getItem_desc());
    			
    			//将文档写入索引库
    			solrServer.add(document);
    		}
    		//提交
    		solrServer.commit();
    		
    		return TaotaoResult.ok();
    	}
    }
    
  3. controller层
    @RequestMapping("/importData")
    @ResponseBody
    public TaotaoResult importItems() {
    	try {
    		TaotaoResult result = itemService.importItems();
    		return result;
    	} catch (Exception e) {
    		e.printStackTrace();
    		return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
    	}	
    }
    

项目中使用solr搜索

  1. 设置查询条件

    public SearchResult search(int page, int row, String keyword) throws Exception {
    		
    		SolrQuery query = new SolrQuery();
    		//设置搜索条件
    		query.setQuery(keyword);
    		//设置分页条件
    		query.setStart((page-1)*row);
    		query.setRows(row);
    		//设置默认搜索域
    		query.set("df", "item_title");
    		//设置高亮
    		query.setHighlight(true);
    		query.addHighlightField("item_title");
    		//设置高亮前端拼接样式
    		query.setHighlightSimplePre("<font class=\"skcolor_ljg\">");
    		query.setHighlightSimplePost("</font>");
    		SearchResult result = searchDao.search(query);
    		
    		Long count = result.getItemCount();
    		int pageCount = (int) (count/page);
    		if(count%row>0) {
    			pageCount++;
    		}
    		result.setPageCount(pageCount);
    		result.setCurPage(page);
    		
    		return result;
    	}
    
  2. 根据搜索条件进行搜索

    public SearchResult search(SolrQuery query) throws Exception {
    		
    		SearchResult result = new SearchResult();
    		List<SearchItem> items = new ArrayList<>();
    		//执行查询
    		QueryResponse response = solrServer.query(query);
    		//获取查询到的文档对象列表
    		SolrDocumentList documentList = response.getResults();
    		//获取文档对象中的所有高亮节点
    		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
    		for (SolrDocument document : documentList) {
    			SearchItem item = new SearchItem();
    			//根据域名取文档对象中当前物品的对应数据
    			item.setId((String) document.get("id"));
    			item.setCategory_name((String) document.get("item_category_name"));
    			item.setImage((String) document.get("item_image"));
    			item.setPrice((Long) document.get("item_price"));
    			item.setSell_point((String) document.get("item_sell_point"));
    			//获取当前物品的指定高亮域的高亮信息
    			List<String> list = highlighting.get(document.get("id")).get("item_title");
    			String itemTitle = "";
    			if(list!=null && list.size()>0) {
    				itemTitle = list.get(0);
    			}else {
    				itemTitle = (String) document.get("item_title");
    			}
    			item.setTitle(itemTitle);
    			items.add(item);
    		}
    		
    		result.setItemList(items);
    		result.setItemCount(documentList.getNumFound());
    		
    		return result;
    	}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值