/
ShardUnificationTest.java
139 lines (121 loc) · 4.43 KB
/
ShardUnificationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package de.cheffe.solrsample;
import de.cheffe.solrsample.rule.JettySolrServerResource;
import org.apache.commons.io.IOUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.Field;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URL;
import java.util.List;
/**
* <p>
* Shows how to query two Solr cores similar to a SQL union statement, when both
* cores differ in terms of existing fields.
* </p>
* <p>
* The configuration files of the Solr instance and their cores can be found
* under
* <ul>
* <li>src/main/resources/solr
* <li>src/main/resources/solr/shard-1
* <li>src/main/resources/solr/shard-2
* <li>src/main/resources/solr/shard-unification
* </ul>
* </p>
*
* @author cheffe
*/
public class ShardUnificationTest {
private static final Logger LOG = LoggerFactory.getLogger(ShardUnificationTest.class);
// start a jetty server, so that we can use solr's shard feature
// set the unification shard as default core
@ClassRule
public static JettySolrServerResource<UnifiedDocument> solr = new JettySolrServerResource<>("shard-unification");
@BeforeClass
public static void setupShards() throws SolrServerException, IOException {
// add a document to the core shard-1
solr.addBeanToIndex(new UnifiedDocument(1, "title 1", null, "description 1"), "shard-1");
// add another document to the core shard-2
solr.addBeanToIndex(new UnifiedDocument(2, "title 2", "abstract 2", null), "shard-2");
}
@Test
public void queryForUnifiedSchema() {
LOG.info("starting test queryForUnifiedSchema");
// check that the core shard-unification itself is empty
Assert.assertEquals(0, solr.query("*:*").getResults().getNumFound());
// fetch all docs via *:* query, so the two added docs should be fetched
// now add the two cores that hold data via the shards param
SolrQuery tmpQuery = new SolrQuery("*:*");
tmpQuery.set("shards", "localhost:8080/solr/shard-1,localhost:8080/solr/shard-2");
tmpQuery.set("indent", true);
List<UnifiedDocument> response = solr.query(tmpQuery, UnifiedDocument.class);
// assure that we got two docs
Assert.assertEquals(2, response.size());
// check that the attributes are set
boolean tmpCheckedDoc1 = false;
boolean tmpCheckedDoc2 = false;
for (UnifiedDocument unifiedDoc : response) {
if (unifiedDoc.id == 1) {
Assert.assertEquals("title 1", unifiedDoc.title);
Assert.assertEquals(null, unifiedDoc.abstractText);
Assert.assertEquals("description 1", unifiedDoc.description);
Assert.assertEquals("localhost:8080/solr/shard-1", unifiedDoc.shard);
Assert.assertFalse(tmpCheckedDoc1);
tmpCheckedDoc1 = true;
} else if (unifiedDoc.id == 2) {
Assert.assertEquals("title 2", unifiedDoc.title);
Assert.assertEquals("abstract 2", unifiedDoc.abstractText);
Assert.assertEquals(null, unifiedDoc.description);
Assert.assertFalse(tmpCheckedDoc2);
Assert.assertEquals("localhost:8080/solr/shard-2", unifiedDoc.shard);
tmpCheckedDoc2 = true;
} else {
Assert.fail("unexpected doc");
}
}
}
@Test
public void printResponseAsXML() throws IOException {
SolrQuery query = new SolrQuery("*:*");
// set indent == true, so that the xml output is formatted
query.set("indent", true);
// use org.apache.solr.client.solrj.util.ClientUtils
// to make a URL compatible query string of your SolrQuery
String urlQueryString = ClientUtils.toQueryString(query, false);
String solrURL = "http://localhost:8080/solr/shard-1/select";
URL url = new URL(solrURL + urlQueryString);
// use org.apache.commons.io.IOUtils to do the http handling for you
String xmlResponse = IOUtils.toString(url);
// have a look
System.out.println(xmlResponse);
}
public static class UnifiedDocument {
@Field
public int id;
@Field
public String title;
@Field
public String abstractText;
@Field
public String description;
@Field("[shard]")
public String shard;
public UnifiedDocument() {
super();
}
public UnifiedDocument(int id, String title, String abstractText, String description) {
super();
this.id = id;
this.title = title;
this.abstractText = abstractText;
this.description = description;
}
}
}