1. 安装&启动

官网:http://lucene.apache.org/solr/

下载源代码,解压,进入根目录(我把solr放在/usr/local/solr下)

在/usr/local/solr/example中,有一些例子

启动例子

bin/solr start –e cloud –noprompt

-e <example>  Name of the example to run; available examples:

      cloud:         SolrCloud example

      default:       Solr default example

      dih:           Data Import Handler

      schemaless:    Schema-less example

      multicore:     Multicore

-noprompt    对输入不进行提示,接受所有默认输入

  

另外一种启动例子的方法

cd example
Java –jar start.jar
所有的日志会打印在控制台

在浏览器中输入以下地址,打开solr管理界面

http://localhost:8983/solr

2. 使用工具快速索引文档

以下命令均在命令行中完成,需要指定-classpath的,即指定solr-core-version.jar的路径

(我的在/usr/local/solr/example目录下执行

java -classpath dist/solr-c -Dauto -Drecursive org.apache.solr.util.SimplePostTool docs/

索引docs目录下的所有文件(当前目录/usr/local/solr/example)

java -Dauto -Drecursive org.apache.solr.util.SimplePostTool docs/

索引xml、json

在索引json时,需要指定 auto参数,因为SimplePostTool默认索引的是xml文档

Because the SimplePostTool defaults to assuming files are in Solr XML format, the -Dauto switch is used to post JSON files so that it uses the appropriate content type.

java org.apache.solr.util.SimplePostTool example/exampledocs/*.xml
java -Dauto org.apache.solr.util.SimplePostTool example/exampledocs/books.csv
java -Dauto org.apache.solr.util.SimplePostTool example/exampledocs/books.json

使用post.jar索引文件

java -jar example/exampledocs/post.jar example/exampledocs/hd.xml

3. 停止solr、清空节点数据

bin/solr stop -all ; rm -Rf node1/ node2/

4. 使用SolrJ操作solr

4.1 安装依赖包

SolrJ是一套操作solr的java API包,官网如下:

参考目录:

http://wiki.apache.org/solr/Solrj

http://wiki.apache.org/solr/SolrQuerySyntax

使用SolrJ前,官网说明需要依赖如下包:

•	apache-solr-solrj-*.jar
• commons-codec-1.3.jar
• commons-httpclient-3.1.jar
• commons-io-1.4.jar
• jcl-over-slf4j-1.5.5.jar
• slf4j-api-1.5.5.jar
• slf4j-jdk14-1.5.5.jar

实际操作中,在IntelliJ IDEA中,编写操作solr的代码,需要的不止这几个包,我的jar包如下:

•	solr-solrj-4.10.2.jar
• commons-codec-1.3.jar
• commons-io-1.4.jar
• jcl-over-slf4j.1.7.7.jar
• slf4j-api-1.5.6.jar
• slf4j-jdk14-1.5.6.jar
• httpclient-4.3.1.jar
• httpcore-4.3.jar
• httpmime-4.3.1.jar
• noggit-0.5.jar

这里面有两个坑:

  一:使用maven的pom.xml来管理包的时候,下载的包经常是不可用的(损坏),但是maven检测不到。这个是我碰到的,最后是在solr安装目录下,搜索找到对应的jar包,手动添加的工程中。

  二:官网给的依赖包不太全,可能并不是针对独立项目,这点我也不太明白,我自己试验是有上面几个包就够了。

在尝试使用solrJ可能使用的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
at solrX.IndexDataTools.main(IndexDataTools.java:115)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more

上面的解法:添加httpclient-4.1.4.jar

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody
Caused by: java.lang.ClassNotFoundException: org.apache.http.entity.mime.content.ContentBody

上面的解法:添加 httpmime-4.2.3.jar

Exception in thread "main" java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.staticInitialize(LoggerFactory.java:83)
at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:73)
at org.apache.solr.client.solrj.impl.HttpSolrServer.<clinit>(HttpSolrServer.java:91)
at solrX.IndexDataTools.main(IndexDataTools.java:115)

上面的解法:slf4j-api-1.5.6.jar

Exception in thread "main" java.lang.NoClassDefFoundError: org/noggit/CharArr
at org.apache.solr.common.util.JavaBinCodec.<init>(JavaBinCodec.java:590)
at org.apache.solr.client.solrj.impl.BinaryResponseParser.processResponse(BinaryResponseParser.java:43)
at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:528)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206)
at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:124)
at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:68)
at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:54)
at solr.data.tools.IndexDataTools.main(IndexDataTools.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: org.noggit.CharArr
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 14 more

上面的解法:添加noggit-0.5.jar

以上异常请同时参考:http://blog.sina.com.cn/s/blog_5ddc071f0101mtpf.html

4.2 Solrj 添加索引

SolrInputDocument:构造Solr文档对象
在进行大数据索引时,最基础的办法就是依次构造该文档对象,然后添加到solr中。
public static void main(String[] args) throws IOException, SolrServerException {
String solrUrl = "http://localhost:8983/solr";
HttpSolrServer server = new HttpSolrServer(solrUrl);
server.setMaxRetries(1);
server.setConnectionTimeout(5000);
server.setSoTimeout(10000);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false);
server.setAllowCompression(true); // String url = server.getBaseURL();
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", "doc3", 1.0f );
doc2.addField( "price", 20 ); Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add(doc2); server.add(docs);
//*********************
//set auto commit docs
// UpdateRequest req = new UpdateRequest();
// req.setAction( UpdateRequest.ACTION.COMMIT, false, false );
// req.add( docs );
// UpdateResponse rsp = req.process( server );
//**********************
server.commit();
}

4.3 查询solr

SolrQuery: 构建solr查询对象,设置查询的属性

QueryResponse: solr查询结果对象

SolrDocumentList: solr查询结果对象包含的结果对象

private String solrUrl = "http://localhost:8983/solr";
public void querySolr(String queryStr) throws SolrServerException {
HttpSolrServer server = new HttpSolrServer(solrUrl); SolrQuery query = new SolrQuery();
query.setRequestHandler("/select");
query.setQuery("name:doc*");
query.setSort("id", SolrQuery.ORDER.asc); QueryResponse rsp = server.query(query); Iterator<SolrDocument> iter = rsp.getResults().iterator();
SolrDocumentList docList = rsp.getResults();
List<BooksItem> beans = rsp.getBeans(BooksItem.class); while(iter.hasNext()){
SolrDocument doc = iter.next();
String id = (String)doc.getFieldValue("id");
String name = (String)doc.getFieldValue("name");
System.out.print(
"---id:"+id+"--name:"+name
);
Collection<String> names = doc.getFieldNames();
for (String fName : names){
java.lang.Object fValue = doc.getFieldValue(fName);
boolean isString = fValue instanceof String;
boolean isList = fValue instanceof ArrayList<?>;
if (isString){
fValue = (String)fValue;
System.out.print("fieldName:"+fName+" filedValue:"+fValue+"\n");
}
if (isList){
for (String s : (ArrayList<String>)fValue){
System.out.print("fieldName:"+fName+" filedValue:"+String.valueOf(s)+"\n");
}
} } }
}

  

  

  

【Solr初探】Solr安装,启动,查询,索引的更多相关文章

  1. Solr 08 - 在Solr Web管理页面中查询索引数据 (Solr中各类查询参数的使用方法)

    目录 1 Solr管理页面的查询入口 2 Solr查询输入框简介 3 Solr管理页面的查询方案 1 Solr管理页面的查询入口 选中需要查询的SolrCore, 然后在菜单栏选择[Query]: 2 ...

  2. lucene&solr学习——创建和查询索引(代码篇)

    1. Lucene的下载 Lucene是开发全文检索功能的工具包,从官网下载Lucene4.10.3并解压. 官网:http://lucene.apache.org/ 版本:lucene7.7.0 ( ...

  3. lucene&solr学习——创建和查询索引(理论)

    1.Lucene基础 (1) 简介 Lucene是apache下的一个开放源代码的全文检索引擎工具包.提供完整的查询引擎和索引引擎:部分文本分析引擎. Lucene的目的是为软件开发人员提供一个简单易 ...

  4. 【Solr】Solr的安装部署

    目录 Solr安装部署 Solr Web界面分析 回到顶部 solr安装和部署 solr下载 http://lucene.apache.org/ 安装solr,就是去部署它的war包,war包所在的位 ...

  5. Nutch搜索引擎(第2期)_ Solr简介及安装

    1.Solr简介 Solr是一个高性能,采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查询性能进行了优化 ...

  6. Solr使用初探——Solr的安装环境与配置

    Solr是一个apache名下很好用的开源索引.搜索工具,网上的资料虽多但很杂,笔者花了一天的时间对Solr进行了较为初步的研究,对Solr的基础应用做了一定的总结.文中涉及到的配置方法并不唯一,AP ...

  7. Nutch搜索引擎Solr简介及安装

    Nutch搜索引擎(第2期)_ Solr简介及安装   1.Solr简介 Solr是一个高性能,采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的 ...

  8. solr简介与安装

    solr简介: Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展,并对索引 ...

  9. solr window环境安装配置和管理页面基本使用

    solr介绍 来自官网http://lucene.apache.org/solr/解释: Solr is highly reliable, scalable and fault tolerant, p ...

随机推荐

  1. Sudoku Killer

    算法:深搜 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份 ...

  2. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  3. Win7下部署 .NET MVC网站 之 HTTP错误 403.14-Forbidden 解决方法

    今天在 IIS 7 发布MVC 站点时 遇到 ”HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容 “ 的错误提示. 一番折腾后发现在web.config 中加入 ...

  4. Android开源项目及库搜集

    TimLiu-Android 自己总结的Android开源项目及库. github排名 https://github.com/trending,github搜索:https://github.com/ ...

  5. python中os.walk()遍历目录中所有文件

    之前一直用判断目录和文件的递归方法来获取一个目录下的所有文件,后来发现python里面已经写好了这个函数,不需要自己递归获取了,记录下os.walk()函数的用法 目的:获取path下所有文件,返回由 ...

  6. A*八数码

    帮同学写的八数码,启发式搜索 创建两个表open,close,分别用的stl中的优先队列priority_queue和map,好久没写过代码了,bug调了半天 #include <iostrea ...

  7. SolrCloud初识

    文章摘自:http://www.bubuko.com/infodetail-923588.html 一.概述 Lucene是一个Java语言编写的利用倒排原理实现的文本检索类库: Solr是以Luce ...

  8. PYTHON线程知识再研习D---可重入锁

    不多解释,预防普通锁不正规的获取与释放 #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time class ...

  9. 各类XML parser的比较

    基于以上的比较 再为公司的项目选择解析器的时候,我选择Xerces.准备把Qt自带的XML库给去掉. references: http://stackoverflow.com/questions/17 ...

  10. 3D打印论坛

    3D打印论坛:http://www.3done.cn http://www.03dp.com www.qjxxw.net/ http://www.3ddayin.net http://oa.zol.c ...