搜索引擎solr
http://blog.csdn.net/tjcyjd/article/details/43091107
1. 一个搜索引擎由搜索器 、索引器 、检索器 和用户接口 四个部分组成。搜索器的功能是在互联网 中漫游,发现和搜集信息。索引器的功能是理解搜索器所搜索的信息,从中抽取出索引项,用于表示文档 以及生成文档库的索引表。检索器的功能是根据用户的查询在索引库中快速检出文档,进行文档与查询的相关度评价,对将要输出的结果进行排序,并实现某种用户相关性反馈机制。用户接口的作用是输入用户查询、显示查询结果、提供用户相关性反馈机制。
2.配置搜索引擎solr
Solr服务器采用java5开发的,是基于Lucene全文搜索的。
要想搭建Solr,首先进行Java环境的配置,安装对应的jdk以及tomcat,在此就不多讲。
以下是在jdk1.7和tomcat1.7的环境下搭建最新版本的solr4.10.3。
具体步骤如下:
(1)到官网http://lucene.apache.org/solr/mirrors-solr-latest-redir.html下载.
(2)建目录/webapps/mysolr/solr
(3)解压压缩包solr-4.10.3,找到example下的webapps中的solr.war包,并将其解压。
(4)将解压完的war包(solr文件夹)拷贝到第2步建的目录:/webapps/mysolr下
(5)拷贝两个地方的jar包到/webapps/mysolr/solr/WEB-INF/lib下
a example下lib包的所有jar包
b example下lib包下的ext包中的所有jar包
(6)拷贝example/resource下的log4j.properties文件到/webapps/mysolr/solr/classpath 下
(7)solrhome的配置:
先创建一个solrhome目录:/webapps/mysolr/solrhome,然后将example/solr下的所有文件拷贝到/webapps/mysolr/solrhome下
然后修改配置文件/webapps/mysolr/solr/WEB-INF/web.xml,将solr/home的注解放开并配置如下:
[html] view plain copy
- <env-entry>
- <env-entry-name>solr/home</env-entry-name>
- <env-entry-value>/webapps/mysolr/solrhome</env-entry-value>
- <env-entry-type>java.lang.String</env-entry-type>
- </env-entry>
(8)把/webapps/mysolr/solr部署到tomcat下,并启动tomcat。
以上就完成了solr环境的基本搭建,访问http://loclhost:8080/solr 可看到如下界面:
3.使用solr
(1). applicationContext-solr.xml
<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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置SolrServer对象 --> <!-- 单机版 --> <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg name="baseURL" value="${SOLR.SERVER.URL}"></constructor-arg> </bean> <!-- 集群版 --> <!-- <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer"> <constructor-arg name="zkHost" value="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"></constructor-arg> <property name="defaultCollection" value="collection2"></property> </bean> --> </beans> |
(2). resource.properties
SOLR.SERVER.URL=http\://192.168.56.102\:8080/solr |
(3)索引库维护(更新数据)http://localhost:8083/search/manager/importall
@Controller @RequestMapping("/manager") public class ItemController {
@Autowired private ItemService itemService;
/** * 导入商品数据到索引库 */ @RequestMapping("/importall") @ResponseBody public EasyBuyResult importAllItems() { EasyBuyResult result = itemService.importAllItems(); return result; }
} |
(4)删除数据
<delete> <query> *:* </query> </delete> <commit/> |
(5)数据搜索
/** * 根据商品分类查询控制器 * @param queryString * @param page * @param rows * @return * @throws Exception */ @RequestMapping(value="/queryItemsByCid", method=RequestMethod.GET) @ResponseBody public EasyBuyResult queryItemsByCid(@RequestParam("cid")String cid, @RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="12")Integer rows) throws Exception { //查询条件不能为空 if (StringUtils.isBlank(cid)) { return EasyBuyResult.build(400, "查询条件不能为空"); } SearchResult searchResult = searchService.queryItemsByCid(cid, page, rows); return EasyBuyResult.ok(searchResult);
}
} |