use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class CloudSolrClientTest method queryWithPreferLocalShards.
private void queryWithPreferLocalShards(CloudSolrClient cloudClient, boolean preferLocalShards, String collectionName) throws Exception {
SolrQuery qRequest = new SolrQuery("*:*");
ModifiableSolrParams qParams = new ModifiableSolrParams();
qParams.add(CommonParams.PREFER_LOCAL_SHARDS, Boolean.toString(preferLocalShards));
qParams.add(ShardParams.SHARDS_INFO, "true");
qRequest.add(qParams);
// CloudSolrClient sends the request to some node.
// And since all the nodes are hosting cores from all shards, the
// distributed query formed by this node will select cores from the
// local shards only
QueryResponse qResponse = cloudClient.query(collectionName, qRequest);
Object shardsInfo = qResponse.getResponse().get(ShardParams.SHARDS_INFO);
assertNotNull("Unable to obtain " + ShardParams.SHARDS_INFO, shardsInfo);
// Iterate over shards-info and check what cores responded
SimpleOrderedMap<?> shardsInfoMap = (SimpleOrderedMap<?>) shardsInfo;
Iterator<Map.Entry<String, ?>> itr = shardsInfoMap.asMap(100).entrySet().iterator();
List<String> shardAddresses = new ArrayList<String>();
while (itr.hasNext()) {
Map.Entry<String, ?> e = itr.next();
assertTrue("Did not find map-type value in " + ShardParams.SHARDS_INFO, e.getValue() instanceof Map);
String shardAddress = (String) ((Map) e.getValue()).get("shardAddress");
assertNotNull(ShardParams.SHARDS_INFO + " did not return 'shardAddress' parameter", shardAddress);
shardAddresses.add(shardAddress);
}
log.info("Shards giving the response: " + Arrays.toString(shardAddresses.toArray()));
// Make sure the distributed queries were directed to a single node only
if (preferLocalShards) {
Set<Integer> ports = new HashSet<Integer>();
for (String shardAddr : shardAddresses) {
URL url = new URL(shardAddr);
ports.add(url.getPort());
}
// This assertion would hold true as long as every shard has a core on each node
assertTrue("Response was not received from shards on a single node", shardAddresses.size() > 1 && ports.size() == 1);
}
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class SolrExampleJettyTest method testArbitraryJsonIndexing.
@Test
public void testArbitraryJsonIndexing() throws Exception {
HttpSolrClient client = (HttpSolrClient) getSolrClient();
client.deleteByQuery("*:*");
client.commit();
// make sure it got in
assertNumFound("*:*", 0);
// two docs, one with uniqueKey, another without it
String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
HttpClient httpClient = client.getHttpClient();
HttpPost post = new HttpPost(getUri(client));
post.setHeader("Content-Type", "application/json");
post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
assertEquals(200, response.getStatusLine().getStatusCode());
client.commit();
QueryResponse rsp = getSolrClient().query(new SolrQuery("*:*"));
assertEquals(2, rsp.getResults().getNumFound());
SolrDocument doc = rsp.getResults().get(0);
String src = (String) doc.getFieldValue("_src_");
Map m = (Map) ObjectBuilder.fromJSON(src);
assertEquals("abc1", m.get("id"));
assertEquals("name1", m.get("name"));
doc = rsp.getResults().get(1);
src = (String) doc.getFieldValue("_src_");
m = (Map) ObjectBuilder.fromJSON(src);
assertEquals("name2", m.get("name"));
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class TestSolrProperties method testProperties.
@Test
public void testProperties() throws Exception {
UpdateRequest up = new UpdateRequest();
up.setAction(ACTION.COMMIT, true, true);
up.deleteByQuery("*:*");
up.process(getSolrCore0());
up.process(getSolrCore1());
up.clear();
// Add something to each core
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", "AAA");
doc.setField("core0", "yup stopfra stopfrb stopena stopenb");
// Add to core0
up.add(doc);
up.process(getSolrCore0());
SolrTestCaseJ4.ignoreException("unknown field");
// You can't add it to core1
try {
up.process(getSolrCore1());
fail("Can't add core0 field to core1!");
} catch (Exception ex) {
}
// Add to core1
doc.setField("id", "BBB");
doc.setField("core1", "yup stopfra stopfrb stopena stopenb");
doc.removeField("core0");
up.add(doc);
up.process(getSolrCore1());
// You can't add it to core1
try {
SolrTestCaseJ4.ignoreException("core0");
up.process(getSolrCore0());
fail("Can't add core1 field to core0!");
} catch (Exception ex) {
}
SolrTestCaseJ4.resetExceptionIgnores();
// now Make sure AAA is in 0 and BBB in 1
SolrQuery q = new SolrQuery();
QueryRequest r = new QueryRequest(q);
q.setQuery("id:AAA");
assertEquals(1, r.process(getSolrCore0()).getResults().size());
assertEquals(0, r.process(getSolrCore1()).getResults().size());
// Now test Changing the default core
assertEquals(1, getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
assertEquals(0, getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());
assertEquals(0, getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
assertEquals(1, getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());
// Now test reloading it should have a newer open time
String name = "core0";
SolrClient coreadmin = getSolrAdmin();
CoreAdminResponse mcr = CoreAdminRequest.getStatus(name, coreadmin);
long before = mcr.getStartTime(name).getTime();
CoreAdminRequest.reloadCore(name, coreadmin);
mcr = CoreAdminRequest.getStatus(name, coreadmin);
long after = mcr.getStartTime(name).getTime();
assertTrue("should have more recent time: " + after + "," + before, after > before);
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class BasicHttpSolrClientTest method testRedirect.
@Test
public void testRedirect() throws Exception {
final String clientUrl = jetty.getBaseUrl().toString() + "/redirect/foo";
try (HttpSolrClient client = getHttpSolrClient(clientUrl)) {
SolrQuery q = new SolrQuery("*:*");
// default = false
try {
client.query(q);
fail("Should have thrown an exception.");
} catch (SolrServerException e) {
assertTrue(e.getMessage().contains("redirect"));
}
client.setFollowRedirects(true);
client.query(q);
//And back again:
client.setFollowRedirects(false);
try {
client.query(q);
fail("Should have thrown an exception.");
} catch (SolrServerException e) {
assertTrue(e.getMessage().contains("redirect"));
}
}
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class BasicHttpSolrClientTest method testCollectionParameters.
@Test
public void testCollectionParameters() throws IOException, SolrServerException {
try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString())) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "collection");
client.add("collection1", doc);
client.commit("collection1");
assertEquals(1, client.query("collection1", new SolrQuery("id:collection")).getResults().getNumFound());
}
final String collection1Url = jetty.getBaseUrl().toString() + "/collection1";
try (HttpSolrClient client = getHttpSolrClient(collection1Url)) {
assertEquals(1, client.query(new SolrQuery("id:collection")).getResults().getNumFound());
}
}
Aggregations