Java Code Examples for org.elasticsearch.action.get.GetResponse#getVersion()

The following examples show how to use org.elasticsearch.action.get.GetResponse#getVersion() . 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: Test.java    From dht-spider with MIT License 6 votes vote down vote up
public static void get(Map<String, Object> m) throws Exception{
    GetRequest getRequest = new GetRequest(
            "haha",
            "doc",
            "2");
    String[] includes = new String[]{"message","user","*Date"};
    String[] excludes = Strings.EMPTY_ARRAY;
    FetchSourceContext fetchSourceContext =
            new FetchSourceContext(true, includes, excludes);
    getRequest.fetchSourceContext(fetchSourceContext);

    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    String index = getResponse.getIndex();
    String type = getResponse.getType();
    String id = getResponse.getId();
    if (getResponse.isExists()) {
        long version = getResponse.getVersion();
        String sourceAsString = getResponse.getSourceAsString();
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        System.out.println(sourceAsMap);
    } else {

    }
}
 
Example 2
Source File: TransportCreateModelFromSetAction.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
private void doStore(Task parentTask, GetResponse response, CreateModelFromSetRequest request,
                     ActionListener<CreateModelFromSetResponse> listener) {
    if (!response.isExists()) {
        throw new IllegalArgumentException("Stored feature set [" + request.getFeatureSetName() + "] does not exist");
    }
    if (request.getExpectedSetVersion() != null && request.getExpectedSetVersion() != response.getVersion()) {
        throw new IllegalArgumentException("Stored feature set [" + request.getFeatureSetName() + "]" +
                " has version [" + response.getVersion() + "] but [" + request.getExpectedSetVersion() + "] was expected.");
    }
    final StoredFeatureSet set;
    try {
        set = IndexFeatureStore.parse(StoredFeatureSet.class, StoredFeatureSet.TYPE, response.getSourceAsBytesRef());
    } catch(IOException ioe) {
        throw new IllegalStateException("Cannot parse stored feature set [" + request.getFeatureSetName() + "]", ioe);
    }
    // Model will be parsed & checked by TransportFeatureStoreAction
    StoredLtrModel model = new StoredLtrModel(request.getModelName(), set, request.getDefinition());
    FeatureStoreRequest featureStoreRequest = new FeatureStoreRequest(request.getStore(), model, FeatureStoreRequest.Action.CREATE);
    featureStoreRequest.setRouting(request.getRouting());
    featureStoreRequest.setParentTask(clusterService.localNode().getId(), parentTask.getId());
    featureStoreRequest.setValidation(request.getValidation());
    featureStoreAction.execute(featureStoreRequest, ActionListener.wrap(
            (r) -> listener.onResponse(new CreateModelFromSetResponse(r.getResponse())),
            listener::onFailure));

}
 
Example 3
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 6 votes vote down vote up
/**
 * 获取索引信息
 * @param index
 * @param type
 * @param id
 */
private static void getIndex(String index, String type, String id){
	Client client = createTransportClient();
	GetResponse response = client.prepareGet(index, type, id)
	        .execute()
	        .actionGet();
	boolean exists = response.isExists();
	System.out.println(exists);// 判断索引是否存在
	String sourceString = response.getSourceAsString();
	System.out.println(sourceString);// 获取索引,并且打印出索引内容
	System.out.println("****************index ***********************");
	// Index name
	String _index = response.getIndex();
	// Type name
	String _type = response.getType();
	// Document ID (generated or not)
	String _id = response.getId();
	// Version (if it's the first time you index this document, you will get: 1)
	long _version = response.getVersion();
	System.out.println(_index+","+_type+","+_id+","+_version);
}
 
Example 4
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns a Document representing the specified document ID (combination of resource and context), or null when no
 * such Document exists yet.
 */
@Override
protected SearchDocument getDocument(String id) throws IOException {
	GetResponse response = client.prepareGet(indexName, documentType, id).execute().actionGet();
	if (response.isExists()) {
		return new ElasticsearchDocument(response.getId(), response.getType(), response.getIndex(),
				response.getVersion(), response.getSource(), geoContextMapper);
	}
	// no such Document
	return null;
}
 
Example 5
Source File: PersistentServiceDef.java    From sfs with Apache License 2.0 5 votes vote down vote up
public static PersistentServiceDef fromGetResponse(GetResponse getResponse) {
    long persistentVersion = getResponse.getVersion();
    JsonObject document = new JsonObject(getResponse.getSourceAsString());

    return new PersistentServiceDef(getResponse.getId(), persistentVersion)
            .merge(document);
}
 
Example 6
Source File: EsHighLevelRestTest2.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
	 * 多查询使用
	 * 
	 * @throws IOException
	 */
	private static void multiGet() throws IOException {

		MultiGetRequest request = new MultiGetRequest();
		request.add(new MultiGetRequest.Item("estest", "estest", "1"));
		request.add(new MultiGetRequest.Item("user", "userindex", "2"));
		// 禁用源检索,默认启用
//		request.add(new MultiGetRequest.Item("user", "userindex", "2").fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE));

		// 同步构建
		MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);

		// 异步构建
//		MultiGetResponse response2 = client.mgetAsync(request, RequestOptions.DEFAULT, listener);

		/*
		 * 返回的MultiGetResponse包含在' getResponses中的MultiGetItemResponse的列表,其顺序与请求它们的顺序相同。
		 * 如果成功,MultiGetItemResponse包含GetResponse或MultiGetResponse。如果失败了就失败。
		 * 成功看起来就像一个正常的GetResponse
		 */

		for (MultiGetItemResponse item : response.getResponses()) {
			assertNull(item.getFailure());
			GetResponse get = item.getResponse();
			String index = item.getIndex();
			String type = item.getType();
			String id = item.getId();
			// 如果请求存在
			if (get.isExists()) {
				long version = get.getVersion();
				String sourceAsString = get.getSourceAsString();
				Map<String, Object> sourceAsMap = get.getSourceAsMap();
				byte[] sourceAsBytes = get.getSourceAsBytes();
				System.out.println("查询的结果:" + sourceAsMap);
			} else {
				System.out.println("没有找到该文档!");
			}

		}

	}
 
Example 7
Source File: PersistentObject.java    From sfs with Apache License 2.0 3 votes vote down vote up
public static PersistentObject fromGetResponse(PersistentContainer container, GetResponse getResponse) {

        String id = getResponse.getId();
        long persistentVersion = getResponse.getVersion();
        JsonObject document = new JsonObject(getResponse.getSourceAsString());

        return new PersistentObject(container, id, persistentVersion)
                .merge(document);

    }