本文整理匯總了Java中org.elasticsearch.action.ListenableActionFuture.actionGet方法的典型用法代碼示例。如果您正苦於以下問題:Java ListenableActionFuture.actionGet方法的具體用法?Java ListenableActionFuture.actionGet怎麽用?Java ListenableActionFuture.actionGet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.elasticsearch.action.ListenableActionFuture
的用法示例。
在下文中一共展示了ListenableActionFuture.actionGet方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: warmFieldData
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
public void warmFieldData(String parentField, String childField) {
ListenableActionFuture<SearchResponse> parentSearch = null;
ListenableActionFuture<SearchResponse> childSearch = null;
if (parentField != null) {
parentSearch = client
.prepareSearch(PARENT_INDEX)
.setQuery(matchAllQuery()).addAggregation(terms("parentfield").field(parentField)).execute();
}
if (childField != null) {
childSearch = client
.prepareSearch(CHILD_INDEX)
.setQuery(matchAllQuery()).addAggregation(terms("childfield").field(childField)).execute();
}
if (parentSearch != null) parentSearch.actionGet();
if (childSearch != null) childSearch.actionGet();
}
示例2: executeProjectSearchQuery
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
private void executeProjectSearchQuery(Client client, QueryBuilder query, String type,
String routing, List<ElasticHit> elasticHits) {
SearchRequestBuilder srb = client.prepareSearch(Settings.META_INDEX);
srb = srb.setTypes(type);
srb = srb.setQuery(query);
srb = srb.addHighlightedField("name");
srb = srb.setRouting(routing);
LOG.log(Level.INFO, "Project Elastic query in Shared Dataset [{0}] is: {1} {2}", new String[]{
type, routing, srb.toString()});
ListenableActionFuture<SearchResponse> futureResponse = srb.execute();
SearchResponse response = futureResponse.actionGet();
if (response.status().getStatus() == 200) {
if (response.getHits().getHits().length > 0) {
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
elasticHits.add(new ElasticHit(hit));
}
}
}
}
示例3: writeToNoSQL
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
@Override
public void writeToNoSQL(List<Map<String, Object>> entityList) {
JSONArray array = JSONArray.fromObject(entityList);
for (int i = 0; i < array.size(); i++) {
IndexRequestBuilder builder = client.prepareIndex(index_name, index_type);
if (getPrimaryKey() != null)
builder.setId( ((JSONObject)array.get(i)).getString(getPrimaryKey()));
builder.setSource(array.get(i).toString());
bulkRequest.add(builder);
}
if (bulkRequest.numberOfActions() > 0) {
long t1 = System.currentTimeMillis();
ListenableActionFuture<BulkResponse> action = bulkRequest.execute();
long t2 = System.currentTimeMillis();
BulkResponse response = action.actionGet();
for (Iterator<BulkItemResponse> iterator = response.iterator(); iterator.hasNext();) {
BulkItemResponse e = (BulkItemResponse) iterator.next();
if (e.isFailed())
throw new FailedCommunicationException("Insertion to ES failed.");
}
log.info("Time taken to Write "+ bulkRequest.numberOfActions() + " documents to ES :" + ((t2-t1)) + " ms");
}
}
示例4: getTermList
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
/**
* getTermList
*
* @param index
* @param keyFields
* @param searchText
* @param maxPerShard
* @param prefix
* @return
*/
private SearchResponse getTermList(String index, List<String> fields, String searchText, int maxPerShard, boolean prefix, boolean caseInsensitive, boolean sort) {
TermListFacetBuilder custom_facet = new TermListFacetBuilder(facetName).fields(fields).maxPerShard(maxPerShard).prefix(prefix).sort(sort).caseInsensitive(caseInsensitive).search(searchText);
SearchResponse custom_sr = null;
try {
SearchRequestBuilder srb = client().prepareSearch(index);
srb.setSearchType(SearchType.COUNT);
srb.addFacet(custom_facet);
System.out.println("SearchRequestBuilder Facet : \n " + srb.toString() + "\n");
ListenableActionFuture<SearchResponse> laf = srb.execute();
custom_sr = laf.actionGet();
} catch (Exception e) {
e.printStackTrace(System.out);
fail("this test failed");
}
assertFalse(custom_sr.toString().startsWith("{ \"error\" : "));
System.out.println("SearchResponse : \n " + custom_sr.toString());
return custom_sr;
}
示例5: ensureSearchWasCancelled
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
private SearchResponse ensureSearchWasCancelled(ListenableActionFuture<SearchResponse> searchResponse) {
try {
SearchResponse response = searchResponse.actionGet();
logger.info("Search response {}", response);
assertNotEquals("At least one shard should have failed", 0, response.getFailedShards());
return response;
} catch (SearchPhaseExecutionException ex) {
logger.info("All shards failed with", ex);
return null;
}
}
示例6: logExcuteAndReturn
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
static <R, T> T logExcuteAndReturn(String method, R requestBuilder,
ListenableActionFuture<T> responseFunction) {
log.debug("[{}][timeoutMillis={}] - {}", method, timeoutMillis,
requestBuilder.toString());
try {
return responseFunction.actionGet();
} catch (Exception e) {
return JMExceptionManager.handleExceptionAndThrowRuntimeEx(log, e,
method, requestBuilder);
}
}
示例7: executeBulk
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
/**
* Executes the bulk request
* @param bulk
*/
private static void executeBulk(BulkRequestBuilder bulk) {
log.info("Executing bulk...");
ListenableActionFuture<BulkResponse> futures = bulk.execute();
BulkResponse response = futures.actionGet();
log.info("Response: " + response.getTookInMillis());
}
示例8: putDocument
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
public void putDocument(String index, String type, String id, String document, boolean sync ) throws EsServerException {
IndexRequestBuilder indexRequestBuilder = getClient().prepareIndex(index,type,id).setSource(document);
ListenableActionFuture<IndexResponse> laf = indexRequestBuilder.execute();
if (sync) {
laf.actionGet();
}
}
示例9: sendBulkInserts
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
private void sendBulkInserts() {
if (docs.isEmpty()) { return; }
BulkRequestBuilder bulkRequestBuilder = null;
try {
synchronized (esClient) {
TransportClient client = getClient();
bulkRequestBuilder = client.prepareBulk();
Iterator<EsDocument> iter = docs.iterator();
while (iter.hasNext()) {
EsDocument doc = iter.next();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(doc.index,doc.type,doc.id).setSource(doc.doc);
bulkRequestBuilder.add(indexRequestBuilder);
}
}
ListenableActionFuture<BulkResponse> laf = bulkRequestBuilder.execute();
if (sync) {
laf.actionGet();
}
} catch (EsServerException e) {
e.printStackTrace();
}
docs = new ArrayList<EsDocument>();
bulkInsertCount = 0;
bulkInsertSize = 0;
}
示例10: writeToNoSQL
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
@Override
public void writeToNoSQL(List<Map<String, Object>> entityList) {
BulkRequestBuilder bulkRequest = client.prepareBulk();
JSONArray array = JSONArray.fromObject(entityList);
for (int i = 0; i < array.size(); i++) {
IndexRequestBuilder builder = client.prepareIndex(index_name, index_type);
if (getPrimaryKey() != null) {
builder.setId(((JSONObject) array.get(i)).getString(getPrimaryKey()));
}
builder.setSource(array.get(i).toString());
bulkRequest.add(builder);
}
if (bulkRequest.numberOfActions() > 0) {
long t1 = System.currentTimeMillis();
ListenableActionFuture<BulkResponse> action = bulkRequest.execute();
long t2 = System.currentTimeMillis();
BulkResponse response = action.actionGet();
for (Iterator<BulkItemResponse> iterator = response.iterator(); iterator.hasNext();) {
BulkItemResponse e = (BulkItemResponse) iterator.next();
if (e.isFailed()) {
throw new FailedCommunicationException("Insertion to ES failed.");
}
}
log.info("Time taken to Write " + bulkRequest.numberOfActions() + " documents to ES :" + ((t2 - t1)) + " ms");
}
}
示例11: testNonStringTermsShouldThrowError
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
/**
* testNonStringTermsShouldThrowError
*
* @throws Exception
*/
public void testNonStringTermsShouldThrowError() throws Exception {
runStandardPutsAndFlush(index); //include numeric data
List<String> randomNumField = new ArrayList<String>();
randomNumField.add("rnum");
boolean thrown = false;
try {
TermListFacetBuilder custom_facet = new TermListFacetBuilder(facetName).fields(randomNumField);
SearchRequestBuilder srb = client().prepareSearch(index);
srb.setSearchType(SearchType.COUNT);
srb.addFacet(custom_facet);
System.out.println("SearchResponse Facet : \n " + srb.toString() + "\n");
ListenableActionFuture<SearchResponse> laf = srb.execute();
SearchResponse sr = laf.actionGet();
fail("Never should have gotten here: ");
} catch (Exception e) {
e.printStackTrace(System.out);
thrown = true;
}
assertTrue(thrown);
}
示例12: testSnapshotWithStuckNode
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
public void testSnapshotWithStuckNode() throws Exception {
logger.info("--> start 2 nodes");
ArrayList<String> nodes = new ArrayList<>();
nodes.add(internalCluster().startNode());
nodes.add(internalCluster().startNode());
Client client = client();
assertAcked(prepareCreate("test-idx", 2, Settings.builder().put("number_of_shards", 2).put("number_of_replicas", 0)));
ensureGreen();
logger.info("--> indexing some data");
for (int i = 0; i < 100; i++) {
index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
}
refresh();
assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
logger.info("--> creating repository");
Path repo = randomRepoPath();
PutRepositoryResponse putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo")
.setType("mock").setSettings(
Settings.builder()
.put("location", repo)
.put("random", randomAsciiOfLength(10))
.put("wait_after_unblock", 200)
).get();
assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
// Pick one node and block it
String blockedNode = blockNodeWithIndex("test-repo", "test-idx");
// Remove it from the list of available nodes
nodes.remove(blockedNode);
int numberOfFilesBeforeSnapshot = numberOfFiles(repo);
logger.info("--> snapshot");
client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(false).setIndices("test-idx").get();
logger.info("--> waiting for block to kick in");
waitForBlock(blockedNode, "test-repo", TimeValue.timeValueSeconds(60));
logger.info("--> execution was blocked on node [{}], aborting snapshot", blockedNode);
ListenableActionFuture<DeleteSnapshotResponse> deleteSnapshotResponseFuture = internalCluster().client(nodes.get(0)).admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").execute();
// Make sure that abort makes some progress
Thread.sleep(100);
unblockNode("test-repo", blockedNode);
logger.info("--> stopping node [{}]", blockedNode);
stopNode(blockedNode);
try {
DeleteSnapshotResponse deleteSnapshotResponse = deleteSnapshotResponseFuture.actionGet();
assertThat(deleteSnapshotResponse.isAcknowledged(), equalTo(true));
} catch (SnapshotMissingException ex) {
// When master node is closed during this test, it sometime manages to delete the snapshot files before
// completely stopping. In this case the retried delete snapshot operation on the new master can fail
// with SnapshotMissingException
}
logger.info("--> making sure that snapshot no longer exists");
assertThrows(client().admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").execute(), SnapshotMissingException.class);
// Subtract three files that will remain in the repository:
// (1) index-1
// (2) index-0 (because we keep the previous version) and
// (3) index-latest
assertThat("not all files were deleted during snapshot cancellation", numberOfFilesBeforeSnapshot, equalTo(numberOfFiles(repo) - 3));
logger.info("--> done");
}
示例13: testDeletionOfFailingToRecoverIndexShouldStopRestore
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
public void testDeletionOfFailingToRecoverIndexShouldStopRestore() throws Exception {
Path repositoryLocation = randomRepoPath();
Client client = client();
logger.info("--> creating repository");
assertAcked(client.admin().cluster().preparePutRepository("test-repo")
.setType("fs").setSettings(Settings.builder().put("location", repositoryLocation)));
createIndex("test-idx");
ensureGreen();
logger.info("--> indexing some data");
for (int i = 0; i < 100; i++) {
index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
}
refresh();
assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
logger.info("--> snapshot");
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
assertThat(createSnapshotResponse.getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(createSnapshotResponse.getSnapshotInfo().successfulShards()));
logger.info("--> update repository with mock version");
assertAcked(client.admin().cluster().preparePutRepository("test-repo")
.setType("mock").setSettings(
Settings.builder()
.put("location", repositoryLocation)
.put("random", randomAsciiOfLength(10))
.put("random_data_file_io_exception_rate", 1.0) // Fail completely
));
// Test restore after index deletion
logger.info("--> delete index");
cluster().wipeIndices("test-idx");
logger.info("--> restore index after deletion");
ListenableActionFuture<RestoreSnapshotResponse> restoreSnapshotResponseFuture =
client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute();
logger.info("--> wait for the index to appear");
// that would mean that recovery process started and failing
assertThat(waitForIndex("test-idx", TimeValue.timeValueSeconds(10)), equalTo(true));
logger.info("--> delete index");
cluster().wipeIndices("test-idx");
logger.info("--> get restore results");
// Now read restore results and make sure it failed
RestoreSnapshotResponse restoreSnapshotResponse = restoreSnapshotResponseFuture.actionGet(TimeValue.timeValueSeconds(10));
assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), greaterThan(0));
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(restoreSnapshotResponse.getRestoreInfo().failedShards()));
logger.info("--> restoring working repository");
assertAcked(client.admin().cluster().preparePutRepository("test-repo")
.setType("fs").setSettings(Settings.builder().put("location", repositoryLocation)));
logger.info("--> trying to restore index again");
restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
SearchResponse countResponse = client.prepareSearch("test-idx").setSize(0).get();
assertThat(countResponse.getHits().getTotalHits(), equalTo(100L));
}
示例14: testBatchingShardUpdateTask
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
public void testBatchingShardUpdateTask() throws Exception {
final Client client = client();
logger.info("--> creating repository");
assertAcked(client.admin().cluster().preparePutRepository("test-repo")
.setType("fs").setSettings(Settings.builder()
.put("location", randomRepoPath())
.put("compress", randomBoolean())
.put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
assertAcked(prepareCreate("test-idx", 0, Settings.builder().put("number_of_shards", between(1, 10))
.put("number_of_replicas", 0)));
ensureGreen();
logger.info("--> indexing some data");
final int numdocs = randomIntBetween(10, 100);
IndexRequestBuilder[] builders = new IndexRequestBuilder[numdocs];
for (int i = 0; i < builders.length; i++) {
builders[i] = client().prepareIndex("test-idx", "type1", Integer.toString(i)).setSource("field1", "bar " + i);
}
indexRandom(true, builders);
flushAndRefresh();
final int numberOfShards = getNumShards("test-idx").numPrimaries;
logger.info("number of shards: {}", numberOfShards);
final ClusterService clusterService = internalCluster().clusterService(internalCluster().getMasterName());
BlockingClusterStateListener snapshotListener = new BlockingClusterStateListener(clusterService, "update_snapshot [", "update snapshot state", Priority.HIGH);
try {
clusterService.addListener(snapshotListener);
logger.info("--> snapshot");
ListenableActionFuture<CreateSnapshotResponse> snapshotFuture = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").execute();
// Await until shard updates are in pending state.
assertBusyPendingTasks("update snapshot state", numberOfShards);
snapshotListener.unblock();
// Check that the snapshot was successful
CreateSnapshotResponse createSnapshotResponse = snapshotFuture.actionGet();
assertEquals(SnapshotState.SUCCESS, createSnapshotResponse.getSnapshotInfo().state());
assertEquals(numberOfShards, createSnapshotResponse.getSnapshotInfo().totalShards());
assertEquals(numberOfShards, createSnapshotResponse.getSnapshotInfo().successfulShards());
} finally {
clusterService.removeListener(snapshotListener);
}
// Check that we didn't timeout
assertFalse(snapshotListener.timedOut());
// Check that cluster state update task was called only once
assertEquals(1, snapshotListener.count());
}
示例15: globalSearch
import org.elasticsearch.action.ListenableActionFuture; //導入方法依賴的package包/類
public List<ElasticHit> globalSearch(String searchTerm) throws AppException {
//some necessary client settings
Client client = getClient();
//check if the index are up and running
if (!this.indexExists(client, Settings.META_INDEX)) {
LOG.log(Level.INFO, ResponseMessages.ELASTIC_INDEX_NOT_FOUND);
throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.
getStatusCode(), ResponseMessages.ELASTIC_INDEX_NOT_FOUND);
}
LOG.log(Level.INFO, "Found elastic index, now executing the query.");
/*
* If projects contain a searchable field then the client can hit both
* indices (projects, datasets) with a single query. Right now the single
* query fails because of the lack of a searchable field in the projects.
* ADDED MANUALLY A SEARCHABLE FIELD IN THE RIVER. MAKES A PROJECT
* SEARCHABLE BY DEFAULT. NEEDS REFACTORING
*/
//hit the indices - execute the queries
SearchRequestBuilder srb = client.prepareSearch(Settings.META_INDEX);
srb = srb.setTypes(Settings.META_PROJECT_TYPE,
Settings.META_DATASET_TYPE);
srb = srb.setQuery(this.globalSearchQuery(searchTerm.toLowerCase()));
srb = srb.addHighlightedField("name");
LOG.log(Level.INFO, "Global search Elastic query is: {0}", srb.toString());
ListenableActionFuture<SearchResponse> futureResponse = srb.execute();
SearchResponse response = futureResponse.actionGet();
if (response.status().getStatus() == 200) {
//construct the response
List<ElasticHit> elasticHits = new LinkedList<>();
if (response.getHits().getHits().length > 0) {
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
ElasticHit eHit = new ElasticHit(hit);
eHit.setLocalDataset(true);
int inode_id = Integer.parseInt(hit.getId());
List<Dataset> dsl = datasetFacade.findByInodeId(inode_id);
if (!dsl.isEmpty() && dsl.get(0).isPublicDs()) {
Dataset ds = dsl.get(0);
eHit.setPublicId(ds.getPublicDsId());
}
elasticHits.add(eHit);
}
}
this.clientShutdown(client);
return elasticHits;
} else {
LOG.log(Level.WARNING, "Elasticsearch error code: {0}", response.status().getStatus());
//something went wrong so throw an exception
this.clientShutdown(client);
throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.
getStatusCode(), ResponseMessages.ELASTIC_SERVER_NOT_FOUND);
}
}