{"id":10006980,"date":"2022-12-16T18:02:13","date_gmt":"2022-12-16T10:02:13","guid":{"rendered":"https:\/\/ityet.com\/?p=10006980"},"modified":"2022-12-16T18:02:13","modified_gmt":"2022-12-16T10:02:13","slug":"%e6%9d%a5%e8%81%8a%e4%b8%80%e8%81%8a-elasticsearch-%e6%9c%80%e6%96%b0%e7%89%88%e7%9a%84-java-%e5%ae%a2%e6%88%b7%e7%ab%af","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006980","title":{"rendered":"\u6765\u804a\u4e00\u804a ElasticSearch \u6700\u65b0\u7248\u7684 Java \u5ba2\u6237\u7aef"},"content":{"rendered":"<p>\u53ef\u80fd\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u6ce8\u610f\u5230\u4e86\uff0c\u4ece ElasticSearch7.17 \u8fd9\u4e2a\u7248\u672c\u5f00\u59cb\uff0c\u539f\u5148\u7684 Java \u9ad8\u7ea7\u5ba2\u6237\u7aef <br \/>Java High Level REST Client \u5e9f\u5f03\u4e86\uff0c\u4e0d\u652f\u6301\u4e86\u3002\u8001\u5b9e\u8bf4\uff0cElasticSearch \u7b97\u662f\u6211\u7528\u8fc7\u7684\u6240\u6709 Java \u5de5\u5177\u4e2d\uff0c\u66f4\u65b0\u6700\u4e3a\u6fc0\u8fdb\u7684\u4e00\u4e2a\u4e86\uff0c\u5728 Es7 \u4e2d\u5e9f\u5f03\u4e86 TransportClient\uff0c7.17 \u53c8\u5e9f\u5f03\u4e86 TransportClient\uff0c\u90a3\u4e48\u73b0\u5728\u7528\u5565\u5462\uff1f\u73b0\u5728\u7684\u5ba2\u6237\u7aef\u53eb\u505a Elasticsearch Java API Client\u3002<\/p>\n<p>\u4e00\u76f4\u5077\u61d2\u9009\u62e9\u65e0\u89c6 Elasticsearch Java API Client\uff0c\u4e0d\u8fc7\u6700\u8fd1\u5de5\u4f5c\u4e2d\u7528\u5230\u4e86\uff0c\u6240\u4ee5\u8fd8\u662f\u6574\u7bc7\u6587\u7ae0\u548c\u5c0f\u4f19\u4f34\u4eec\u7b80\u5355\u68b3\u7406\u4e00\u4e0b Elasticsearch Java API Client \u7684\u73a9\u6cd5\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ecb\u7ecd\u6211\u4e3b\u8981\u4ece\u7d22\u5f15\u64cd\u4f5c\u548c\u6587\u6863\u64cd\u4f5c\u4e24\u4e2a\u65b9\u9762\u6765\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u3002<\/p>\n<p>\u4e0d\u8fc7\u9700\u8981\u8ddf\u5927\u5bb6\u5f3a\u8c03\u7684\u662f\uff0cElasticSearch \u7684 Java \u5ba2\u6237\u7aef\u60f3\u8981\u7528\u7684 6\uff0c\u5fc5\u987b\u8981\u719f\u6089 ElasticSearch \u7684\u67e5\u8be2\u811a\u672c\uff0c\u5927\u5bb6\u5e73\u65f6\u5728\u5de5\u4f5c\u4e2d\u9047\u5230 Es \u76f8\u5173\u7684\u95ee\u9898\uff0c\u6211\u4e5f\u90fd\u662f\u5efa\u8bae\u5148\u5728 Kibana \u4e2d\u628a\u64cd\u4f5c\u811a\u672c\u5199\u597d\uff0c\u7136\u540e\u518d\u7ffb\u8bd1\u6210 Java \u4ee3\u7801\uff0c\u6216\u8005\u76f4\u63a5\u62f7\u8d1d\u5230 Java \u4ee3\u7801\u4e2d\uff0c<strong>\u975e\u5e38\u4e0d\u5efa\u8bae\u4e0a\u6765\u5c31\u6574 Java \u4ee3\u7801<\/strong>\uff0c\u90a3\u6837\u5f88\u5bb9\u6613\u51fa\u9519\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5bf9 Es \u7684\u64cd\u4f5c\u4e0d\u719f\u6089\uff0c\u677e\u54e5\u5f55\u4e86\u514d\u8d39\u7684\u89c6\u9891\u6559\u7a0b\uff0c\u5927\u5bb6\u53ef\u4ee5\u53c2\u8003\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.bilibili.com\/video\/BV1ft4y1e7tq\/\">https:\/\/www.bilibili.com\/video\/BV1ft4y1e7tq\/<\/a><\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043099132\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0d\u60f3\u770b\u89c6\u9891\uff0c\u4e5f\u53ef\u4ee5\u5728\u5fae\u4fe1\u516c\u4f17\u53f7\u540e\u53f0\u56de\u590d es\uff0c\u6709\u6587\u6863\u6559\u7a0b\u3002<\/p>\n<h2>1. Elasticsearch Java API Client<\/h2>\n<p>Elasticsearch Java API Client \u662f Elasticsearch \u7684\u5b98\u65b9 Java API\uff0c\u8fd9\u4e2a\u5ba2\u6237\u7aef\u4e3a\u6240\u6709 Elasticsearch APIs \u63d0\u4f9b\u5f3a\u7c7b\u578b\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<blockquote><p>\u8fd9\u91cc\u8ddf\u5927\u5bb6\u89e3\u91ca\u4e0b\u4ec0\u4e48\u662f\u5f3a\u7c7b\u578b\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\uff1a\u56e0\u4e3a\u6240\u6709\u7684 Elasticsearch APIs \u672c\u8d28\u4e0a\u90fd\u662f\u4e00\u4e2a RESTful \u98ce\u683c\u7684 HTTP \u8bf7\u6c42\uff0c\u6240\u4ee5\u5f53\u6211\u4eec\u8c03\u7528\u8fd9\u4e9b Elasticsearch APIs \u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5c31\u5f53\u6210\u666e\u901a\u7684 HTTP \u63a5\u53e3\u6765\u5bf9\u5f85\uff0c\u4f8b\u5982\u4f7f\u7528 HttpUrlConnection \u6216\u8005 RestTemplate \u7b49\u5de5\u5177\u6765\u76f4\u63a5\u8c03\u7528\uff0c\u5982\u679c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u76f4\u63a5\u8c03\u7528\uff0c\u5c31\u9700\u8981\u6211\u4eec\u81ea\u5df1\u7ec4\u88c5 JSON \u53c2\u6570\uff0c\u7136\u540e\u81ea\u5df1\u89e3\u6790\u670d\u52a1\u7aef\u8fd4\u56de\u7684 JSON\u3002\u800c\u5f3a\u7c7b\u578b\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u5219\u662f\u7cfb\u7edf\u628a\u8bf7\u6c42\u53c2\u6570\u5c01\u88c5\u6210\u4e00\u4e2a\u5bf9\u8c61\u4e86\uff0c\u6211\u4eec\u8c03\u7528\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5\u53bb\u8bbe\u7f6e\u5c31\u53ef\u4ee5\u4e86\uff0c\u4e0d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u62fc\u63a5 JSON \u53c2\u6570\u4e86\uff0c\u8bf7\u6c42\u7684\u7ed3\u679c\u7cfb\u7edf\u4e5f\u4f1a\u5c01\u88c5\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e0d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u53bb\u89e3\u6790 JSON \u53c2\u6570\u4e86\u3002<\/p><\/blockquote>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u4e00\u4e0b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u60f3\u67e5\u8be2 books \u7d22\u5f15\u4e2d\uff0c\u4e66\u540d\u4e2d\u5305\u542b Java \u5173\u952e\u5b57\u7684\u56fe\u4e66\uff1a<\/p>\n<pre><code class=\"java\">public class EsDemo02 {\n    public static void main(String[] args) throws IOException {\n        URL url = new URL(&quot;http:\/\/localhost:9200\/books\/_search?pretty&quot;);\n        HttpURLConnection con = (HttpURLConnection) url.openConnection();\n        con.setRequestMethod(&quot;GET&quot;);\n        con.setRequestProperty(&quot;content-type&quot;,&quot;application\/json;charset=utf-8&quot;);\n        \/\/\u5141\u8bb8\u8f93\u51fa\u6d41\/\u5141\u8bb8\u53c2\u6570\n        con.setDoOutput(true);\n        \/\/\u83b7\u53d6\u8f93\u51fa\u6d41\n        OutputStream out = con.getOutputStream();\n        String params = &quot;{\/n&quot; +\n                &quot;  \/&quot;query\/&quot;: {\/n&quot; +\n                &quot;    \/&quot;term\/&quot;: {\/n&quot; +\n                &quot;      \/&quot;name\/&quot;: {\/n&quot; +\n                &quot;        \/&quot;value\/&quot;: \/&quot;java\/&quot;\/n&quot; +\n                &quot;      }\/n&quot; +\n                &quot;    }\/n&quot; +\n                &quot;  }\/n&quot; +\n                &quot;}&quot;;\n        out.write(params.getBytes());\n        if (con.getResponseCode() == 200) {\n            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));\n            String str = null;\n            while ((str = br.readLine()) != null) {\n                System.out.println(str);\n            }\n            br.close();\n        }\n    }\n}<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u5230\uff0c\u8fd9\u5c31\u662f\u4e00\u4e2a\u666e\u901a\u7684 HTTP \u8bf7\u6c42\uff0c\u8bf7\u6c42\u53c2\u6570\u5c31\u662f\u67e5\u8be2\u7684\u6761\u4ef6\uff0c\u8fd9\u4e2a\u6761\u4ef6\u662f\u4e00\u4e2a JSON \u5b57\u7b26\u4e32\uff0c\u9700\u8981\u6211\u4eec\u81ea\u5df1\u7ec4\u88c5\uff0c\u8bf7\u6c42\u7684\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2a JSON \u5b57\u7b26\u4e32\uff0c\u8fd9\u4e2a JSON \u5b57\u7b26\u4e32\u4e5f\u9700\u8981\u6211\u4eec\u81ea\u5df1\u624b\u52a8\u53bb\u89e3\u6790\uff0c\u8fd9\u79cd\u53ef\u4ee5\u7b97\u662f\u5f31\u7c7b\u578b\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<p>Elasticsearch Java API Client \u5177\u6709\u5982\u4e0b\u7279\u6027\uff1a<\/p>\n<ul>\n<li>\u4e3a\u6240\u6709 Elasticsearch APIs \u63d0\u4f9b\u5f3a\u7c7b\u578b\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/li>\n<li>\u6240\u6709 API \u90fd\u6709\u963b\u585e\u548c\u5f02\u6b65\u7248\u672c\u3002<\/li>\n<li>\u4f7f\u7528\u6784\u5efa\u5668\u6a21\u5f0f\uff0c\u5728\u521b\u5efa\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u65f6\uff0c\u53ef\u4ee5\u7f16\u5199\u7b80\u6d01\u800c\u53ef\u8bfb\u7684\u4ee3\u7801\u3002<\/li>\n<li>\u901a\u8fc7\u4f7f\u7528\u5bf9\u8c61\u6620\u5c04\u5668\uff08\u5982 Jackson \u6216\u4efb\u4f55\u5b9e\u73b0\u4e86 JSON-B \u7684\u89e3\u6790\u5668\uff09\uff0c\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7c7b\u7684\u65e0\u7f1d\u96c6\u6210\u3002<\/li>\n<li>\u5c06\u534f\u8bae\u5904\u7406\u59d4\u6258\u7ed9\u4e00\u4e2a http \u5ba2\u6237\u7aef\uff0c\u5982 Java Low Level REST Client\uff0c\u5b83\u8d1f\u8d23\u6240\u6709\u4f20\u8f93\u7ea7\u7684\u95ee\u9898\u3002HTTP \u8fde\u63a5\u6c60\u3001\u91cd\u8bd5\u3001\u8282\u70b9\u53d1\u73b0\u7b49\u7b49\u7531\u5b83\u53bb\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<p>\u5173\u4e8e\u7b2c\u4e09\u70b9\uff0c\u677e\u54e5\u5410\u69fd\u4e00\u53e5\uff0c\u786e\u5b9e\u7b80\u6d01\uff0c\u4f46\u662f\u53ef\u8bfb\u6027\u4e00\u822c\u822c\u5427\u3002<\/p>\n<p>\u53e6\u5916\u8fd8\u6709\u4e24\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>Elasticsearch Java \u5ba2\u6237\u7aef\u662f\u5411\u524d\u517c\u5bb9\u7684\uff0c\u5373\u8be5\u5ba2\u6237\u7aef\u652f\u6301\u4e0e Elasticsearch \u7684\u66f4\u5927\u6216\u76f8\u7b49\u7684\u6b21\u8981\u7248\u672c\u8fdb\u884c\u901a\u4fe1\u3002<\/li>\n<li>Elasticsearch Java \u5ba2\u6237\u7aef\u53ea\u5411\u540e\u517c\u5bb9\u9ed8\u8ba4\u7684\u53d1\u884c\u7248\u672c\uff0c\u5e76\u4e14\u6ca1\u6709\u505a\u51fa\u4fdd\u8bc1\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u90a3\u5c31\u4e0d\u5e9f\u8bdd\u4e86\uff0c\u5f00\u6574\u5427\u3002<\/p>\n<h2>2. \u5f15\u5165 Elasticsearch Java API Client<\/h2>\n<p>\u9996\u5148\u9700\u8981\u6211\u4eec\u52a0\u4f9d\u8d56\uff0c\u5bf9 JDK \u7684\u7248\u672c\u8981\u6c42\u662f 1.8\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u5982\u4e0b\u4e24\u4e2a\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n  &lt;groupId&gt;co.elastic.clients&lt;\/groupId&gt;\n  &lt;artifactId&gt;elasticsearch-java&lt;\/artifactId&gt;\n  &lt;version&gt;8.5.1&lt;\/version&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n  &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\n  &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;\n  &lt;version&gt;2.12.3&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u5982\u679c\u662f Spring Boot \u9879\u76ee\uff0c\u5c31\u4e0d\u7528\u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u4f9d\u8d56\u4e86\uff0c\u56e0\u4e3a Spring Boot \u7684 Web \u4e2d\u9ed8\u8ba4\u5df2\u7ecf\u52a0\u4e86\u8fd9\u4e2a\u4f9d\u8d56\u4e86\uff0c\u4f46\u662f Spring Boot \u4e00\u822c\u9700\u8981\u989d\u5916\u6dfb\u52a0\u4e0b\u9762\u8fd9\u4e2a\u4f9d\u8d56\uff0c\u51fa\u73b0\u8fd9\u4e2a\u539f\u56e0\u662f\u7531\u4e8e\u4ece JavaEE \u8fc7\u6e21\u5230 JakartaEE \u65f6\u884d\u751f\u51fa\u6765\u7684\u4e00\u4e9b\u95ee\u9898\uff0c\u8fd9\u91cc\u6211\u5c31\u4e0d\u5570\u55e6\u4e86\uff0c\u54b1\u4eec\u76f4\u63a5\u52a0\u4f9d\u8d56\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n  &lt;groupId&gt;jakarta.json&lt;\/groupId&gt;\n  &lt;artifactId&gt;jakarta.json-api&lt;\/artifactId&gt;\n  &lt;version&gt;2.0.1&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>3. \u5efa\u7acb\u8fde\u63a5<\/h2>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u7528\u6211\u4eec\u7684 Java \u5ba2\u6237\u7aef\u548c ElasticSearch \u4e4b\u95f4\u5efa\u7acb\u8fde\u63a5\uff0c\u5efa\u7acb\u8fde\u63a5\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">RestClient restClient = RestClient.builder(\n    new HttpHost(&quot;localhost&quot;, 9200)).build();\nElasticsearchTransport transport = new RestClientTransport(\n    restClient, new JacksonJsonpMapper());\nElasticsearchClient client = new ElasticsearchClient(transport);<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u5230\uff0c\u8fd9\u91cc\u4e00\u5171\u6709\u4e09\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u4f4e\u7ea7\u5ba2\u6237\u7aef\uff0c\u8fd9\u4e2a\u5176\u5b9e\u677e\u54e5\u4e4b\u524d\u7684\u89c6\u9891\u4e2d\u548c\u5927\u5bb6\u8bb2\u8fc7\u4f4e\u7ea7\u5ba2\u6237\u7aef\u7684\u7528\u6cd5\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u8d58\u8ff0\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\u521b\u5efa\u4e00\u4e2a\u901a\u4fe1 Transport\uff0c\u5e76\u5229\u7528 JacksonJsonpMapper \u505a\u6570\u636e\u7684\u89e3\u6790\u3002<\/li>\n<li>\u6700\u540e\u521b\u5efa\u4e00\u4e2a\u963b\u585e\u7684 Java \u5ba2\u6237\u7aef\u3002<\/li>\n<\/ol>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u963b\u585e\u7684 Java \u5ba2\u6237\u7aef\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u521b\u5efa\u975e\u963b\u585e\u7684 Java \u5ba2\u6237\u7aef\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">RestClient restClient = RestClient.builder(\n        new HttpHost(&quot;localhost&quot;, 9200)).build();\nElasticsearchTransport transport = new RestClientTransport(\n        restClient, new JacksonJsonpMapper());\nElasticsearchAsyncClient client = new ElasticsearchAsyncClient(transport);<\/code><\/pre>\n<p>\u53ea\u6709\u7b2c\u4e09\u6b65\u548c\u524d\u9762\u7684\u4e0d\u4e00\u6837\uff0c\u5176\u4ed6\u90fd\u4e00\u6837\u3002<\/p>\n<blockquote><p>\u5229\u7528\u963b\u585e\u7684 Java \u5ba2\u6237\u7aef\u64cd\u4f5c Es \u7684\u65f6\u5019\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u4e5f\u5c31\u662f\u5fc5\u987b\u7b49\u5230 Es \u7ed9\u51fa\u54cd\u5e94\u4e4b\u540e\uff0c\u4ee3\u7801\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\uff1b\u975e\u963b\u585e\u7684 Java \u5ba2\u6237\u7aef\u5219\u4e0d\u4f1a\u963b\u585e\u540e\u9762\u7684\u4ee3\u7801\u6267\u884c\uff0c\u975e\u963b\u585e\u7684 Java \u5ba2\u6237\u7aef\u4e00\u822c\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u5904\u7406\u8bf7\u6c42\u7684\u54cd\u5e94\u503c\u3002<\/p><\/blockquote>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u8fd8\u9700\u8981\u548c Es \u4e4b\u95f4\u5efa\u7acb HTTPS \u8fde\u63a5\uff0c\u90a3\u4e48\u9700\u8981\u5728\u524d\u9762\u4ee3\u7801\u7684\u57fa\u7840\u4e4b\u4e0a\uff0c\u518d\u5957\u4e0a\u4e00\u5c42 SSL\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">String fingerprint = &quot;&lt;certificate fingerprint&gt;&quot;;\nSSLContext sslContext = TransportUtils\n    .sslContextFromCaFingerprint(fingerprint); \nBasicCredentialsProvider credsProv = new BasicCredentialsProvider(); \ncredsProv.setCredentials(\n    AuthScope.ANY, new UsernamePasswordCredentials(login, password)\n);\nRestClient restClient = RestClient\n    .builder(new HttpHost(host, port, &quot;https&quot;)) \n    .setHttpClientConfigCallback(hc -&gt; hc\n        .setSSLContext(sslContext) \n        .setDefaultCredentialsProvider(credsProv)\n    )\n    .build();\nElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());\nElasticsearchClient client = new ElasticsearchClient(transport);<\/code><\/pre>\n<p>\u597d\u4e86\uff0c\u5173\u4e8e\u5efa\u7acb\u8fde\u63a5\uff0c\u5dee\u4e0d\u591a\u5c31\u8fd9\u4e9b\u70b9\u3002<\/p>\n<h2>4. \u7d22\u5f15\u64cd\u4f5c<\/h2>\n<p>Elasticsearch Java API Client \u4e2d\u6700\u5927\u7684\u7279\u8272\u5c31\u662f\u5efa\u9020\u8005\u6a21\u5f0f+Lambda \u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c\u6211\u60f3\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\uff0c\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test99() throws IOException {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchClient client = new ElasticsearchClient(transport);\n    CreateIndexResponse createIndexResponse = client.indices().create(\n            c -&gt;\n                    c.index(&quot;javaboy_books&quot;)\n                            .settings(s -&gt;\n                                    s.numberOfShards(&quot;3&quot;)\n                                            .numberOfReplicas(&quot;1&quot;))\n                            .mappings(m -&gt;\n                                    m.properties(&quot;name&quot;, p -&gt; p.text(f -&gt; f.analyzer(&quot;ik_max_word&quot;)))\n                                            .properties(&quot;birthday&quot;, p -&gt; p.date(d -&gt; d.format(&quot;yyyy-MM-dd&quot;))))\n                            .aliases(&quot;books_alias&quot;, f -&gt; f.isWriteIndex(true)));\n    System.out.println(&quot;createResponse.acknowledged() = &quot; + createIndexResponse.acknowledged());\n    System.out.println(&quot;createResponse.index() = &quot; + createIndexResponse.index());\n    System.out.println(&quot;createResponse.shardsAcknowledged() = &quot; + createIndexResponse.shardsAcknowledged());\n}<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u5230\uff0c\u8fd9\u91cc\u90fd\u662f\u5efa\u9020\u8005\u6a21\u5f0f\u548c Lambda \u8868\u8fbe\u5f0f\uff0c\u65b9\u6cd5\u540d\u79f0\u5176\u5b9e\u90fd\u5f88\u597d\u7406\u89e3\uff08\u524d\u63d0\u662f\u4f60\u5f97\u719f\u6089 ElasticSearch \u64cd\u4f5c\u811a\u672c\uff09\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>index \u65b9\u6cd5\u8868\u793a\u8bbe\u7f6e\u7d22\u5f15\u540d\u79f0<\/li>\n<li>settings \u65b9\u6cd5\u8868\u793a\u914d\u7f6e setting \u4e2d\u7684\u53c2\u6570<\/li>\n<li>numberOfShards \u8868\u793a\u7d22\u5f15\u7684\u5206\u7247\u6570<\/li>\n<li>numberOfReplicas \u8868\u793a\u914d\u7f6e\u7d22\u5f15\u7684\u526f\u672c\u6570<\/li>\n<li>mapping \u8868\u793a\u914d\u7f6e\u7d22\u5f15\u4e2d\u7684\u6620\u5c04\u89c4\u5219<\/li>\n<li>properties \u8868\u793a\u914d\u7f6e\u7d22\u5f15\u4e2d\u7684\u5177\u4f53\u5b57\u6bb5<\/li>\n<li>text \u65b9\u6cd5\u8868\u793a\u5b57\u6bb5\u662f text \u7c7b\u578b\u7684<\/li>\n<li>analyzer \u8868\u793a\u914d\u7f6e\u5b57\u6bb5\u7684\u5206\u8bcd\u5668<\/li>\n<li>aliases \u8868\u793a\u914d\u7f6e\u7d22\u5f15\u7684\u522b\u540d<\/li>\n<\/ul>\n<p>\u53cd\u6b63\u8fd9\u91cc\u7684\u65b9\u6cd5\u90fd\u662f\u89c1\u540d\u77e5\u4e49\u7684\uff0c\u4e0a\u9762\u8fd9\u4e2a\u5c31\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"json\">PUT javaboy_books\n{\n  &quot;settings&quot;: {\n    &quot;number_of_replicas&quot;: 1,\n    &quot;number_of_shards&quot;: 3\n  },\n  &quot;mappings&quot;: {\n    &quot;properties&quot;: {\n      &quot;name&quot;:{\n        &quot;type&quot;: &quot;text&quot;,\n        &quot;analyzer&quot;: &quot;ik_max_word&quot;\n      },\n      &quot;birthday&quot;:{\n        &quot;type&quot;: &quot;date&quot;,\n        &quot;format&quot;: &quot;yyyy-MM-dd&quot;\n      }\n    }\n  },\n  &quot;aliases&quot;: {\n    &quot;xxxx&quot;:{\n      \n    }\n  }\n}<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5728\u5199\u7684\u65f6\u5019\uff0c\u8111\u5b50\u91cc\u8981\u5148\u6709\u4e0b\u9762\u8fd9\u4e2a\u811a\u672c\uff0c\u7136\u540e Java \u65b9\u6cd5\u53ef\u4ee5\u987a\u624b\u62c8\u6765\u4e86\u3002<\/p>\n<p>\u6700\u7ec8\u521b\u5efa\u597d\u7684\u7d22\u5f15\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043099133\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6709\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u89c9\u5f97\u8c03\u8fd9\u4e00\u5927\u5806\u65b9\u6cd5\u592a\u5570\u91cc\u5570\u5506\u4e86\uff0c\u6765\u4e2a\u7b80\u5355\u7684\uff0c\u76f4\u63a5\u4e0a JSON\uff0c\u90a3\u4e5f\u4e0d\u662f\u4e0d\u53ef\u4ee5\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test98() throws IOException {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchClient client = new ElasticsearchClient(transport);\n    StringReader json = new StringReader(&quot;{\/n&quot; +\n            &quot;  \/&quot;settings\/&quot;: {\/n&quot; +\n            &quot;    \/&quot;number_of_replicas\/&quot;: 1,\/n&quot; +\n            &quot;    \/&quot;number_of_shards\/&quot;: 3\/n&quot; +\n            &quot;  },\/n&quot; +\n            &quot;  \/&quot;mappings\/&quot;: {\/n&quot; +\n            &quot;    \/&quot;properties\/&quot;: {\/n&quot; +\n            &quot;      \/&quot;name\/&quot;:{\/n&quot; +\n            &quot;        \/&quot;type\/&quot;: \/&quot;text\/&quot;,\/n&quot; +\n            &quot;        \/&quot;analyzer\/&quot;: \/&quot;ik_max_word\/&quot;\/n&quot; +\n            &quot;      },\/n&quot; +\n            &quot;      \/&quot;birthday\/&quot;:{\/n&quot; +\n            &quot;        \/&quot;type\/&quot;: \/&quot;date\/&quot;,\/n&quot; +\n            &quot;        \/&quot;format\/&quot;: \/&quot;yyyy-MM-dd\/&quot;\/n&quot; +\n            &quot;      }\/n&quot; +\n            &quot;    }\/n&quot; +\n            &quot;  },\/n&quot; +\n            &quot;  \/&quot;aliases\/&quot;: {\/n&quot; +\n            &quot;    \/&quot;xxxx\/&quot;:{\/n&quot; +\n            &quot;      \/n&quot; +\n            &quot;    }\/n&quot; +\n            &quot;  }\/n&quot; +\n            &quot;}&quot;);\n    CreateIndexResponse createIndexResponse = client.indices().create(\n            c -&gt;\n                    c.index(&quot;javaboy_books&quot;).withJson(json));\n    System.out.println(&quot;createResponse.acknowledged() = &quot; + createIndexResponse.acknowledged());\n    System.out.println(&quot;createResponse.index() = &quot; + createIndexResponse.index());\n    System.out.println(&quot;createResponse.shardsAcknowledged() = &quot; + createIndexResponse.shardsAcknowledged());\n}<\/code><\/pre>\n<p>\u8fd9\u662f\u76f4\u63a5\u628a JSON \u53c2\u6570\u7ed9\u62fc\u63a5\u51fa\u6765\uff0c\u5c31\u4e0d\u9700\u8981\u4e00\u5806\u5efa\u9020\u8005+Lambda \u4e86\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u5220\u9664\u7d22\u5f15\u5462\uff1f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test06() throws IOException {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchClient client = new ElasticsearchClient(transport);\n    \/\/\u5220\u9664\u4e00\u4e2a\u7d22\u5f15\n    DeleteIndexResponse delete = client.indices().delete(f -&gt;\n            f.index(&quot;my-index&quot;)\n    );\n    System.out.println(&quot;delete.acknowledged() = &quot; + delete.acknowledged());\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8868\u793a\u5220\u9664\u4e00\u4e2a\u540d\u4e3a <code>my-index<\/code> \u7684\u7d22\u5f15\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u5173\u4e8e\u7d22\u5f15\u7684\u64cd\u4f5c\u6211\u5c31\u8bf4\u8fd9\u4e24\u70b9\u3002<\/p>\n<p>\u53ef\u80fd\u6709\u7684\u5c0f\u4f19\u4f34\u4f1a\u8bf4\uff0cElasticSearch \u4e2d\u521b\u5efa\u7d22\u5f15\u53ef\u4ee5\u914d\u7f6e\u5f88\u591a\u53c2\u6570\u4f60\u90fd\u6ca1\u8bb2\u3002\u5728\u6211\u770b\u6765\uff0c\u54ea\u4e9b\u5f88\u591a\u53c2\u6570\u5176\u5b9e\u8ddf\u8fd9\u4e2a Java API \u6ca1\u6709\u591a\u5927\u5173\u7cfb\uff0c\u53ea\u8981\u4f60\u4f1a\u5199\u67e5\u8be2\u811a\u672c\uff0c\u5c31\u81ea\u7136\u61c2\u5f97 Java API \u4e2d\u8be5\u8c03\u7528\u54ea\u4e2a\u65b9\u6cd5\uff0c\u9000\u4e00\u4e07\u6b65\u8bb2\uff0c\u4f60\u4f1a\u811a\u672c\uff0c\u4e0d\u61c2 Java API \u7684\u65b9\u6cd5\uff0c\u90a3\u4e48\u5c31\u50cf\u4e0a\u9762\u90a3\u6837\uff0c\u76f4\u63a5\u628a\u4f60\u7684 JSON \u62f7\u8d1d\u8fc7\u6765\uff0c\u4f5c\u4e3a Java API \u7684\u53c2\u6570\u5373\u53ef\u3002<\/p>\n<h2>5. \u6587\u6863\u64cd\u4f5c<\/h2>\n<h3>5.1 \u6dfb\u52a0\u6587\u6863<\/h3>\n<p>\u5148\u6765\u770b\u6587\u6863\u7684\u6dfb\u52a0\u64cd\u4f5c\u3002<\/p>\n<p>\u5982\u4e0b\u8868\u793a\u6211\u60f3\u7ed9\u4e00\u4e2a\u540d\u4e3a books \u7684\u7d22\u5f15\u4e2d\u6dfb\u52a0\u4e00\u4e2a id \u4e3a 890 \u7684\u4e66\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test07() throws IOException {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchClient client = new ElasticsearchClient(transport);\n    Book book = new Book();\n    book.setId(890);\n    book.setName(&quot;\u6df1\u5165\u7406\u89e3Java\u865a\u62df\u673a&quot;);\n    book.setAuthor(&quot;xxx&quot;);\n    \/\/\u6dfb\u52a0\u4e00\u4e2a\u6587\u6863\n    \/\/\u8fd9\u662f\u4e00\u4e2a\u540c\u6b65\u8bf7\u6c42\uff0c\u8bf7\u6c42\u4f1a\u5361\u5728\u8fd9\u91cc\n    IndexResponse response = client.index(i -&gt; i.index(&quot;books&quot;).document(book).id(&quot;890&quot;));\n    System.out.println(&quot;response.result() = &quot; + response.result());\n    System.out.println(&quot;response.id() = &quot; + response.id());\n    System.out.println(&quot;response.seqNo() = &quot; + response.seqNo());\n    System.out.println(&quot;response.index() = &quot; + response.index());\n    System.out.println(&quot;response.shards() = &quot; + response.shards());\n}<\/code><\/pre>\n<p>\u6dfb\u52a0\u6210\u529f\u4e4b\u540e\uff0c\u8fd4\u56de\u7684 IndexResponse \u5bf9\u8c61\u5176\u5b9e\u5c31\u662f\u5bf9\u4e0b\u9762\u8fd9\u4e2a JSON \u7684\u5c01\u88c5\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043099134\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u73b0\u5728\u6211\u4eec\u53ea\u9700\u8981\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230 JSON \u76f8\u5173\u7684\u5c5e\u6027\u4e86\u3002<\/p>\n<h3>5.2 \u5220\u9664\u6587\u6863<\/h3>\n<p>\u5982\u4e0b\u8868\u793a\u5220\u9664 books \u7d22\u5f15\u4e2d id \u4e3a 891 \u7684\u6587\u6863\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test09() {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchAsyncClient client = new ElasticsearchAsyncClient(transport);\n    client.delete(d -&gt; d.index(&quot;books&quot;).id(&quot;891&quot;)).whenComplete((resp, e) -&gt; {\n        System.out.println(&quot;resp.result() = &quot; + resp.result());\n    });\n}<\/code><\/pre>\n<p>\u5220\u9664\u8fd9\u91cc\u6211\u7528\u4e86\u5f02\u6b65\u975e\u963b\u585e\u7684\u5ba2\u6237\u7aef\u6765\u7ed9\u5c0f\u4f19\u4f34\u4eec\u6f14\u793a\u7684\uff0c\u5f02\u6b65\u975e\u963b\u585e\u7684\u8bdd\uff0c\u5c31\u4f7f\u7528 whenComplete \u65b9\u6cd5\u5904\u7406\u56de\u8c03\u5c31\u884c\u4e86\uff0c\u91cc\u8fb9\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f\u6b63\u5e38\u60c5\u51b5\u4e0b\u8fd4\u56de\u7684\u5bf9\u8c61\uff0c\u53e6\u5916\u4e00\u4e2a\u5219\u662f\u51fa\u9519\u65f6\u5019\u7684\u5f02\u5e38\u3002<\/p>\n<h3>5.3 \u67e5\u8be2\u6587\u6863<\/h3>\n<p>\u6700\u540e\uff0c\u5c31\u662f\u67e5\u8be2\u4e86\u3002\u8fd9\u5e94\u8be5\u662f\u5927\u5bb6\u65e5\u5e38\u5f00\u53d1\u4e2d\u4f7f\u7528\u8f83\u591a\u7684\u529f\u80fd\u9879\u4e86\uff0c\u4e0d\u8fc7\u6211\u8fd8\u662f\u524d\u9762\u7684\u6001\u5ea6\uff0c\u67e5\u8be2\u7684\u5173\u952e\u4e0d\u5728 Java API\uff0c\u800c\u5728\u4e8e\u4f60\u5bf9 ElasticSearch \u811a\u672c\u7684\u638c\u63e1\u7a0b\u5ea6\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u8fd9\u91cc\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c0f\u4f19\u4f34\u4eec\u5927\u81f4\u4e86\u89e3\u4e0b Java API \u7684\u65b9\u6cd5\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test01() throws IOException {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchClient client = new ElasticsearchClient(transport);\n    SearchRequest request = new SearchRequest.Builder()\n            \/\/\u53bb\u54ea\u4e2a\u7d22\u5f15\u91cc\u641c\u7d22\n            .index(&quot;books&quot;)\n            .query(QueryBuilders.term().field(&quot;name&quot;).value(&quot;java&quot;).build()._toQuery())\n            .build();\n    SearchResponse&lt;Book&gt; search = client.search(request, Book.class);\n    System.out.println(&quot;search.toString() = &quot; + search.toString());\n    long took = search.took();\n    System.out.println(&quot;took = &quot; + took);\n    boolean b = search.timedOut();\n    System.out.println(&quot;b = &quot; + b);\n    ShardStatistics shards = search.shards();\n    System.out.println(&quot;shards = &quot; + shards);\n    HitsMetadata&lt;Book&gt; hits = search.hits();\n    TotalHits total = hits.total();\n    System.out.println(&quot;total = &quot; + total);\n    Double maxScore = hits.maxScore();\n    System.out.println(&quot;maxScore = &quot; + maxScore);\n    List&lt;Hit&lt;Book&gt;&gt; list = hits.hits();\n    for (Hit&lt;Book&gt; bookHit : list) {\n        System.out.println(&quot;bookHit.source() = &quot; + bookHit.source());\n        System.out.println(&quot;bookHit.score() = &quot; + bookHit.score());\n        System.out.println(&quot;bookHit.index() = &quot; + bookHit.index());\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e00\u4e2a term \u67e5\u8be2\uff0c\u67e5\u8be2 books \u7d22\u5f15\u4e2d\u4e66\u540d name \u4e2d\u5305\u542b java \u5173\u952e\u5b57\u7684\u56fe\u4e66\uff0c\u7b49\u4ef7\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u67e5\u8be2\uff1a<\/p>\n<pre><code class=\"json\">GET books\/_search\n{\n  &quot;query&quot;: {\n    &quot;term&quot;: {\n      &quot;name&quot;: {\n        &quot;value&quot;: &quot;java&quot;\n      }\n    }\n  }\n}<\/code><\/pre>\n<p>\u5982\u679c\u5e0c\u671b\u80fd\u591f\u5bf9\u67e5\u8be2\u5173\u952e\u5b57\u5206\u8bcd\u4e4b\u540e\u67e5\u8be2\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 match \u67e5\u8be2\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test03() throws IOException {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchClient client = new ElasticsearchClient(transport);\n    SearchResponse&lt;Book&gt; search = client.search(s -&gt; {\n        s.index(&quot;books&quot;)\n                .query(q -&gt; {\n                    q.match(m -&gt; {\n                        m.field(&quot;name&quot;).query(&quot;\u7f8e\u672f\u8ba1\u7b97\u673a&quot;);\n                        return m;\n                    });\n                    return q;\n                });\n        return s;\n    }, Book.class);\n    System.out.println(&quot;search.toString() = &quot; + search.toString());\n    long took = search.took();\n    System.out.println(&quot;took = &quot; + took);\n    boolean b = search.timedOut();\n    System.out.println(&quot;b = &quot; + b);\n    ShardStatistics shards = search.shards();\n    System.out.println(&quot;shards = &quot; + shards);\n    HitsMetadata&lt;Book&gt; hits = search.hits();\n    TotalHits total = hits.total();\n    System.out.println(&quot;total = &quot; + total);\n    Double maxScore = hits.maxScore();\n    System.out.println(&quot;maxScore = &quot; + maxScore);\n    List&lt;Hit&lt;Book&gt;&gt; list = hits.hits();\n    for (Hit&lt;Book&gt; bookHit : list) {\n        System.out.println(&quot;bookHit.source() = &quot; + bookHit.source());\n        System.out.println(&quot;bookHit.score() = &quot; + bookHit.score());\n        System.out.println(&quot;bookHit.index() = &quot; + bookHit.index());\n    }\n}<\/code><\/pre>\n<blockquote><p>\u4e3a\u4e86\u8ba9\u5c0f\u4f19\u4f34\u4eec\u770b\u5230\u8fd9\u4e2a Java \u5ba2\u6237\u7aef\u7684\u4e0d\u540c\u7528\u6cd5\uff0c\u4e0a\u9762\u4e24\u4e2a\u67e5\u8be2\u7684\u4f8b\u5b50\uff0c\u6211\u5206\u522b\u4f7f\u7528\u4e86\u6784\u9020\u67e5\u8be2\u8bf7\u6c42\u548c\u5efa\u9020\u8005+Lambda \u7684\u65b9\u5f0f\u3002<\/p><\/blockquote>\n<p>match \u67e5\u8be2\u5c31\u8c03\u7528 match \u65b9\u6cd5\u5c31\u884c\u4e86\uff0c\u8bbe\u7f6e\u67e5\u8be2\u5173\u952e\u5b57\u5373\u53ef\uff0c\u8fd9\u4e2a\u67e5\u8be2\u7b49\u4ef7\u4e8e\u4e0b\u9762\u8fd9\u4e2a\u67e5\u8be2\uff1a<\/p>\n<pre><code class=\"json\">GET books\/_search\n{\n  &quot;query&quot;: {\n    &quot;match&quot;: {\n      &quot;name&quot;: &quot;\u7f8e\u672f\u8ba1\u7b97\u673a&quot;\n    }\n  }\n}<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u89c9\u5f97\u8fd9\u79cd\u8c03\u7528\u5404\u79cd\u65b9\u6cd5\u62fc\u63a5\u53c2\u6570\u7684\u65b9\u5f0f\u4e0d\u4e60\u60ef\uff0c\u90a3\u4e48\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4e0a JSON\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Test\npublic void test04() throws IOException {\n    RestClient restClient = RestClient.builder(\n            new HttpHost(&quot;localhost&quot;, 9200)).build();\n    ElasticsearchTransport transport = new RestClientTransport(\n            restClient, new JacksonJsonpMapper());\n    ElasticsearchClient client = new ElasticsearchClient(transport);\n    String key = &quot;java&quot;;\n    StringReader sr = new StringReader(&quot;{\/n&quot; +\n            &quot;  \/&quot;query\/&quot;: {\/n&quot; +\n            &quot;    \/&quot;term\/&quot;: {\/n&quot; +\n            &quot;      \/&quot;name\/&quot;: {\/n&quot; +\n            &quot;        \/&quot;value\/&quot;: \/&quot;&quot; + key + &quot;\/&quot;\/n&quot; +\n            &quot;      }\/n&quot; +\n            &quot;    }\/n&quot; +\n            &quot;  }\/n&quot; +\n            &quot;}&quot;);\n    SearchRequest request = new SearchRequest.Builder()\n            .withJson(sr)\n            .build();\n    SearchResponse&lt;Book&gt; search = client.search(request, Book.class);\n    System.out.println(&quot;search.toString() = &quot; + search.toString());\n    long took = search.took();\n    System.out.println(&quot;took = &quot; + took);\n    boolean b = search.timedOut();\n    System.out.println(&quot;b = &quot; + b);\n    ShardStatistics shards = search.shards();\n    System.out.println(&quot;shards = &quot; + shards);\n    HitsMetadata&lt;Book&gt; hits = search.hits();\n    TotalHits total = hits.total();\n    System.out.println(&quot;total = &quot; + total);\n    Double maxScore = hits.maxScore();\n    System.out.println(&quot;maxScore = &quot; + maxScore);\n    List&lt;Hit&lt;Book&gt;&gt; list = hits.hits();\n    for (Hit&lt;Book&gt; bookHit : list) {\n        System.out.println(&quot;bookHit.source() = &quot; + bookHit.source());\n        System.out.println(&quot;bookHit.score() = &quot; + bookHit.score());\n        System.out.println(&quot;bookHit.index() = &quot; + bookHit.index());\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u76f4\u63a5\u628a\u67e5\u8be2\u7684 JSON \u53c2\u6570\u4f20\u8fdb\u6765\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5148\u5728 Kibana \u4e2d\u5199\u597d\u811a\u672c\uff0c\u7136\u540e\u76f4\u63a5\u5c06\u811a\u672c\u62f7\u8d1d\u5230 Java \u4ee3\u7801\u4e2d\u6765\u6267\u884c\u5c31\u884c\u4e86\u3002<\/p>\n<p>\u597d\u5566\uff0c\u5173\u4e8e Es \u4e2d\u65b0\u7684 Java \u5ba2\u6237\u7aef\uff0c\u6211\u5c31\u548c\u5927\u5bb6\u8bf4\u8fd9\u4e48\u591a\uff0c\u6700\u540e\u518d\u5f3a\u8c03\u4e00\u4e0b\uff0c\u8fd9\u5176\u5b9e\u4e0d\u662f\u91cd\u70b9\uff0c\u73a9 Es \u7684\u91cd\u70b9\u662f\u628a Es \u7684\u5404\u79cd\u67e5\u8be2\u53c2\u6570\u641e\u61c2\uff0c\u90a3\u4e48 Java \u4ee3\u7801\u5176\u5b9e\u5c31\u662f\u987a\u624b\u62c8\u6765\u7684\u4e8b\u4e86\u3002<\/p>\n<p>\u6700\u540e\uff0c\u5982\u679c\u5927\u5bb6\u5bf9 Es \u4e0d\u719f\u6089\uff0c\u53ef\u4ee5\u770b\u770b\u677e\u54e5\u5f55\u7684\u8fd9\u4e2a\u514d\u8d39\u89c6\u9891\u6559\u7a0b\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.bilibili.com\/video\/BV1ft4y1e7tq\/\">https:\/\/www.bilibili.com\/video\/BV1ft4y1e7tq\/<\/a><\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043099135\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043099130\">https:\/\/segmentfault.com\/a\/1190000043099130<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u80fd\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u6ce8\u610f\u5230\u4e86\uff0c\u4ece ElasticSearch7.17 \u8fd9\u4e2a\u7248\u672c\u5f00\u59cb\uff0c\u539f\u5148\u7684 Java \u9ad8\u7ea7\u5ba2\u6237\u7aef  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006980","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006980"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006980\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}