本文整理匯總了Java中org.apache.solr.client.solrj.impl.HttpSolrServer.query方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpSolrServer.query方法的具體用法?Java HttpSolrServer.query怎麽用?Java HttpSolrServer.query使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.solr.client.solrj.impl.HttpSolrServer
的用法示例。
在下文中一共展示了HttpSolrServer.query方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
public static void main(String[] args) throws MalformedURLException, SolrServerException {
HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
// ModifiableSolrParams params = new ModifiableSolrParams();
// params.setQuery("name:Samsung ");
// params.setStart(0);
// params.setRows(100);
SolrQuery params = new SolrQuery();
params.setQuery("*:*");
params.setSort("score ",ORDER.desc);
params.setStart(Integer.getInteger("0"));
params.setRows(Integer.getInteger("100"));
QueryResponse response = solr.query(params);
SolrDocumentList results = response.getResults();
for (int i = 0; i < results.size(); ++i) {
System.out.println(results.get(i));
}
}
示例2: waitForNon403or404or503
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
public static void waitForNon403or404or503(HttpSolrServer collectionClient)
throws Exception {
SolrException exp = null;
long timeoutAt = System.currentTimeMillis() + 30000;
while (System.currentTimeMillis() < timeoutAt) {
boolean missing = false;
try {
collectionClient.query(new SolrQuery("*:*"));
} catch (SolrException e) {
if (!(e.code() == 403 || e.code() == 503 || e.code() == 404)) {
throw e;
}
exp = e;
missing = true;
}
if (!missing) {
return;
}
Thread.sleep(50);
}
fail("Could not find the new collection - " + exp.code() + " : " + collectionClient.getBaseURL());
}
示例3: testSimple
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
@Test
public void testSimple() throws Exception {
DirectXmlRequest req = new DirectXmlRequest("/dataimport", xml);
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("command", "full-import");
params.set("clean", "false");
req.setParams(params);
HttpSolrServer solrServer = new HttpSolrServer(buildUrl(jetty.getLocalPort(), "/solr"));
solrServer.request(req);
ModifiableSolrParams qparams = new ModifiableSolrParams();
qparams.add("q", "*:*");
QueryResponse qres = solrServer.query(qparams);
SolrDocumentList results = qres.getResults();
assertEquals(2, results.getNumFound());
SolrDocument doc = results.get(0);
assertEquals("1", doc.getFieldValue("id"));
assertEquals("Hello C1", ((List)doc.getFieldValue("desc")).get(0));
solrServer.shutdown();
}
示例4: testCommitWithin
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
@Test
public void testCommitWithin() throws Exception {
DirectXmlRequest req = new DirectXmlRequest("/dataimport", xml);
ModifiableSolrParams params = params("command", "full-import",
"clean", "false", UpdateParams.COMMIT, "false",
UpdateParams.COMMIT_WITHIN, "1000");
req.setParams(params);
HttpSolrServer solrServer = new HttpSolrServer(buildUrl(jetty.getLocalPort(), "/solr"));
solrServer.request(req);
Thread.sleep(100);
ModifiableSolrParams queryAll = params("q", "*");
QueryResponse qres = solrServer.query(queryAll);
SolrDocumentList results = qres.getResults();
assertEquals(0, results.getNumFound());
Thread.sleep(1000);
for (int i = 0; i < 10; i++) {
qres = solrServer.query(queryAll);
results = qres.getResults();
if (2 == results.getNumFound()) {
solrServer.shutdown();
return;
}
Thread.sleep(500);
}
fail("Commit should have occured but it did not");
}
示例5: checkConsistency
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
private void checkConsistency(String replicatedCollection)
throws SolrServerException {
Collection<Slice> slices = cloudClient.getZkStateReader().getClusterState()
.getSlices(replicatedCollection);
for (Slice slice : slices) {
Collection<Replica> replicas = slice.getReplicas();
long found = -1;
for (Replica replica : replicas) {
HttpSolrServer client = new HttpSolrServer(
new ZkCoreNodeProps(replica).getCoreUrl());
SolrQuery query = new SolrQuery("*:*");
query.set("distrib", false);
QueryResponse replicaResults = client.query(query);
long count = replicaResults.getResults().getNumFound();
if (found != -1) {
assertEquals(slice.getName() + " is inconsistent "
+ new ZkCoreNodeProps(replica).getCoreUrl(), found, count);
}
found = count;
client.shutdown();
}
}
}
示例6: checkSubShardConsistency
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
protected void checkSubShardConsistency(String shard) throws SolrServerException {
SolrQuery query = new SolrQuery("*:*").setRows(1000).setFields("id", "_version_");
query.set("distrib", false);
ClusterState clusterState = cloudClient.getZkStateReader().getClusterState();
Slice slice = clusterState.getSlice(AbstractDistribZkTestBase.DEFAULT_COLLECTION, shard);
long[] numFound = new long[slice.getReplicasMap().size()];
int c = 0;
for (Replica replica : slice.getReplicas()) {
String coreUrl = new ZkCoreNodeProps(replica).getCoreUrl();
HttpSolrServer server = new HttpSolrServer(coreUrl);
QueryResponse response;
try {
response = server.query(query);
} finally {
server.shutdown();
}
numFound[c++] = response.getResults().getNumFound();
log.info("Shard: " + shard + " Replica: {} has {} docs", coreUrl, String.valueOf(response.getResults().getNumFound()));
assertTrue("Shard: " + shard + " Replica: " + coreUrl + " has 0 docs", response.getResults().getNumFound() > 0);
}
for (int i = 0; i < slice.getReplicasMap().size(); i++) {
assertEquals(shard + " is not consistent", numFound[0], numFound[i]);
}
}
示例7: query
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
/**
* 查詢 solr,
*
* @param coreName core 名稱, 如: core1 core0
* @param solrSearchParams 查詢參數, 與 solr 後台 一 一對應
* @return
*/
public QueryResponse query(String coreName, SolrSearchParams solrSearchParams) {
if (StringUtils.isEmpty(coreName) || null == solrSearchParams) {
logger.error("沒有傳遞有效參數!!");
return null;
}
HttpSolrServer server = new HttpSolrServer(getHttpSolrServerUrl(coreName));
SolrQuery params = new SolrQuery();
if (StringUtils.isNoneEmpty(solrSearchParams.getQ())) {
params.add("q", solrSearchParams.getQ());
}
if (StringUtils.isNotEmpty(solrSearchParams.getSort())) {
params.add("sort", solrSearchParams.getSort());
}
if (null != solrSearchParams.getWt()) {
params.add("wt", solrSearchParams.getWt().getType());
}
// 分頁查詢
if (null == solrSearchParams.getPageNum() || solrSearchParams.getPageNum() <= 0) {
solrSearchParams.setPageNum(1);
}
if (null == solrSearchParams.getPageSize() || solrSearchParams.getPageSize() <= 0) {
solrSearchParams.setPageSize(10);
}
params.add("start", String.valueOf(((solrSearchParams.getPageNum() - 1) * solrSearchParams.getPageSize())));
params.add("rows", solrSearchParams.getPageSize().toString());
QueryResponse rsp = null;
try {
rsp = server.query(params);
} catch (SolrServerException e) {
logger.error("solr query 異常, e:{}", e.getMessage());
}
return rsp;
}
示例8: main
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
public static void main(String[] agrs){
HttpSolrServer server = new HttpSolrServer("http://121.41.42.83:8100/solr/core0");
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setStart(0);
query.setRows(1);
try {
QueryResponse response = server.query(query);
SolrDocumentList results = response.getResults();
System.out.println(results);
} catch (SolrServerException e) {
e.printStackTrace();
}
}
示例9: testWithBinaryBean
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
@Test
public void testWithBinaryBean()throws Exception{
HttpSolrServer httpSolrServer = (HttpSolrServer) getSolrServer();
httpSolrServer.setRequestWriter(new BinaryRequestWriter());
httpSolrServer.deleteByQuery( "*:*" ); // delete everything!
final int[] counter = new int[1];
counter[0] = 0;
httpSolrServer.addBeans(new Iterator<Bean>() {
@Override
public boolean hasNext() {
return counter[0] < numdocs;
}
@Override
public Bean next() {
Bean bean = new Bean();
bean.id = "" + (++counter[0]);
bean.cat = "foocat";
return bean;
}
@Override
public void remove() {
//do nothing
}
});
httpSolrServer.commit();
SolrQuery query = new SolrQuery("*:*");
QueryResponse response = httpSolrServer.query(query);
assertEquals(0, response.getStatus());
assertEquals(numdocs, response.getResults().getNumFound());
}
示例10: doIt
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
private void doIt(HttpSolrServer httpSolrServer) throws SolrServerException, IOException {
final int[] counter = new int[1];
counter[0] = 0;
httpSolrServer.add(new Iterator<SolrInputDocument>() {
@Override
public boolean hasNext() {
return counter[0] < numdocs;
}
@Override
public SolrInputDocument next() {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "" + (++counter[0]));
doc.addField("cat", "foocat");
return doc;
}
@Override
public void remove() {
//do nothing
}
});
httpSolrServer.commit();
SolrQuery query = new SolrQuery("*:*");
QueryResponse response = httpSolrServer.query(query);
assertEquals(0, response.getStatus());
assertEquals(numdocs, response.getResults().getNumFound());
}
示例11: getCount
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
public long getCount(HttpSolrServer server, String core) {
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/select");
params.set("q", "*:*");
long numFound = 0;
server.setBaseURL(baseUrl + core);
try {
QueryResponse response = server.query(params);
numFound = response.getResults().getNumFound();
} catch (Exception e) {
e.printStackTrace();
}
return numFound;
}
示例12: checkDocCountsAndShardStates
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
protected void checkDocCountsAndShardStates(int[] docCounts, int numReplicas) throws Exception {
ClusterState clusterState = null;
Slice slice1_0 = null, slice1_1 = null;
int i = 0;
for (i = 0; i < 10; i++) {
ZkStateReader zkStateReader = cloudClient.getZkStateReader();
zkStateReader.updateClusterState(true);
clusterState = zkStateReader.getClusterState();
slice1_0 = clusterState.getSlice(AbstractDistribZkTestBase.DEFAULT_COLLECTION, "shard1_0");
slice1_1 = clusterState.getSlice(AbstractDistribZkTestBase.DEFAULT_COLLECTION, "shard1_1");
if (Slice.ACTIVE.equals(slice1_0.getState()) && Slice.ACTIVE.equals(slice1_1.getState()))
break;
Thread.sleep(500);
}
log.info("ShardSplitTest waited for {} ms for shard state to be set to active", i * 500);
assertNotNull("Cluster state does not contain shard1_0", slice1_0);
assertNotNull("Cluster state does not contain shard1_0", slice1_1);
assertEquals("shard1_0 is not active", Slice.ACTIVE, slice1_0.getState());
assertEquals("shard1_1 is not active", Slice.ACTIVE, slice1_1.getState());
assertEquals("Wrong number of replicas created for shard1_0", numReplicas, slice1_0.getReplicas().size());
assertEquals("Wrong number of replicas created for shard1_1", numReplicas, slice1_1.getReplicas().size());
commit();
// can't use checkShardConsistency because it insists on jettys and clients for each shard
checkSubShardConsistency(SHARD1_0);
checkSubShardConsistency(SHARD1_1);
SolrQuery query = new SolrQuery("*:*").setRows(1000).setFields("id", "_version_");
query.set("distrib", false);
ZkCoreNodeProps shard1_0 = getLeaderUrlFromZk(AbstractDistribZkTestBase.DEFAULT_COLLECTION, SHARD1_0);
HttpSolrServer shard1_0Server = new HttpSolrServer(shard1_0.getCoreUrl());
QueryResponse response;
try {
response = shard1_0Server.query(query);
} finally {
shard1_0Server.shutdown();
}
long shard10Count = response.getResults().getNumFound();
ZkCoreNodeProps shard1_1 = getLeaderUrlFromZk(
AbstractDistribZkTestBase.DEFAULT_COLLECTION, SHARD1_1);
HttpSolrServer shard1_1Server = new HttpSolrServer(shard1_1.getCoreUrl());
QueryResponse response2;
try {
response2 = shard1_1Server.query(query);
} finally {
shard1_1Server.shutdown();
}
long shard11Count = response2.getResults().getNumFound();
logDebugHelp(docCounts, response, shard10Count, response2, shard11Count);
assertEquals("Wrong doc count on shard1_0. See SOLR-5309", docCounts[0], shard10Count);
assertEquals("Wrong doc count on shard1_1. See SOLR-5309", docCounts[1], shard11Count);
}
示例13: multipleShardMigrateTest
import org.apache.solr.client.solrj.impl.HttpSolrServer; //導入方法依賴的package包/類
protected void multipleShardMigrateTest() throws Exception {
del("*:*");
commit();
assertTrue(cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound() == 0);
final String splitKey = "a";
final int BIT_SEP = 1;
final int[] splitKeyCount = new int[1];
for (int id = 0; id < 26*3; id++) {
String shardKey = "" + (char) ('a' + (id % 26)); // See comment in ShardRoutingTest for hash distribution
String key = shardKey;
if (splitKey.equals(shardKey)) {
key += "/" + BIT_SEP; // spread it over half the collection
}
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", key + "!" + id);
doc.addField("n_ti", id);
cloudClient.add(doc);
if (splitKey.equals(shardKey))
splitKeyCount[0]++;
}
assertTrue(splitKeyCount[0] > 0);
String targetCollection = "migrate_multipleshardtest_targetCollection";
createCollection(targetCollection);
Indexer indexer = new Indexer(cloudClient, splitKey, 1, 30);
indexer.start();
String url = CustomCollectionTest.getUrlFromZk(getCommonCloudSolrServer().getZkStateReader().getClusterState(), targetCollection);
HttpSolrServer collectionClient = new HttpSolrServer(url);
SolrQuery solrQuery = new SolrQuery("*:*");
assertEquals("DocCount on target collection does not match", 0, collectionClient.query(solrQuery).getResults().getNumFound());
invokeMigrateApi(AbstractDistribZkTestBase.DEFAULT_COLLECTION, splitKey + "/" + BIT_SEP + "!", targetCollection);
long finishTime = System.currentTimeMillis();
indexer.join();
splitKeyCount[0] += indexer.getSplitKeyCount();
try {
cloudClient.deleteById("a/" + BIT_SEP + "!104");
splitKeyCount[0]--;
} catch (Exception e) {
log.warn("Error deleting document a/" + BIT_SEP + "!104", e);
}
cloudClient.commit();
collectionClient.commit();
solrQuery = new SolrQuery("*:*").setRows(1000);
QueryResponse response = collectionClient.query(solrQuery);
log.info("Response from target collection: " + response);
assertEquals("DocCount on target collection does not match", splitKeyCount[0], response.getResults().getNumFound());
collectionClient.shutdown();
collectionClient = null;
getCommonCloudSolrServer().getZkStateReader().updateClusterState(true);
ClusterState state = getCommonCloudSolrServer().getZkStateReader().getClusterState();
Slice slice = state.getSlice(AbstractDistribZkTestBase.DEFAULT_COLLECTION, SHARD2);
assertNotNull("Routing rule map is null", slice.getRoutingRules());
assertFalse("Routing rule map is empty", slice.getRoutingRules().isEmpty());
assertNotNull("No routing rule exists for route key: " + splitKey, slice.getRoutingRules().get(splitKey + "!"));
boolean ruleRemoved = waitForRuleToExpire(splitKey, finishTime);
assertTrue("Routing rule was not expired", ruleRemoved);
}