solr 7.1.0中solrJ的使用
参考文档:http://blog.****.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文件中
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.3.1</version>
</dependency>
- 初始化SolrClient对象
//方法一:直接指定solr的URL和core1,只能查询或更新core1内容
SolrClient client = newHttpSolrClient("http://my-solr-server:8983/solr/core1");
QueryResponse resp = client.query(newSolrQuery("*:*"));
//方法二:指定solr的URL,查询或更新时要指定core
SolrClient client = newHttpSolrClient("http://my-solr-server:8983/solr");
QueryResponse resp = client.query("core1",new SolrQuery("*:*"));
SolrDocumentList sdl = resp.getResults();
System.out.println("总数"+ sdl.getNumFound() );
for (SolrDocument sd: sdl)
System.out.println("id = "+sd.get("id")+"--name = "+sd.get("name"));