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+"
");
}
if (isList){
for (String s : (ArrayList<String>)fValue){
System.out.print("fieldName:"+fName+" filedValue:"+String.valueOf(s)+"
");
}
}
}
}
}
博客同步地址:http://www.cnblogs.com/SEEKTHINKING/p/4228935.html