Java Code Examples for io.searchbox.client.JestResult#getSourceAsObject()

The following examples show how to use io.searchbox.client.JestResult#getSourceAsObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JestExample.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
/**
 * 获取Document
 * @param index
 * @param type
 * @param id
 * @throws Exception
 */
private static void getDocument(String index,String type,String id) throws Exception {
	JestClient jestClient = JestExample.getJestClient();
	Get get = new Get.Builder(index, id).type(type).build();
	JestResult result = jestClient.execute(get);
	Article article = result.getSourceAsObject(Article.class);
	System.out.println(article.getTitle()+","+article.getContent());
}
 
Example 2
Source File: EsStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Gets an entity.  Callers must unmarshal the resulting map.
 * @param type
 * @param id
 * @throws StorageException
 */
private Map<String, Object> getEntity(String type, String id) throws StorageException {
    try {
        JestResult response = esClient.execute(new Get.Builder(getIndexName(), id).type(type).build());
        if (!response.isSucceeded()) {
            return null;
        }
        return response.getSourceAsObject(Map.class);
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
 
Example 3
Source File: ESRateLimiterComponent.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void accept(final String bucketId, final RateBucketPeriod period, final long limit,
        final long increment, final IAsyncResultHandler<RateLimitResponse> handler) {
    final String id = id(bucketId);

    try {
        Get get = new Get.Builder(getIndexName(), id).type("rateBucket").build(); //$NON-NLS-1$
        JestResult result = getClient().execute(get);
        RateLimiterBucket bucket;
        long version;
        if (result.isSucceeded()) {
            // use the existing bucket
            version = result.getJsonObject().get("_version").getAsLong(); //$NON-NLS-1$
            bucket = result.getSourceAsObject(RateLimiterBucket.class);
        } else {
            // make a new bucket
            version = 0;
            bucket = new RateLimiterBucket();
        }
        bucket.resetIfNecessary(period);

        final RateLimitResponse rlr = new RateLimitResponse();
        if (bucket.getCount() > limit) {
            rlr.setAccepted(false);
        } else {
            rlr.setAccepted(bucket.getCount() < limit);
            bucket.setCount(bucket.getCount() + increment);
            bucket.setLast(System.currentTimeMillis());
        }
        int reset = (int) (bucket.getResetMillis(period) / 1000L);
        rlr.setReset(reset);
        rlr.setRemaining(limit - bucket.getCount());
        updateBucketAndReturn(id, bucket, rlr, version, bucketId, period, limit, increment, handler);
    } catch (Throwable e) {
        handler.handle(AsyncResultImpl.create(e, RateLimitResponse.class));
    }
}
 
Example 4
Source File: ESRegistry.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the api synchronously.
 * @param id
 * @throws IOException
 */
protected Api getApi(String id) throws IOException {
    Get get = new Get.Builder(getIndexName(), id).type("api").build(); //$NON-NLS-1$
    JestResult result = getClient().execute(get);
    if (result.isSucceeded()) {
        Api api = result.getSourceAsObject(Api.class);
        return api;
    } else {
        return null;
    }
}
 
Example 5
Source File: ESRegistry.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the client synchronously.
 * @param id
 * @throws IOException
 */
protected Client getClient(String id) throws IOException {
    Get get = new Get.Builder(getIndexName(), id).type("client").build(); //$NON-NLS-1$
    JestResult result = getClient().execute(get);
    if (result.isSucceeded()) {
        Client client = result.getSourceAsObject(Client.class);
        return client;
    } else {
        return null;
    }
}
 
Example 6
Source File: ESSharedStateComponent.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a stored primitive.
 * @param result
 */
protected Object readPrimitive(JestResult result) throws Exception {
    PrimitiveBean pb = result.getSourceAsObject(PrimitiveBean.class);
    String value = pb.getValue();
    Class<?> c = Class.forName(pb.getType());
    return BackingStoreUtil.readPrimitive(c, value);
}