下载地址: http://www.apache.org/dyn/closer.cgi/lucene/solr/
下载得到zip压缩包,下载的版本为4.10.2. 文件大小148MB左右。
1、 将solr-4.10.2.zip文件拷贝到C盘;(或者其他盘都可以,只要目录中不要出现中文就行。)
2、 解压solr-4.10.2.zip文件,得到solr-4.10.2目录。
3、 运行-> cmd 执行命(进入D:\solr\solr-4.10.2\example):
4、 执行命令:java -jar start.jar
5、 打开浏览器,输入地址:http://localhost:8983/solr/
至此,Solr已经安装成功。
solr安装完成后是没有数据的,需要导入一些数据进去方便我们学习。
运行->cmd -> 输入命令:cd D:\solr\solr-4.10.2\example\exampledocs
再输入命令:java -jar post.jar solr.xml monitor.xml
已经查询到数据了。
通过solr.taotao.com访问。
配置nginx:
测试:
发现,请求域名后面依然得有/solr.
默认启动的web应用服务器是jetty。
修改配置文件:
1、 在example目录下创建taotao-solr文件夹;
2、 将./solr下的solr.xml拷贝到taotao-solr目录下;
3、 在taotao-solr下创建taotao目录,并且在taotao目录下创建conf和data目录;
4、 将example\solr\collection1\core.properties文件拷贝到example\taotao-solr\taotao下,并且修改name=taotao;
5、 将example\solr\collection1\conf下的schema.xml、solrconfig.xml拷贝到example\taotao-solr\taotao\conf下;
<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"/>
<field name="sellPoint" type="string" indexed="false" stored="true"/>
<field name="price" type="long" indexed="true" stored="true"/>
<field name="image" type="string" indexed="false" stored="true"/>
<field name="cid" type="long" indexed="true" stored="true"/>
<field name="status" type="int" indexed="true" stored="false"/>
<field name="created" type="long" indexed="true" stored="false"/>
<field name="updated" type="long" indexed="true" stored="false"/>
<uniqueKey>iduniqueKey>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
schema>
7、 修改solrconfig.xml文件,修改一些配置,大部分配置先保持默认:
a) 将所有的
标签注释掉;
b) 搜索
替换成
,搜索,然后全部替换掉。
c) 将
注释掉(这个的功能类似百度的竞价排名):
8、 启动solr:
java -Dsolr.solr.home=taotao-solr -jar start.jar
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>
package cn.itcast.solrj.test;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.junit.Before;
import org.junit.Test;
import cn.itcast.solrj.pojo.Item;
public class ItemDataTest {
private HttpSolrServer httpSolrServer;
@Before
public void setUp() throws Exception {
// 在url中指定core名称:taotao
//http://solr.taotao.com/#/taotao -- 界面地址
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;
}
@Test
public void testInsert() throws Exception{
Item item = new Item();
item.setCid(1L);
item.setId(999L);
item.setImage("image");
item.setPrice(100L);
item.setSellPoint("很好啊,赶紧来买吧.");
item.setStatus(1);
item.setTitle("飞利浦 老人手机 (X2560) 深情蓝 移动联通2G手机 双卡双待");
this.httpSolrServer.addBean(item);
this.httpSolrServer.commit();
}
@Test
public void testUpdate() throws Exception{
Item item = new Item();
item.setCid(1L);
item.setId(999L);
item.setImage("image");
item.setPrice(100L);
item.setSellPoint("很好啊,赶紧来买吧. 豪啊");
item.setStatus(1);
item.setTitle("飞利浦 老人手机 (X2560) 深情蓝 移动联通2G手机 双卡双待");
this.httpSolrServer.addBean(item);
this.httpSolrServer.commit();
}
@Test
public void testDelete() throws Exception{
this.httpSolrServer.deleteById("999");
this.httpSolrServer.commit();
}
@Test
public void testQuery() throws Exception{
int page = 2;
int rows = 1;
String keywords = "手机";
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("");// 标记,高亮关键字前缀
solrQuery.setHighlightSimplePost("");// 后缀
}
// 执行查询
QueryResponse queryResponse = this.httpSolrServer.query(solrQuery);
List- items = queryResponse.getBeans(Item.class);
if (isHighlighting) {
// 将高亮的标题数据写回到数据对象中
Map
>> map = queryResponse.getHighlighting();
for (Map.Entry>> highlighting : map.entrySet()) {
for (Item item : items) {
if (!highlighting.getKey().equals(item.getId().toString())) {
continue;
}
item.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
break;
}
}
}
for (Item item : items) {
System.out.println(item);
}
}
}