elasticsearch api中的get操作,elasticsearchapi
官网:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html
The get API allows to get a typed JSON document from the index based onits id. The following example gets a JSON document from an index calledtwitter, under a type called tweet, with id valued 1:
GetResponse response = client.prepareGet("twitter", "tweet", "1").get();
For more information on the get operation, check out the RESTget docs.
Operation Threadingedit
The get API allows to set the threading model the operation will beperformed when the actual execution of the API is performed on the samenode (the API is executed on a shard that is allocated on the sameserver).
The options are to execute the operation on a different thread, or toexecute it on the calling thread (note that the API is still async). Bydefault,operationThreaded is set to
true which means the operationis executed on a different thread. Here is an example that sets it tofalse:
GetResponse response = client.prepareGet("twitter", "tweet", "1")
.setOperationThreaded(false)
.get();
练习源码:
package es.documentApi;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class ElasticSearchGet {
public static void main(String[] args) {
try {
// 设置集群名称
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch")
.put("client.transport.sniff", true).build();
// 创建client
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.224.140"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.224.141"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("192.168.224.142"), 9300));
GetResponse response = client.prepareGet("twitter", "tweet", "1").get();
GetResponse response1 = client.prepareGet("twitter", "tweet", "1")
.setOperationThreaded(false)
.get();
String res = response.getSourceAsString();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最后得到查询结果:
no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
{
"title":"my sense",
"author":"sxx",
"content":"sense is a good tool"
}