Solr学习笔记四--solrj的使用(更新文档)

来源:互联网 发布:英剧推荐知乎 编辑:程序博客网 时间:2024/05/24 00:14

在eclipse中通过solrj进行solr索引,要注意:

1,版本问题

2,包

自己测试了一个更新文档程序,可用

源代码:

package solrtest;import java.io.IOException;import java.net.MalformedURLException;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;import org.apache.solr.client.solrj.request.UpdateRequest;import org.apache.solr.client.solrj.response.UpdateResponse;import org.apache.solr.common.SolrInputDocument;public class test {    public static SolrServer getSolrServer() throws MalformedURLException{          //the instance can be reused          return new CommonsHttpSolrServer("http://localhost:8983/solr/");      }      public static void main(String[] args) throws SolrServerException, IOException {// TODO Auto-generated method stubSolrServer server = getSolrServer();SolrInputDocument document=new SolrInputDocument();document.addField("id", "lskyne");document.addField("title", "welcome to solr");server.add(document);server.commit();//UpdateRequest req =new UpdateRequest();//req.setAction(UpdateRequest.ACTION.COMMIT, false, false);//req.add(document);//UpdateResponse rsp =req.process(server);}}

结果:

进入http://localhost:8983/solr/admin/页面,输入查询条件:id:lskyne

<response><lst name="responseHeader"><int name="status">0</int><int name="QTime">1</int><lst name="params"><str name="indent">on</str><str name="start">0</str><str name="q">id:uid</str><str name="version">2.2</str><str name="rows">10</str></lst></lst><result name="response" numFound="1" start="0"><doc><str name="id">uid</str><arr name="title"><str>华中科技大学</str></arr></doc></result></response>



原创粉丝点击