分两部分:1.自己写的代码实现,2.在网上找的实现方式。
1. 自己的代码
启动solr集群,有两种方法连接solr。
1) 使用之前连接单台的方法
之前的代码中是配置的某一台slr,如:
可将101改为102,103都可以连接成功。
2) 连接集群zk
public void query_cloud_id() throws SolrServerException, IOException{
String zkHost = "192.168.75.101:2181,192.168.75.102:2181,192.168.75.103:2181";
CloudSolrClient cloudSolrClient = new CloudSolrClient(zkHost);
cloudSolrClient.setDefaultCollection("oldharvewifi");
cloudSolrClient.setZkClientTimeout(3000);
cloudSolrClient.setZkConnectTimeout(3000);
cloudSolrClient.connect();
String id = "21ba9624-7d65-41f3-896f-0b4b24728d9a";
String queryStr = "id:"+id;
SolrQuery query = new SolrQuery(queryStr);
QueryResponse rsp = cloudSolrClient.query(query);
// cloudSolrClient.optimize();
// 获取结果集
SolrDocumentList docs = rsp.getResults();
Long count = docs.getNumFound();//搜索结果数量
Iterator<SolrDocument> iter = docs.iterator();
while (iter.hasNext()) {
SolrDocument doc = iter.next();//SolrDocument 返回搜索数据的map
if (doc.getFieldValueMap() != null && doc.getFieldValueMap().size() > 0) {
System.out.println(doc.getFieldValueMap());
}
}
}
2. 网上找的代码
mavan 项目依赖(注意solr 的版本)
[html] view plain copy
1. <properties>
2. <solr_version>5.5.3</solr_version>
3. <slf4j_version>1.6.6</slf4j_version>
4. <log4j_version>1.2.16</log4j_version>
5. <jcl_version>1.1</jcl_version>
6. </properties>
7.
8. <dependencies>
9. <dependency>
10. <groupId>org.apache.zookeeper</groupId>
11. <artifactId>zookeeper</artifactId>
12. <version>3.4.8</version>
13. </dependency>
14.
15. <!-- solr -->
16. <dependency>
17.
18. <groupId>org.apache.solr</groupId>
19. <artifactId>solr-solrj</artifactId>
20. <version>${solr_version}</version>
21. <exclusions><!-- 去除依赖重复 -->
22. <exclusion>
23. <groupId>org.eclipse.jetty.orbit</groupId>
24. <artifactId>javax.servlet</artifactId>
25. </exclusion>
26. </exclusions>
27. </dependency>
28. <dependency>
29. <groupId>org.apache.solr</groupId>
30. <artifactId>solr-core</artifactId>
31. <version>${solr_version}</version>
32. <exclusions>
33. <exclusion>
34. <groupId>jdk.tools</groupId>
35. <artifactId>jdk.tools</artifactId>
36. </exclusion>
37. <exclusion>
38. <groupId>org.restlet.jee</groupId>
39. <artifactId>org.restlet</artifactId>
40. </exclusion>
41. <exclusion>
42. <groupId>org.restlet.jee</groupId>
43. <artifactId>org.restlet.ext.servlet</artifactId>
44. </exclusion>
45. <exclusion>
46. <groupId>org.eclipse.jetty.orbit</groupId>
47. <artifactId>javax.servlet</artifactId>
48. </exclusion>
49. </exclusions>
50. </dependency>
51.
52. <!-- log -->
53. <dependency>
54. <groupId>log4j</groupId>
55. <artifactId>log4j</artifactId>
56. <version>${log4j_version}</version>
57. </dependency>
58. <dependency>
59. <groupId>org.slf4j</groupId>
60. <artifactId>slf4j-api</artifactId>
61. <version>${slf4j_version}</version>
62. </dependency>
63. <dependency>
64. <groupId>org.slf4j</groupId>
65. <artifactId>slf4j-log4j12</artifactId>
66. <version>${slf4j_version}</version>
67. </dependency>
68. <dependency>
69. <groupId>commons-logging</groupId>
70. <artifactId>commons-logging-api</artifactId>
71. <version>${jcl_version}</version>
72. </dependency>
73. </dependencies>
java 代码:
[java] view plain copy
1. //店铺集合名称
2. private static final String SHOP_COLLECTION_NAME = "shop-collection";
3. private static final String GOODSINFO_COLLECTION_NAME = "goodsInfo-collection";
4.
5. public static void main(String[] args) throws Exception {
6. String zkHost = "192.168.172.128:2181,192.168.172.129:2181,192.168.172.130:2181";
7. CloudSolrClient cloudSolrClient = new CloudSolrClient(zkHost);
8.
9. cloudSolrClient.setDefaultCollection(SHOP_COLLECTION_NAME);
10. cloudSolrClient.setZkClientTimeout(3000);
11. cloudSolrClient.setZkConnectTimeout(3000);
12. cloudSolrClient.connect();
13.
14. System.out.println("-----------搜索-------------");
15. searchShop(cloudSolrClient, "家电");
16.
17. System.out.println("-----------添加索引-------------");
18. addShopIndex(cloudSolrClient, 100L, 111000L, "幸福家电");
19. searchShop(cloudSolrClient, "家电");
20.
21.
22. System.out.println("-----------修改索引-------------");
23. addShopIndex(cloudSolrClient, 100L, 111000L, "家电甩卖");
24. searchShop(cloudSolrClient, "家电");
25.
26. System.out.println("-----------删除索引-------------");
27. deleteShopIndex(cloudSolrClient, 111000L);
28. searchShop(cloudSolrClient, "家电");
29.
30. cloudSolrClient.close();
31. }
32.
33. //查询商铺
34. private static void searchShop(CloudSolrClient cloudSolrClient, String shopName) throws Exception {
35. SolrQuery query = new SolrQuery();
36. query.setQuery("shop_name:" + shopName);
37. QueryResponse rsp = cloudSolrClient.query(query);
38. cloudSolrClient.optimize();
39. // 获取结果集
40. SolrDocumentList docs = rsp.getResults();
41. Long count = docs.getNumFound();//搜索结果数量
42. Iterator<SolrDocument> iter = docs.iterator();
43. while (iter.hasNext()) {
44. SolrDocument doc = iter.next();//SolrDocument 返回搜索数据的map
45. if (doc.getFieldValueMap() != null && doc.getFieldValueMap().size() > 0) {
46. System.out.println(doc.getFieldValueMap());
47. }
48. }
49. }
50.
51. //删除店铺索引
52. private static void deleteShopIndex(CloudSolrClient cloudSolrClient, Long shopId) throws Exception {
53. cloudSolrClient.deleteById(shopId+"");
54. cloudSolrClient.commit();
55.
56. }
57.
58. //添加shop索引
59. private static void addShopIndex(CloudSolrClient cloudSolrClient, Long mainUserId, Long shopId, String shopName) throws Exception {
60. SolrInputDocument document = new SolrInputDocument();
61. document.addField("main_user_id",mainUserId);
62. document.addField("id",shopId);
63. document.addField("shop_name",shopName);
64. cloudSolrClient.add(document);
65. cloudSolrClient.commit();
66. }
打印结果:
-----------搜索-------------
-----------添加索引-------------
{main_user_id=100, id=111000, shop_name=幸福家电, _version_=1552538842666369024}
-----------修改索引-------------
{main_user_id=100, id=111000, shop_name=家电甩卖, _version_=1552538842764935168}
-----------删除索引-------------
说明:第一次查询没有结果,添加一次索引后,可以搜索到“幸福家电”,修改后再次查询可以查到只有shop_name 和_version 变化了,根据id删除后就搜索不到“家电”关键字了。