#1. solr的安装 1.1 解压文件solr-4.10.2.zip 1.2 进入C:\solr-4.10.2\example 1.3 运行命令:java -jar start.jar
1.4 打开浏览器,输入地址:http://localhost:8983/solr/java
#2. solr功能简介nginx
#3. solr 默认的web容器:jetty #4. 配置经过域名访问solrweb
127.0.0.1 solr.taotao.com
server { #1.侦听80端口 listen 80; server_name solr.taotao.com; proxy_set_header Host $host; proxy_set_header X-Forward-Host $host; proxy_set_header X-Forward-Server $host; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; location /{ proxy_pass http://127.0.0.1:8983; proxy_connect_timeout 600; proxy_read_timeout 600; } }
solr-4.10.2\example\contexts\solr-jetty-context.xml
<Set name="contextPath"><SystemProperty name="hostContext" default="/solr"/></Set>
替换为 <Set name="contextPath"><SystemProperty name="hostContext" default="/"/></Set>
#5. solr的web目录 - solr:存放core - solr-webapp 运行的web服务 - start.jar 启动solr服务的jar(java -jar start.jar)apache
#6. 建立taotao core core是solr中最为重要的一个概念,solr应用能够部署多个core.每一个core包含2个目录,conf和data,分别用于存放配置文件和数据.core的核心配置文件有2个.solrconfig.xml和schema.xml,分别用于solr的配置以及数据格式定义. 1) 建立taotao-solr - 在example目录下建立taotao-solr文件夹 - 将./solr下的solr.xml拷贝到taotao-solr目录下 - 在taotao-solr下建立taotao目录,而且在taotao目录下建立conf和data目录 - 将example\solr\collection1\core.properties文件拷贝到example\taotao-solr\taotao下,而且修改name=taotao - 将example\solr\collection1\conf下的schema.xml、solrconfig.xml拷贝到example\taotao-solr\taotao\conf下 - 修改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" required="true" multiValued="false"/> <uniqueKey>id</uniqueKey> <fieldType name="string" class="solr.StrField" sortMissingLast="true"/> <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/> </schema>
<str name="df">text</str>
替换成<str name="df">title</str>
<searchComponent name="elevator" class="solr.QueryElevationComponent" >
注释掉(这个的功能相似百度的竞价排名)java -Dsolr.solr.home=taotao-solr -jar start.jar
#7. 集成IKAnalyzer中文分词器 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>
<field name="title" type="string" indexed="true" stored="true" required="true" multiValued="false"/>
中的string
换成text_ik
#8. solrj solrj是solr的java客户端,通常状况下都是经过solrj来调用solr服务. 导入依赖:app
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.10.1</version> </dependency>
测试 1). setUp()
webapp
@Before public void setUp() throws Exception { // 在url中指定core名称:taotao // http://solr.taotao.com/#/taotao地址是solr控制台的url // http://solr.taotao.com/taotao地址是solrj操做solr的接口地址 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; solrjService = new SolrjService(httpSolrServer); }
2). add
性能
@Test public void testAdd() throws Exception { Foo foo = new Foo(); // foo.setId(System.currentTimeMillis()); foo.setTitle("轻量级企业Java应用实战(第3版):Struts2+Spring3+Hibernate(JAVA)整合开发(附CD光盘)java"); foo.setId(1481722460050l); this.solrjService.add(foo); foo.setId(1446948087657l); this.solrjService.add(foo); foo.setId(1481722517441l); this.solrjService.add(foo); foo.setId(1481722521468l); this.solrjService.add(foo); // 1481722460050 // 1446948087657 // 1481722517441 // 1481722521468 }
3). update
(根据主键判断是否存在,若是存在就更新,不然插入)测试
@Test public void testUpdate() throws Exception { Foo foo = new Foo(); foo.setId(1446948087657l); foo.setTitle("new -轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)java 个人Java"); this.solrjService.add(foo); }
4). delete
@Test public void testDelete() throws Exception { this.solrjService.delete(Arrays.asList("1481722460050")); }
5). search
@Test public void testSearch() throws Exception { List<Foo> foos = this.solrjService.search("java", 1, 10); for (Foo foo : foos) { System.out.println(foo); } }
其中search
的具体实现是:
public List<Foo> search(String keywords, Integer page, Integer rows) throws Exception { 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<Foo> foos = queryResponse.getBeans(Foo.class); if (isHighlighting) { // 将高亮的标题数据写回到数据对象中 Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting(); for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) { for (Foo foo : foos) { if (!highlighting.getKey().equals(foo.getId().toString())) { continue; } foo.setTitle(StringUtils.join(highlighting.getValue().get("title"), "")); break; } } } return foos; }
注意点:
field:key
如:title:java
.AND
和OR
必定大写solrconfig.xml
中的df
值在定义<requestHandler name="/select" class="solr.SearchHandler"> <!-- default values for query parameters can be specified, these will be overridden by parameters in the request --> <lst name="defaults"> <str name="echoParams">explicit</str> <int name="rows">10</int> <str name="df">title</str> </lst>
6). deleteByQuery
@Test public void testDeleteByQuery() throws Exception { httpSolrServer.deleteByQuery("*:*"); httpSolrServer.commit(); }
#9. 性能优化