大多数搜索引擎应用都必须具备某种搜索功能,问题是搜索功能每每是巨大的资源消耗而且它们因为沉重的数据库加载而拖垮你的应用的性能。java
这就是为何转移负载到一个外部的搜索服务器是一个不错的主意,Apache Solr是一个流行的开源搜索服务器,它经过使用相似REST的HTTP API,这就确保你能从几乎任何编程语言来使用solr。web
Solr是一个开源搜索平台,用于构建搜索应用程序。 它创建在Lucene(全文搜索引擎)之上。 Solr是企业级的,快速的和高度可扩展的。 使用Solr构建的应用程序很是复杂,可提供高性能。算法
Solr能够和Hadoop一块儿使用。因为Hadoop处理大量数据,Solr帮助咱们从这么大的源中找到所需的信息。不只限于搜索,Solr也能够用于存储目的。像其余NoSQL数据库同样,它是一种非关系数据存储和处理技术。spring
总之,Solr是一个可扩展的,可部署,搜索/存储引擎,优化搜索大量以文本为中心的数据。数据库
1:安装 Tomcat,解压缩便可。编程
2:解压 solr。服务器
3:把 solr 下的dist目录solr-4.10.3.war部署到 Tomcat\webapps下(去掉版本号)。app
4:启动 Tomcat解压缩 war 包框架
5:把solr下example/lib/ext 目录下的全部的 jar 包,添加到 solr 的工程中(\WEB-INF\lib目录下)。yii
6:建立一个 solrhome 。solr 下的/example/solr 目录就是一个 solrhome。复制此目录到D盘更名为solrhome
7:关联 solr 及 solrhome。须要修改 solr 工程的 web.xml 文件。
1 <env-entry> 2 <env-entry-name>solr/home</env-entry-name> 3 <env-entry-value>d:\solrhome</env-entry-value> 4 <env-entry-type>java.lang.String</env-entry-type> 5 </env-entry>
8:启动 Tomcat
http://IP:8080/solr/
IK Analyzer 是一个开源的,基亍 java 语言开发的轻量级的中文分词工具包。从 2006年 12 月推出 1.0 版开始, IKAnalyzer 已经推出了 4 个大版本。最初,它是以开源项目Luence 为应用主体的,结合词典分词和文法分析算法的中文分词组件。从 3.0 版本开始,IK 发展为面向 Java 的公用分词组件,独立亍 Lucene 项目,同时提供了对 Lucene 的默认优化实现。在 2012 版本中,IK 实现了简单的分词歧义排除算法,标志着 IK 分词器从单纯的词典分词向模拟语义分词衍化。
1、把IKAnalyzer2012FF_u1.jar 添加到 solr 工程的 lib 目录下
2、建立WEB-INF/classes文件夹 把扩展词典、停用词词典、配置文件放到 solr 工程的 WEB-INF/classes 目录下。
3、修改 Solrhome 的 schema.xml 文件,配置一个 FieldType,使用 IKAnalyzer
1 <fieldType name="text_ik" class="solr.TextField"> 2 <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> 3 </fieldType>
域至关于数据库的表字段,用户存放数据,所以用户根据业务须要去定义相关的Field(域),通常来讲,每一种对应着一种数据,用户对同一种数据进行相同的操做。
域的经常使用属性:
修改solrhome的schema.xml 文件 设置业务系统 Field
1 <field name="item_goodsid" type="long" indexed="true" stored="true"/> 2 <field name="item_title" type="text_ik" indexed="true" stored="true"/> 3 <field name="item_price" type="double" indexed="true" stored="true"/> 4 <field name="item_image" type="string" indexed="false" stored="true" /> 5 <field name="item_category" type="string" indexed="true" stored="true" /> 6 <field name="item_seller" type="text_ik" indexed="true" stored="true" /> 7 <field name="item_brand" type="string" indexed="true" stored="true" />
复制域的做用在于将某一个Field中的数据复制到另外一个域中
1 <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> 2 <copyField source="item_title" dest="item_keywords"/> 3 <copyField source="item_category" dest="item_keywords"/> 4 <copyField source="item_seller" dest="item_keywords"/> 5 <copyField source="item_brand" dest="item_keywords"/>
当咱们须要动态扩充字段时,咱们须要使用动态域。对于品优购,规格的值是不肯定的,因此咱们须要使用动态域来实现。须要实现的效果以下:
配置
1 <dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
如何将Solr的应用集成到Spring中?能够,Spring Data Solr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。
(1) 建立maven工程,pom.xml中引入依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.data</groupId> 4 <artifactId>spring-data-solr</artifactId> 5 <version>1.5.5.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-test</artifactId> 10 <version>4.2.4.RELEASE</version> 11 </dependency> 12 <dependency> 13 <groupId>junit</groupId> 14 <artifactId>junit</artifactId> 15 <version>4.9</version> 16 </dependency> 17 </dependencies>
(2)在src/main/resources下建立 applicationContext-solr.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:solr="http://www.springframework.org/schema/data/solr" 6 xsi:schemaLocation="http://www.springframework.org/schema/data/solr 7 http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd"> 12 <!-- solr服务器地址 --> 13 <solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" /> 14 <!-- solr模板,使用solr模板可对索引库进行CRUD的操做 --> 15 <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> 16 <constructor-arg ref="solrServer" /> 17 </bean> 18 </beans>
建立 cn.itcast.pojo 包,将品优购的TbItem实体类拷入本工程 ,属性使用@Field注解标识 。 若是属性与配置文件定义的域名称不一致,须要在注解中指定域名称。
1 public class TbItem implements Serializable{ 2 3 @Field 4 private Long id; 5 6 @Field("item_title") 7 private String title; 8 9 @Field("item_price") 10 private BigDecimal price; 11 12 @Field("item_image") 13 private String image; 14 15 @Field("item_goodsid") 16 private Long goodsId; 17 18 @Field("item_category") 19 private String category; 20 21 @Field("item_brand") 22 private String brand; 23 24 @Field("item_seller") 25 private String seller; 26 ....... 27 }
建立测试类TestTemplate.java
1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration(locations="classpath:applicationContext-solr.xml") 3 public class TestTemplate { 4 5 @Autowired 6 private SolrTemplate solrTemplate; 7 8 @Test 9 public void testAdd(){ 10 TbItem item=new TbItem(); 11 item.setId(1L); 12 item.setBrand("华为"); 13 item.setCategory("手机"); 14 item.setGoodsId(1L); 15 item.setSeller("华为2号专卖店"); 16 item.setTitle("华为Mate9"); 17 item.setPrice(new BigDecimal(2000)); 18 solrTemplate.saveBean(item); 19 solrTemplate.commit(); 20 } 21 }
1 @Test 2 public void testFindOne(){ 3 TbItem item = solrTemplate.getById(1, TbItem.class); 4 System.out.println(item.getTitle()); 5 }
1 @Test 2 public void testDelete(){ 3 solrTemplate.deleteById("1"); 4 solrTemplate.commit(); 5 }
首先循环插入100条测试数据
1 @Test 2 public void testAddList(){ 3 List<TbItem> list=new ArrayList(); 4 5 for(int i=0;i<100;i++){ 6 TbItem item=new TbItem(); 7 item.setId(i+1L); 8 item.setBrand("华为"); 9 item.setCategory("手机"); 10 item.setGoodsId(1L); 11 item.setSeller("华为2号专卖店"); 12 item.setTitle("华为Mate"+i); 13 item.setPrice(new BigDecimal(2000+i)); 14 list.add(item); 15 } 16 17 solrTemplate.saveBeans(list); 18 solrTemplate.commit(); 19 }
编写分页查询测试代码:
1 2 @Test 3 public void testPageQuery(){ 4 Query query=new SimpleQuery("*:*"); 5 query.setOffset(20);//开始索引(默认0) 6 query.setRows(20);//每页记录数(默认10) 7 ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class); 8 System.out.println("总记录数:"+page.getTotalElements()); 9 List<TbItem> list = page.getContent(); 10 showList(list); 11 } 12 //显示记录数据 13 private void showList(List<TbItem> list){ 14 for(TbItem item:list){ 15 System.out.println(item.getTitle() +item.getPrice()); 16 } 17 }
Criteria 用于对条件的封装:
1 @Test 2 public void testPageQueryMutil(){ 3 Query query=new SimpleQuery("*:*"); 4 Criteria criteria=new Criteria("item_title").contains("2"); 5 criteria=criteria.and("item_title").contains("5"); 6 query.addCriteria(criteria); 7 //query.setOffset(20);//开始索引(默认0) 8 //query.setRows(20);//每页记录数(默认10) 9 ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class); 10 System.out.println("总记录数:"+page.getTotalElements()); 11 List<TbItem> list = page.getContent(); 12 showList(list); 13 }
1 @Test 2 public void testDeleteAll(){ 3 Query query=new SimpleQuery("*:*"); 4 solrTemplate.delete(query); 5 solrTemplate.commit(); 6 }