|
相信很多人,在准备提交数据让solr建立索引的那刻,很纳闷,尽管看了不少网上的一些文章,但是我想依然还是有不少不理解的地方。
比如提交一个xml,采用post方式,尽管有些文章说了可以采用httpclient。但是我那个时候,还不是很理解,当然现在看来其实也没有 什么了。但是对于一个刚入门solr的初学者,我想讲讲关于solr1.3的 solrj( sorlr J 目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。)!
先上一个例子:
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20static%20final%20String%20SOLR_URL%20%3D%20%22http%3A%2F%2Flocalhost%2Fsolr%2Fcore0%22%3B%0Apublic%20static%20void%20commit()%20%7B%0A%09Date%20date%20%3D%20new%20Date()%3B%0A%09SolrServer%20solr%20%3D%20null%3B%0A%09try%20%7B%0A%09%09solr%20%3D%20new%20CommonsHttpSolrServer(SOLR_URL)%3B%0A%09%7D%20catch%20(MalformedURLException%20e1)%20%7B%0A%09%09e1.printStackTrace()%3B%0A%09%7D%0Afor%20(int%20i%20%3D%200%3B%20i%20%3C%2010000%3B%20i%2B%2B)%20%7B%0A%09%09%09SolrInputDocument%20sid%20%3D%20new%20SolrInputDocument()%3B%0A%09%09%09sid.addField(%22id%22%2C%20i)%3B%0A%09%09%09sid.addField(%22name%22%2C%20%22struts%2Bhibernate%2Bspring%20%E5%BC%80%E5%8F%91%E5%A4%A7%E5%85%A8%22%20%2B%20i)%3B%0A%09%09%09sid.addField(%22summary%22%2C%20%22%E4%B8%89%E7%A7%8D%E6%A1%86%E6%9E%B6%E7%9A%84%E7%BB%BC%E5%90%88%E5%BA%94%E7%94%A8%22%20%2B%20i)%3B%0A%09%09%09sid.addField(%22author%22%2C%20%22%E6%9D%8E%E8%89%AF%E6%9D%B0%22%20%2B%20i)%3B%0A%09%09%09sid.addField(%22date%22%2C%20new%20Date())%3B%0A%09%09%09sid.addField(%22content%22%2C%20%22%E9%AB%98%E7%BA%A7%E5%BA%94%E7%94%A8%E7%B1%BB%E4%B9%A6%E7%B1%8D%22%20%2B%20i)%3B%0A%09%09%09sid.addField(%22keywords%22%2C%20%22SSH%22%20%2B%20i)%3B%0A%09%09%09try%20%7B%0A%09%09%09%09solr.add(sid)%3B%0A%09%09%09%7D%20catch%20(MalformedURLException%20e)%20%7B%0A%09%09%09%09e.printStackTrace()%3B%0A%09%09%09%7D%20catch%20(SolrServerException%20e)%20%7B%0A%09%09%09%09e.printStackTrace()%3B%0A%09%09%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09%09%09e.printStackTrace()%3B%0A%09%09%09%7D%0A%09%09%09System.out.println(i)%3B%0A%09%09%09if%20(i%20%3D%3D%20999)%20%0A%09%09%09%09System.out.println((new%20Date().getTime()%20-%20date.getTime())%20%2F%2060000%20%2B%20%22%E5%88%86%E9%92%9F%22)%3B%0A%09%09%7D%0A%09%09try%20%7B%0A%09%09%09solr.commit()%3B%0A%09%09%7D%20catch%20(SolrServerException%20e)%20%7B%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%0A%7D
- public static final String SOLR_URL = "http://localhost/solr/core0";
- public static void commit() {
- Date date = new Date();
- SolrServer solr = null;
- try {
- solr = new CommonsHttpSolrServer(SOLR_URL);
- } catch (MalformedURLException e1) {
- e1.printStackTrace();
- }
- for (int i = 0; i < 10000; i++) {
- SolrInputDocument sid = new SolrInputDocument();
- sid.addField("id", i);
- sid.addField("name", "struts+hibernate+spring 开发大全" + i);
- sid.addField("summary", "三种框架的综合应用" + i);
- sid.addField("author", "李良杰" + i);
- sid.addField("date", new Date());
- sid.addField("content", "高级应用类书籍" + i);
- sid.addField("keywords", "SSH" + i);
- try {
- solr.add(sid);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println(i);
- if (i == 999)
- System.out.println((new Date().getTime() - date.getTime()) / 60000 + "分钟");
- }
- try {
- solr.commit();
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
上面这段代码的意思是:利用for提交10000个document,并打印提交10000所需的时间。
1》CommonsHttpSolrServer 使用HTTPClient 和solr服务器进行通信。
2》CommonsHttpSorlrServer 允许设置链接属性。
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=server.setSoTimeout(1000)%3B%20%20%2F%2F%20socket%20read%20timeout%20%20%0A%20%20server.setConnectionTimeout(100)%3B%20%20%0A%20%20server.setDefaultMaxConnectionsPerHost(100)%3B%20%20%0A%20%20server.setMaxTotalConnections(100)%3B%20%20%0A%20%20server.setFollowRedirects(false)%3B%20%20%2F%2F%20defaults%20to%20false%20%20%0A%20%20%2F%2F%20allowCompression%20defaults%20to%20false.%20%20%0A%20%20%2F%2F%20Server%20side%20must%20support%20gzip%20or%20deflate%20for%20this%20to%20have%20any%20effect.%20%20%0A%20%20server.setAllowCompression(true)%3B%20%20%0A%20%20server.setMaxRetries(1)%3B%20%2F%2F%20defaults%20to%200.%20%20%3E%201%20not%20recommended.
- server.setSoTimeout(1000); // socket read timeout
- server.setConnectionTimeout(100);
- server.setDefaultMaxConnectionsPerHost(100);
- server.setMaxTotalConnections(100);
- server.setFollowRedirects(false); // defaults to false
- // allowCompression defaults to false.
- // Server side must support gzip or deflate for this to have any effect.
- server.setAllowCompression(true);
- server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
3》实现SolrServer接口的另一个类:EmbeddedSorrServer,它不需要http连接。
4》在构造document的时候,可以一个一个添加到solrServer,也可以构建一个包含document的Collection,将Collection添加到solrServer,然后commit。
5》也可以构造一个跟document匹配的JavaBean进行提交
使用 java 注释创建java bean。@Field ,可以被用在域上,或者是setter方法上。如果一个域的名称跟bean的名称是不一样的,那么在java注释中填写别名,具体的,可以参照下面的域categories
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=import%20org.apache.solr.client.solrj.beans.Field%3B%20%20%0A%20public%20class%20Item%20%7B%20%20%0A%20%20%20%20%40Field%20%20%0A%20%20%20%20String%20id%3B%20%20%0A%20%20%20%20%40Field(%22cat%22)%20%20%0A%20%20%20%20String%5B%5D%20categories%3B%20%20%20%0A%20%20%20%20%40Field%20%20%0A%20%20%20%20List%3CString%3E%20features%3B%20%20%20%0A%20%20%7D%20%20
- import org.apache.solr.client.solrj.beans.Field;
- public class Item {
- @Field
- String id;
- @Field("cat")
- String[] categories;
- @Field
- List features;
- }
import org.apache.solr.client.solrj.beans.Field; public class Item { @Field String id; @Field("cat") String[] categories; @Field List features; }
java注释也可以使用在setter方法上,如下面的例子:
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%40Field(%22cat%22)%20%20%0A%20public%20void%20setCategory(String%5B%5D%20c)%7B%20%20%0A%20%20%20%20%20this.categories%20%3D%20c%3B%20%20%0A%20%7D
- @Field("cat")
- public void setCategory(String[] c){
- this.categories = c;
- }
@Field("cat") public void setCategory(String[] c){ this.categories = c; }
这里应该要有一个相对的,get方法(没有加java注释的)来读取属性
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=Item%20item%20%3D%20new%20Item()%3B%20%20%0Aitem.id%20%3D%20%22one%22%3B%20%20%0Aitem.categories%20%3D%20%20new%20String%5B%5D%20%7B%20%22aaa%22%2C%20%22bbb%22%2C%20%22ccc%22%20%7D%3B%20
- Item item = new Item();
- item.id = "one";
- item.categories = new String[] { "aaa", "bbb", "ccc" };
Item item = new Item(); item.id = "one"; item.categories = new String[] { "aaa", "bbb", "ccc" };
添加给solr
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=server.addBean(item)%3B将多个bean提交给solr
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=List%3CItem%3E%20beans%20%3B%20%20%0A%2F%2Fadd%20Item%20objects%20to%20the%20list%20%20%0Aserver.addBeans(beans)%3B
- List beans ;
- //add Item objects to the list
- server.addBeans(beans);
List beans ; //add Item objects to the list server.addBeans(beans);
注意: 你可以重复使用SolrServer,这样可以提高性能。
6》
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20static%20void%20update()%20%7B%0A%09SolrServer%20solrServer%20%3D%20null%3B%0A%09try%20%7B%0A%09%09solrServer%20%3D%20new%20CommonsHttpSolrServer(SOLR_URL)%3B%0A%09%7D%20catch%20(MalformedURLException%20e)%20%7B%0A%09%09e.printStackTrace()%3B%0A%09%7D%0A%09UpdateRequest%20updateRequest%20%3D%20new%20UpdateRequest()%3B%0A%09SolrInputDocument%20sid%20%3D%20new%20SolrInputDocument()%3B%0A%09sid.addField(%22id%22%2C%20100000)%3B%0A%09sid.addField(%22name%22%2C%20%22struts%2Bhibernate%2Bspring%20%E5%BC%80%E5%8F%91%E5%A4%A7%E5%85%A8%22)%3B%0A%09sid.addField(%22summary%22%2C%20%22%E4%B8%89%E7%A7%8D%E6%A1%86%E6%9E%B6%E7%9A%84%E7%BB%BC%E5%90%88%E5%BA%94%E7%94%A8%22)%3B%0A%09sid.addField(%22author%22%2C%20%22%E6%9D%8E%E8%89%AF%E6%9D%B0%22)%3B%0A%09sid.addField(%22date%22%2C%20new%20Date())%3B%0A%09sid.addField(%22content%22%2C%20%22%E9%AB%98%E7%BA%A7%E5%BA%94%E7%94%A8%E7%B1%BB%E4%B9%A6%E7%B1%8D%22)%3B%0A%09sid.addField(%22keywords%22%2C%20%22SSH%22)%3B%0A%09updateRequest.setAction(UpdateRequest.ACTION.COMMIT%2C%20false%2C%20false)%3B%20%20%0A%09updateRequest.add(sid)%3B%20%20%0A%09try%20%7B%0A%09%09UpdateResponse%20updateResponse%20%3D%20updateRequest.process(solrServer)%3B%0A%09%09System.out.println(updateResponse.getStatus())%3B%0A%09%7D%20catch%20(SolrServerException%20e)%20%7B%0A%09%09e.printStackTrace()%3B%0A%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09e.printStackTrace()%3B%0A%09%7D%0A%7D
- public static void update() {
- SolrServer solrServer = null;
- try {
- solrServer = new CommonsHttpSolrServer(SOLR_URL);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- UpdateRequest updateRequest = new UpdateRequest();
- SolrInputDocument sid = new SolrInputDocument();
- sid.addField("id", 100000);
- sid.addField("name", "struts+hibernate+spring 开发大全");
- sid.addField("summary", "三种框架的综合应用");
- sid.addField("author", "李良杰");
- sid.addField("date", new Date());
- sid.addField("content", "高级应用类书籍");
- sid.addField("keywords", "SSH");
- updateRequest.setAction(UpdateRequest.ACTION.COMMIT, false, false);
- updateRequest.add(sid);
- try {
- UpdateResponse updateResponse = updateRequest.process(solrServer);
- System.out.println(updateResponse.getStatus());
- } catch (SolrServerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
public static void update() { SolrServer solrServer = null; try { solrServer = new CommonsHttpSolrServer(SOLR_URL); } catch (MalformedURLException e) { e.printStackTrace(); } UpdateRequest updateRequest = new UpdateRequest(); SolrInputDocument sid = new SolrInputDocument(); sid.addField("id", 100000); sid.addField("name", "struts+hibernate+spring 开发大全"); sid.addField("summary", "三种框架的综合应用"); sid.addField("author", "李良杰"); sid.addField("date", new Date()); sid.addField("content", "高级应用类书籍"); sid.addField("keywords", "SSH"); updateRequest.setAction(UpdateRequest.ACTION.COMMIT, false, false); updateRequest.add(sid); try { UpdateResponse updateResponse = updateRequest.process(solrServer); System.out.println(updateResponse.getStatus()); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
提交一个document,采用更新方式,注意:
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=updateRequest.setAction(UpdateRequest.ACTION.COMMIT%2C%20false%2C%20false)%3B
- updateRequest.setAction(UpdateRequest.ACTION.COMMIT, false, false);
7》
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20static%20void%20query()%20%7B%0A%09SolrServer%20solr%20%3D%20null%3B%0A%09try%20%7B%0A%09%09solr%20%3D%20new%20CommonsHttpSolrServer(SOLR_URL)%3B%0A%09%7D%20catch%20(MalformedURLException%20e)%20%7B%0A%09%09e.printStackTrace()%3B%0A%09%09return%3B%0A%09%7D%0A%09%2F%2F%20http%3A%2F%2Flocalhost%3A8983%2Fsolr%2FspellCheckCompRH%3Fq%3Depod%26spellcheck%3Don%26spellcheck.build%3Dtrue%0A%09ModifiableSolrParams%20params%20%3D%20new%20ModifiableSolrParams()%3B%0A%09params.set(%22qt%22%2C%20%22%2FspellCheckCompRH%22)%3B%0A%09params.set(%22q%22%2C%20%22%E7%BC%96%E7%A8%8B%22)%3B%0A%09params.set(%22spellcheck%22%2C%20%22on%22)%3B%0A%09params.set(%22spellcheck.build%22%2C%20%22true%22)%3B%0A%09QueryResponse%20response%20%3D%20null%3B%0A%09try%20%7B%0A%09%09response%20%3D%20solr.query(params)%3B%0A%09%7D%20catch%20(SolrServerException%20e)%20%7B%0A%09%09e.printStackTrace()%3B%0A%09%09return%3B%0A%09%7D%0A%09System.out.println(%22response%20%3D%20%22%20%2B%20response)%3B%0A%7D
- public static void query() {
- SolrServer solr = null;
- try {
- solr = new CommonsHttpSolrServer(SOLR_URL);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- return;
- }
- // http://localhost:8983/solr/spellCheckCompRH?q=epod&spellcheck=on&spellcheck.build=true
- ModifiableSolrParams params = new ModifiableSolrParams();
- params.set("qt", "/spellCheckCompRH");
- params.set("q", "编程");
- params.set("spellcheck", "on");
- params.set("spellcheck.build", "true");
- QueryResponse response = null;
- try {
- response = solr.query(params);
- } catch (SolrServerException e) {
- e.printStackTrace();
- return;
- }
- System.out.println("response = " + response);
- }
public static void query() { SolrServer solr = null; try { solr = new CommonsHttpSolrServer(SOLR_URL); } catch (MalformedURLException e) { e.printStackTrace(); return; } // http://localhost:8983/solr/spellCheckCompRH?q=epod&spellcheck=on&spellcheck.build=true ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/spellCheckCompRH"); params.set("q", "编程"); params.set("spellcheck", "on"); params.set("spellcheck.build", "true"); QueryResponse response = null; try { response = solr.query(params); } catch (SolrServerException e) { e.printStackTrace(); return; } System.out.println("response = " + response); }
这是一个查询方法。关键字:“编程”。关于查询的关键字,请参见slor wiki http://wiki.apache.org/solr/QueryParametersIndex,或等待我的博客更新,在后面会有篇文章详细讲这个问题!
8》给solr的索引文件手动进行优化,
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=solr.optimize()%3Bsolr.optimize();
9》solrJ 提供了一组API,来帮助我们创建查询,下面是一个faceted query的例子。
Java代码
Java代码 http://lianj-lee.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=SolrServer%20server%20%3D%20getSolrServer()%3B%20%20%0ASolrQuery%20solrQuery%20%3D%20new%20%20SolrQuery().setQuery(%22ipod%22).setFacet(true).setFacetMinCount(1).setFacetLimit(8).%20%20%20%20%20%20addFacetField(%22category%22).addFacetField(%22inStock%22)%3B%20%20%20%20%0AQueryResponse%20rsp%20%3D%20server.query(solrQuery)%3B
- SolrServer server = getSolrServer();
- SolrQuery solrQuery = new SolrQuery().setQuery("ipod").setFacet(true).setFacetMinCount(1).setFacetLimit(8). addFacetField("category").addFacetField("inStock");
- QueryResponse rsp = server.query(solrQuery);
SolrServer server = getSolrServer(); SolrQuery solrQuery = new SolrQuery().setQuery("ipod").setFacet(true).setFacetMinCount(1).setFacetLimit(8). addFacetField("category").addFacetField("inStock"); QueryResponse rsp = server.query(solrQuery);
所有的 setter/add 方法都是返回它自己本身的实例,所以就像你所看到的一样,上面的用法是链式的。 |
|
|