elasticsearch api中的get操作
官網: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"
}
總結
以上是生活随笔為你收集整理的elasticsearch api中的get操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elasticsearch rest a
- 下一篇: elasticsearch api中的d