首页 技术 正文
技术 2022年11月20日
0 收藏 957 点赞 3,001 浏览 2726 个字

Solr配置与简单Demo

简介:

solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目。它的官方网址在http://lucene.apache.org/solr/  。solr需要运行在一个servlet 容器里,例如tomcat。solr在lucene的上层提供了一个基于HTTP/XML的Web Services,我们的应用需要通过这个服务与solr进行交互。

前提,下载tomcat。省略。

第一步:下载Solr, http://www.apache.org/dyn/closer.cgi/lucene/solr/

我下载的是3.5版本,把它解压到E盘。E:/apache-solr-3.5.0

第二步:修改conf\server.xml,把8080端口所在的那一行修改如下:

<Connector port="8080" protocol="HTTP/1.1"  
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

也有用下面这个的。

<Connector port="8080" URLEncoder="UTF-8" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>

其实就是增加了URIEncoding=”UTF-8″这一配置。推荐用第一种,这两个区别暂时不知。

第三步:配置Solr

还是在conf目录下,增加 Catalina\localhost\solr.xml 文件,如果conf文件夹下没有Catalina,新建它。

solr.xml内容:

<Context docBase="E:/apache-solr-3.5.0/dist/apache-solr-3.5.0.war" debug="0" crossContext="true" >
<Environment name="solr/home" type="java.lang.String" value="E:/apache-solr-3.5.0/example/solr" override="true" /></Context>

第四步:启动Tomcat。输入http://localhost:8080/solr/ 出现欢迎界面,表示成功。

第五步:简单Java Api 操作

参考 http://www.iteye.com/topic/315330 写的很详细了。

一个简单的写入,读出数据的完整代码如下:

Solr配置与简单Demo[转]

package com.weishangye.test.solr;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;public class SolrTest1 { public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/solr";
SolrServer server = new CommonsHttpSolrServer(url);
server.deleteByQuery( "*:*" );// delete everything!
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "id1", 1.0f );
doc1.addField( "name", "doc1", 1.0f );
doc1.addField( "price", 10 ); SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField( "id", "id2", 1.0f );
doc2.addField( "name", "冰羽", 1.0f );
doc2.addField( "price", 20 ); Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add( doc2 );
server.add( docs );
server.commit(); SolrQuery query = new SolrQuery();
query.setQuery( "*:*" );
query.addSortField( "price", SolrQuery.ORDER.desc );
QueryResponse rsp = server.query( query );
SolrDocumentList docsList = rsp.getResults();
for(Iterator<SolrDocument> doc =docsList.iterator();doc.hasNext();){
SolrDocument d = doc.next();
System.out.print(d.getFieldValue("id")+"->");
System.out.println(d.getFieldValue("name"));
}
}}

Solr配置与简单Demo[转]

资源:

http://www.ibm.com/search/csass/search/?sn=dw&lang=zh&cc=CN&en=utf&hpp=20&dws=cndw&lo=zh&q=solr&Search=%E6%90%9C%E7%B4%A2  IBM社区

http://www.oschina.net/question/12_9398 利用开源的 Apache Solr 搜索引擎构建 RESTful 基础存储服务

http://blog.chenlb.com/tag/solr

 分类: JAVA

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918