solr 7.1.0中solrJ的使用

来源:互联网 发布:ubuntu的网卡配置文件 编辑:程序博客网 时间:2024/05/16 12:36

参考文档:http://blog.csdn.net/frankcheng5143/article/details/52506948

http://www.cnblogs.com/doit8791/p/4902571.html


前提准备,需要安装并配置有maven,教程网上有很多,而且不麻烦。

1.在MyEclipse中新建web project,注意选择java version要和solr版本相匹配,勾选add maven support。然后一路向下,直到完成创建。


2. 将下载的solr包中的dist/solrj-lib目录下的如下jar包拷贝到 步骤1 中新建的web项目中的WebRoot/WEB-INF/lib 目录中。


3.  将下面内容加入到pom文件中

  1. <dependency>
  2. <groupId>org.apache.solr</groupId>
  3. <artifactId>solr-solrj</artifactId>
  4. <version>5.3.1</version>
  5. </dependency>
4. 编写代码测试

  • 初始化SolrClient对象
  1. //方法一:直接指定solr的URL和core1,只能查询或更新core1内容
  2. SolrClient client = newHttpSolrClient("http://my-solr-server:8983/solr/core1");
  3. QueryResponse resp = client.query(newSolrQuery("*:*"));

  4. //方法二:指定solr的URL,查询或更新时要指定core
  5. SolrClient client = newHttpSolrClient("http://my-solr-server:8983/solr");
  6. QueryResponse resp = client.query("core1",newSolrQuery("*:*"));
//查询出来的结果都保存在SolrDocumentList中
SolrDocumentList sdl = resp.getResults();
System.out.println("总数"+ sdl.getNumFound() );
for (SolrDocument sd: sdl)
System.out.println("id = "+sd.get("id")+"--name = "+sd.get("name"));


原创粉丝点击