solr搭建,
Solr服务搭建
Solr的环境
Solr是java开发。
需要安装jdk。
安装环境Linux。
需要安装Tomcat。
搭建步骤
第一步:把solr 的压缩包上传到Linux系统,解压solr
第二步:安装Tomcat,解压缩即可。
第三步:把solr部署到Tomcat下。
- 把/home/123/Desktop/solr-4.10.3/dist 目录的war拷贝到tomcat里面
solr-solrj-4.10.3.jar,这个是客户端jar包 - solr中的配置文件
solr文件其实就是solrHome文件 ,其实就是配置文件 - 创建文件夹 mkdir /usr/local/solr
- 将tomcat复制到/usr/local/solr这个文件夹里面 cp apache-tomcat-7.0.47 /usr/local/solr/tomcat -r
第四步:解压缩war包。启动Tomcat解压
- cp solr-4.10.3.war /usr/local/solr/tomcat/webapps/solr.war 将上面的war包赋值到tomcat里面
- 启动tmcat。./startup.sh
- 查看tomcat的日志,tail -f logs/catalina.out
- 然后删除war包,切记删除war前,必须要停止tomcat,不然会错误
- 把/root/solr-4.10.3/example/lib/ext目录下的所有的jar包,添加到toomcat 里面的solr工程中
/root/solr-4.10.3/example/lib/ext
cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
- 创建一个solrhome。/example/solr目录就是一个solrhome。复制此目录到/usr/local/solr/solrhome
[root@123 example]# cp solr /usr/local/solr/solrhome -r 配置solrhome,在/usr/local/solr/tomcat/webapps/solr/WEB-INF中的web.xml文件中配置solrhome
第五步:启动Tomcat
http://localhost:8080/solr/
配置业务域
1、商品Id
2、商品标题
3、商品卖点
4、商品价格
5、商品图片
6、分类名称
创建对应的业务域。需要制定中文分析器。
创建步骤:
第一步:把中文分析器添加到工程中。不然就不能中文分词了
1、把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目录下
2、把扩展词典、配置文件放到solr工程的WEB-INF/classes目录下,放这三个文件
第二步:配置一个FieldType,制定使用IKAnalyzer
修改schema.xml文件
:$ 跳转到最后一行 shift+g
:1 跳转到第一行 gg
修改Solr的schema.xml文件,添加FieldType:
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
第三步:配置业务域,type制定使用自定义的FieldType。
设置业务系统Field
<!--type="text_ik" 是分词的意思,定义类型 -->
<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_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"/>
第四步:重启tomcat
使用solrj,进行solr的一些操作
1.添加
/**
* 添加,修改和添加是一样的
* 查询到id,进行修改
* @Test
*/
public void add() throws Exception{
//创建一个SolrServer对象,创建一个连接。参数solr服务的url
//SolrServer HttpSolrServer是其子类
SolrServer solrServer = new HttpSolrServer("http://192.168.31.61:8080/solr/collection1");
//创建一个文档对象SolrInputDocument
SolrInputDocument document = new SolrInputDocument();
//向文档对象中添加域。文档中必须包含一个id域,所有的域的名称必须在schema.xml中定义。
document.addField("id", "doc01");
document.addField("item_title", "测试商品01");
document.addField("item_price", 1000);
//把文档写入索引库
solrServer.add(document);
//提交
solrServer.commit();
}
2.修改
/**
* 修改
* 与添加一样
* @throws Exception
*/
@Test
public void update() throws Exception{
//创建一个SolrServer对象,创建一个连接。参数solr服务的url
//SolrServer HttpSolrServer是其子类
SolrServer solrServer = new HttpSolrServer("http://192.168.31.61:8080/solr/collection1");
//创建一个文档对象SolrInputDocument
SolrInputDocument document = new SolrInputDocument();
//向文档对象中添加域。文档中必须包含一个id域,所有的域的名称必须在schema.xml中定义。
document.addField("id", "doc01");
document.addField("item_title", "测试商品01,修改");
document.addField("item_price", 1000);
//把文档写入索引库
solrServer.add(document);
//提交
solrServer.commit();
}
3.删除
/**
* 删除
* @throws Exception
*/
@Test
public void delect() throws Exception{
SolrServer solrServer = new HttpSolrServer("http://192.168.31.61:8080/solr/collection1");
//删除文档
//solrServer.deleteById("doc01");
//这个是根据属性删除,两个是一样的
solrServer.deleteByQuery("id:doc01");
//提交
solrServer.commit();
}
4.查询
/**
* 简单查询
* @throws Exception
*/
@Test
public void select() throws Exception{
//创建一个SolrServer对象。
SolrServer solrServer = new HttpSolrServer("http://192.168.31.61:8080/solr/collection1");
//创建一个SolrQuery对象。
SolrQuery query = new SolrQuery();
//设置查询条件。 查询所有
//query.setQuery("*:*");
query.set("q", "*:*");
//执行查询,QueryResponse对象。
QueryResponse queryResponse = solrServer.query(query);
//取文档列表。取查询结果的总记录数
SolrDocumentList solrDocumentList = queryResponse.getResults();
System.out.println("查询结果总记录数:" + solrDocumentList.getNumFound());
//遍历文档列表,从取域的内容。
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("item_title"));
System.out.println(solrDocument.get("item_sell_point"));
System.out.println(solrDocument.get("item_price"));
System.out.println(solrDocument.get("item_image"));
System.out.println(solrDocument.get("item_category_name"));
}
}
/**
* 查询复杂的
* @throws Exception
*/
public void selectQZ() throws Exception{
SolrServer solrServer = new HttpSolrServer("http://192.168.25.163:8080/solr/collection1");
//创建一个查询对象
SolrQuery query = new SolrQuery();
//查询条件
//查询有“手机”内容
query.setQuery("手机");
//分页,开始第几页
query.setStart(0);
//一页显示多少个
query.setRows(20);
//df 查询那个字段,比如在item_title 这个字段里面查询里面有手机的字段
query.set("df", "item_title");
//是不是要高亮
query.setHighlight(true);
//在那个字段里面高亮
query.addHighlightField("item_title");
//关键字前面加“<em>”
query.setHighlightSimplePre("<em>");
////关键字后面加“</em>”
query.setHighlightSimplePost("</em>");
//执行查询
QueryResponse queryResponse = solrServer.query(query);
//取文档列表。取查询结果的总记录数
SolrDocumentList solrDocumentList = queryResponse.getResults();
System.out.println("查询结果总记录数:" + solrDocumentList.getNumFound());
//遍历文档列表,从取域的内容。
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
//取高亮显示,
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
String title = "";
//有高亮的就显示高亮的,没有就显示原来的
if (list !=null && list.size() > 0 ) {
title = list.get(0);
} else {
title = (String) solrDocument.get("item_title");
}
System.out.println(title);
System.out.println(solrDocument.get("item_sell_point"));
System.out.println(solrDocument.get("item_price"));
System.out.println(solrDocument.get("item_image"));
System.out.println(solrDocument.get("item_category_name"));
}
}
借鉴黑马资料,在其谢谢黑马
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。