├── .gitignore
├── README.md
├── SUMMARY.md
├── assets
├── Cover_1600_2400.jpg
├── Cover_400_600.jpg
├── Cover_800_1200.jpg
└── qrcode_for_gh_26893aa0a4ea_258.jpg
├── java-high-level-rest-client.md
├── java-high-level-rest-client
├── getting-started.md
├── getting-started
│ ├── compatibility.md
│ ├── dependencies.md
│ ├── initialization.md
│ ├── javadoc.md
│ └── maven-repository.md
├── migration-guide.md
├── migration-guide
│ ├── changing-the-applications-code.md
│ ├── changing-the-clients-initialization-code.md
│ ├── how-to-migrate.md
│ ├── motivations-around-a-new-java-client.md
│ ├── prerequisite.md
│ ├── provide-feedback.md
│ └── updating-the-dependencies.md
├── supported-apis.md
├── supported-apis
│ ├── bulk-api.md
│ ├── clear-scroll-api.md
│ ├── delete-api.md
│ ├── get-api.md
│ ├── index-api.md
│ ├── info-api.md
│ ├── search-api.md
│ ├── search-scroll-api.md
│ └── update-api.md
├── using-java-builders.md
└── using-java-builders
│ ├── building-aggregations.md
│ └── building-queries.md
├── java-low-level-rest-client.md
├── java-low-level-rest-client
├── common-configuration.md
├── common-configuration
│ ├── basic-authentication.md
│ ├── encrypted-communication.md
│ ├── number-of-threads.md
│ ├── others.md
│ └── timeouts.md
├── getting-started.md
├── getting-started
│ ├── dependencies.md
│ ├── initialization.md
│ ├── javadoc.md
│ ├── logging.md
│ ├── maven-repository.md
│ ├── performing-requests.md
│ ├── reading-responses.md
│ └── shading.md
├── sniffer.md
└── sniffer
│ ├── javadoc.md
│ ├── maven-repository.md
│ └── usage.md
└── overview.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Node rules:
2 | ## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
3 | .grunt
4 |
5 | ## Dependency directory
6 | ## Commenting this out is preferred by some people, see
7 | ## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git
8 | node_modules
9 |
10 | # Book build output
11 | _book
12 |
13 | # eBook build output
14 | *.epub
15 | *.mobi
16 | *.pdf
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Elasticsearch Java Rest API 手册
2 |
3 | 
4 |
5 | 本手册由 [全科](http://quanke.name) 翻译,并且整理成电子书,支持PDF,ePub,Mobi格式,方便大家下载阅读。
6 |
7 | 阅读地址:[https://es.quanke.name](https://es.quanke.name)
8 |
9 | 下载地址:[https://www.gitbook.com/book/quanke/elasticsearch-java-rest](https://www.gitbook.com/book/quanke/elasticsearch-java-rest)
10 |
11 | github地址:[https://github.com/quanke/elasticsearch-java-rest](https://github.com/quanke/elasticsearch-java-rest)
12 |
13 | gitee 地址:[https://gitee.com/quanke/elasticsearch-java-rest](https://gitee.com/quanke/elasticsearch-java-rest)
14 |
15 | 编辑:[http://quanke.name](http://quanke.name)
16 |
17 | 编辑整理辛苦,还望大神们点一下star ,抚平我虚荣的心
18 |
19 | > 不只是官方文档的翻译,还包含使用实例,包含我们使用踩过的坑
20 |
21 | 更多请关注我的微信公众号:
22 |
23 | 
24 |
25 |
--------------------------------------------------------------------------------
/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # Summary
2 |
3 | * [Introduction](README.md)
4 | * [Overview](overview.md)
5 | * [Java Low Level REST Client](java-low-level-rest-client.md)
6 | * [Getting started](java-low-level-rest-client/getting-started.md)
7 | * [Javadoc](java-low-level-rest-client/getting-started/javadoc.md)
8 | * [Maven Repository](java-low-level-rest-client/getting-started/maven-repository.md)
9 | * [Dependencies](java-low-level-rest-client/getting-started/dependencies.md)
10 | * [Shading](java-low-level-rest-client/getting-started/shading.md)
11 | * [Initialization](java-low-level-rest-client/getting-started/initialization.md)
12 | * [Performing requests](java-low-level-rest-client/getting-started/performing-requests.md)
13 | * [Reading responses](java-low-level-rest-client/getting-started/reading-responses.md)
14 | * [Logging](java-low-level-rest-client/getting-started/logging.md)
15 | * [Common configuration](java-low-level-rest-client/common-configuration.md)
16 | * [Timeouts](java-low-level-rest-client/common-configuration/timeouts.md)
17 | * [Number of threads](java-low-level-rest-client/common-configuration/number-of-threads.md)
18 | * [Basic authentication](java-low-level-rest-client/common-configuration/basic-authentication.md)
19 | * [Encrypted communication](java-low-level-rest-client/common-configuration/encrypted-communication.md)
20 | * [Others](java-low-level-rest-client/common-configuration/others.md)
21 | * [Sniffer](java-low-level-rest-client/sniffer.md)
22 | * [Javadoc](java-low-level-rest-client/sniffer/javadoc.md)
23 | * [Maven Repository](java-low-level-rest-client/sniffer/maven-repository.md)
24 | * [Usage](java-low-level-rest-client/sniffer/usage.md)
25 | * [Java High Level REST Client](java-high-level-rest-client.md)
26 | * [Getting started](java-high-level-rest-client/getting-started.md)
27 | * [Compatibility](java-high-level-rest-client/getting-started/compatibility.md)
28 | * [Javadoc](java-high-level-rest-client/getting-started/javadoc.md)
29 | * [Maven Repository](java-high-level-rest-client/getting-started/maven-repository.md)
30 | * [Dependencies](java-high-level-rest-client/getting-started/dependencies.md)
31 | * [Initialization](java-high-level-rest-client/getting-started/initialization.md)
32 | * [Supported APIs](java-high-level-rest-client/supported-apis.md)
33 | * [Index API](java-high-level-rest-client/supported-apis/index-api.md)
34 | * [Get API](java-high-level-rest-client/supported-apis/get-api.md)
35 | * [Delete API](java-high-level-rest-client/supported-apis/delete-api.md)
36 | * [Update API](java-high-level-rest-client/supported-apis/update-api.md)
37 | * [Bulk API](java-high-level-rest-client/supported-apis/bulk-api.md)
38 | * [Search API](java-high-level-rest-client/supported-apis/search-api.md)
39 | * [Search Scroll API](java-high-level-rest-client/supported-apis/search-scroll-api.md)
40 | * [Clear Scroll API](java-high-level-rest-client/supported-apis/clear-scroll-api.md)
41 | * [Info API](java-high-level-rest-client/supported-apis/info-api.md)
42 | * [Using Java Builders](java-high-level-rest-client/using-java-builders.md)
43 | * [Building Queries](java-high-level-rest-client/using-java-builders/building-queries.md)
44 | * [Building Aggregations](java-high-level-rest-client/using-java-builders/building-aggregations.md)
45 | * [Migration Guide](java-high-level-rest-client/migration-guide.md)
46 | * [Motivations around a new Java client](java-high-level-rest-client/migration-guide/motivations-around-a-new-java-client.md)
47 | * [Prerequisite](java-high-level-rest-client/migration-guide/prerequisite.md)
48 | * [How to migrate](java-high-level-rest-client/migration-guide/how-to-migrate.md)
49 | * [Updating the dependencies](java-high-level-rest-client/migration-guide/updating-the-dependencies.md)
50 | * [Changing the client’s initialization code](java-high-level-rest-client/migration-guide/changing-the-clients-initialization-code.md)
51 | * [Changing the application’s code](java-high-level-rest-client/migration-guide/changing-the-applications-code.md)
52 | * [Provide feedback](java-high-level-rest-client/migration-guide/provide-feedback.md)
53 |
54 |
--------------------------------------------------------------------------------
/assets/Cover_1600_2400.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/quanke/elasticsearch-java-rest/c76f985ea7e8661a462259d5fb9c9a4b336728e5/assets/Cover_1600_2400.jpg
--------------------------------------------------------------------------------
/assets/Cover_400_600.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/quanke/elasticsearch-java-rest/c76f985ea7e8661a462259d5fb9c9a4b336728e5/assets/Cover_400_600.jpg
--------------------------------------------------------------------------------
/assets/Cover_800_1200.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/quanke/elasticsearch-java-rest/c76f985ea7e8661a462259d5fb9c9a4b336728e5/assets/Cover_800_1200.jpg
--------------------------------------------------------------------------------
/assets/qrcode_for_gh_26893aa0a4ea_258.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/quanke/elasticsearch-java-rest/c76f985ea7e8661a462259d5fb9c9a4b336728e5/assets/qrcode_for_gh_26893aa0a4ea_258.jpg
--------------------------------------------------------------------------------
/java-high-level-rest-client.md:
--------------------------------------------------------------------------------
1 |
2 | # Java 高级 REST 客户端
3 |
4 | Java高级REST客户端可以在Java Low Level REST客户机之上工作。 其主要目标是公开特定方法的API,接受请求对象作为参数并返回响应对象,以便客户端自己处理请求编组和响应解组。
5 |
6 | 每个API可以同步或异步地调用。 同步方法返回一个响应对象,而名称以 `async` 后缀结尾的异步方法需要收到响应或错误后才会通知(在低级别客户端管理的线程池上)的侦听器参数。
7 |
8 | Java高级REST客户端依赖于 `Elasticsearch` 核心项目。 它接受与 `TransportClient` 相同的请求参数,并返回相同的响应对象。
9 |
--------------------------------------------------------------------------------
/java-high-level-rest-client/getting-started.md:
--------------------------------------------------------------------------------
1 | # 起步
2 |
3 | 本节介绍了如何在应用程序中使用高级REST客户端。
--------------------------------------------------------------------------------
/java-high-level-rest-client/getting-started/compatibility.md:
--------------------------------------------------------------------------------
1 | # 兼容性
2 |
3 | Java高级REST客户端需要Java 1.8,并依赖于Elasticsearch核心项目。 客户端版本要与客户端开发的Elasticsearch版本相同。 它接受与 `TransportClient` 相同的请求参数,并返回相同的响应对象。 如果需要将应用程序从TransportClient迁移到新的REST客户端,请参阅 [`迁移指南`](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-level-migration.html)。
4 |
5 | 高级客户端保证能够与运行在相同主版本和大于或等于次要版本的任何Elasticsearch节点进行通信。它不需要与它进行通信的弹性搜索节点相同的次要版本,因为它是向前兼容的,意味着它支持与之前开发的弹性搜索的更新版本进行通信。
6 |
7 | 5.6 客户端可以与任何 5.6.x Elasticsearch 节点进行通信。以前的 5.x 小版本,如 5.5.x,5.4.x 等不(完全)支持。
8 |
9 | 6.0 客户端能够与任何 6.x Elasticsearch 节点进行通信,而 6.1 客户端确实能够与 6.1,6.2 和以后的 6.x 版本进行通信,但与以前的 Elasticsearch 节点版本通信时可能会出现不兼容问题例如 6.1 到 6.0 之间,例如 6.1 客户端支持而 6.0 节点不知道的某些API的新请求主体字段。
10 |
11 | 建议在将Elasticsearch集群升级到新的主要版本时升级高级客户端,因为REST API突破性更改可能会导致意外的结果,具体取决于请求所击中的节点,新添加的API只能由较新版本的客户端。一旦群集中的所有节点都升级到新的主版本,则客户端应当更新。
--------------------------------------------------------------------------------
/java-high-level-rest-client/getting-started/dependencies.md:
--------------------------------------------------------------------------------
1 | # 依赖项
2 |
3 | 高级 Java REST Client 依赖以下包:
4 |
5 | - org.elasticsearch.client:elasticsearch-rest-client
6 | - org.elasticsearch:elasticsearch
--------------------------------------------------------------------------------
/java-high-level-rest-client/getting-started/initialization.md:
--------------------------------------------------------------------------------
1 | # 初始化
2 |
3 | RestHighLevelClient 实例的构建需要一个 REST 低级客户端就像下面这样:
4 |
5 | ```
6 | RestHighLevelClient client =
7 | new RestHighLevelClient(lowLevelRestClient); //lowLevelRestClient: 我们之前创建的 [REST低级客户端](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-initialization.html) 实例
8 | ```
9 |
10 | 在本文档中关于Java高级客户端的的其余部分里,`RestHighLevelClient` 实例将以 `client` 被引用。
11 |
--------------------------------------------------------------------------------
/java-high-level-rest-client/getting-started/javadoc.md:
--------------------------------------------------------------------------------
1 | # Javadoc
2 |
3 | https://artifacts.elastic.co/javadoc/org/elasticsearch/client/elasticsearch-rest-high-level-client/5.6.0/index.html
4 |
--------------------------------------------------------------------------------
/java-high-level-rest-client/getting-started/maven-repository.md:
--------------------------------------------------------------------------------
1 |
2 | # Maven 仓库
3 |
4 | 高级 Java REST 客户端被托管在 Maven 中央仓库里。所需的最低Java版本为 1.8。
5 |
6 | 高级 REST 客户端与 elasticsearch 的发行周期相同。可以使用期望的版本进行替换。
7 |
8 | ## Maven 配置
9 |
10 | 若使用 Maven 作依赖管理,你可以这样配置依赖。将下列内容添加到你的 pom.xml 文件里:
11 |
12 | ```
13 |
14 | org.elasticsearch.client
15 | elasticsearch-rest-high-level-client
16 | 5.6.0
17 |
18 | ```
19 |
20 | ## Gradle 配置
21 |
22 | 若使用 gradle 作依赖管理,你可以这样配置依赖。将下列内容添加到你的 build.gradle 文件里:
23 |
24 | ```
25 | dependencies {
26 | compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:5.6.0'
27 | }
28 | ```
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide.md:
--------------------------------------------------------------------------------
1 | 迁移指南
2 |
3 | 本节介绍如何将现有代码从 TransportClient 迁移到使用 Elasticsearch 5.6.0 版本发布的新的 Java 高级 REST 客户端。
4 |
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide/changing-the-applications-code.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/quanke/elasticsearch-java-rest/c76f985ea7e8661a462259d5fb9c9a4b336728e5/java-high-level-rest-client/migration-guide/changing-the-applications-code.md
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide/changing-the-clients-initialization-code.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/quanke/elasticsearch-java-rest/c76f985ea7e8661a462259d5fb9c9a4b336728e5/java-high-level-rest-client/migration-guide/changing-the-clients-initialization-code.md
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide/how-to-migrate.md:
--------------------------------------------------------------------------------
1 | # 怎样合并
2 |
3 | 调整现有代码以使用RestHighLevelClient而不是TransportClient需要以下步骤:
4 |
5 | - 更新依赖关系
6 | - 更新客户端初始化
7 | - 更新应用程序代码
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide/motivations-around-a-new-java-client.md:
--------------------------------------------------------------------------------
1 | # 使用一个新的Java客户端的动机
2 |
3 | 自从第一次提交以来,现有的TransportClient已经成为Elasticsearch的一部分。 它是一个特殊的客户端,因为它使用传输协议与Elasticsearch进行通信,如果客户端的Elasticsearch实例与Elasticsearch实例的版本不同,则会导致兼容性问题。
4 |
5 | 我们在2016年发布了一个低级别的REST客户端,它基于众所周知的Apache HTTP客户端,并且允许在任何版本中使用HTTP与Elasticsearch集群进行通信。 最重要的是,我们发布了基于低级客户端的高级REST客户端,但是负责请求编组和响应解组。
6 |
7 | 如果您有兴趣了解更多关于这些变化的信息,我们写了一篇关于官方Elasticsearch Java客户端状态的博客文章。
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide/prerequisite.md:
--------------------------------------------------------------------------------
1 | # 条件
2 |
3 | Java 1.8
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide/provide-feedback.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/quanke/elasticsearch-java-rest/c76f985ea7e8661a462259d5fb9c9a4b336728e5/java-high-level-rest-client/migration-guide/provide-feedback.md
--------------------------------------------------------------------------------
/java-high-level-rest-client/migration-guide/updating-the-dependencies.md:
--------------------------------------------------------------------------------
1 | # 更新依赖
2 |
3 | 使用TransportClient的Java应用程序依赖于org.elasticsearch.client:transport artifact。 这个依赖关系必须被高级客户端的新依赖所取代。
4 |
5 | “入门”页面显示了Maven和Gradle的典型配置,并介绍了高级客户端提供的依赖关系。
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis.md:
--------------------------------------------------------------------------------
1 |
2 | # 支持的 API
3 |
4 | Java 高级 REST 客户端支持下列 API :
5 |
6 | 单一文档 API
7 |
8 | - [Index API](supported-apis/index-api.md)
9 | - [Get API](supported-apis/get-api.md)
10 | - [Delete API](supported-apis/delete-api.md)
11 | - [Update API](supported-apis/update-api.md)
12 |
13 | 多文档 API
14 |
15 | - [Bulk API](supported-apis/bulk-api.md)
16 |
17 | 搜索 API
18 |
19 | - [Search API](supported-apis/search-api.md)
20 | - [Search Scroll API](supported-apis/search-scroll-api.md)
21 | - [Clear Scroll API](supported-apis/clear-scroll-api.md)
22 |
23 | 各种 API
24 |
25 | - [Info API](supported-apis/info-api.md)
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis/bulk-api.md:
--------------------------------------------------------------------------------
1 |
2 | # Bulk API
3 |
4 | 注意: Java高级REST客户端提供批量处理器来协助大量请求
5 |
6 | ## Bulk 请求
7 |
8 | BulkRequest 可以被用在使用单个请求执行多个 索引,更新 和/或 删除 操作的情况下。
9 |
10 | 它要求至少要一个操作被添加到 Bulk 请求上:
11 |
12 | ```
13 | BulkRequest request = new BulkRequest(); // 创建 BulkRequest
14 | request.add(new IndexRequest("posts", "doc", "1") // 添加第一个 IndexRequest 到 Bulk 请求上, 参看 [Index API](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html) 获取更多关于如何构建 IndexRequest 的信息.
15 | .source(XContentType.JSON,"field", "foo"));
16 | request.add(new IndexRequest("posts", "doc", "2") // 添加第二个 IndexRequest
17 | .source(XContentType.JSON,"field", "bar"));
18 | request.add(new IndexRequest("posts", "doc", "3") // 添加第三个 IndexRequest
19 | .source(XContentType.JSON,"field", "baz"));
20 | ```
21 |
22 | > 警告: Bulk API仅支持以JSON或SMILE编码的文档。 以任何其他格式提供文件将导致错误。
23 |
24 | 不同的操作类型也可以添加到同一个BulkRequest中:
25 |
26 | ```
27 | BulkRequest request = new BulkRequest();
28 | request.add(new DeleteRequest("posts", "doc", "3")); //
29 | Adds a DeleteRequest to the BulkRequest. See Delete API for more information on how to build DeleteRequest.
30 | request.add(new UpdateRequest("posts", "doc", "2")
31 | .doc(XContentType.JSON,"other", "test")); //
32 | Adds an UpdateRequest to the BulkRequest. See Update API for more information on how to build UpdateRequest.
33 | request.add(new IndexRequest("posts", "doc", "4") //Adds an IndexRequest using the SMILE format
34 | .source(XContentType.JSON,"field", "baz"));
35 | ```
36 |
37 | ### 可选参数
38 |
39 | 提供下列可选参数:
40 |
41 | ```
42 | request.timeout(TimeValue.timeValueMinutes(2));
43 | request.timeout("2m");
44 | Timeout to wait for the bulk request to be performed as a TimeValue
45 | Timeout to wait for the bulk request to be performed as a String
46 | request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
47 | request.setRefreshPolicy("wait_for");
48 | Refresh policy as a WriteRequest.RefreshPolicy instance
49 | Refresh policy as a String
50 | request.waitForActiveShards(2);
51 | request.waitForActiveShards(ActiveShardCount.ALL);
52 | Sets the number of shard copies that must be active before proceeding with the index/update/delete operations.
53 | Number of shard copies provided as a ActiveShardCount: can be ActiveShardCount.ALL, ActiveShardCount.ONE or ActiveShardCount.DEFAULT (default)
54 | ```
55 |
56 | ### 同步执行
57 |
58 | ```
59 | BulkResponse bulkResponse = client.bulk(request);
60 | ```
61 |
62 | ### 异步执行
63 |
64 | ```
65 | client.bulkAsync(request, new ActionListener() {
66 | @Override
67 | public void onResponse(BulkResponse bulkResponse) {
68 | //Called when the execution is successfully completed. The response is provided as an argument and contains a list of individual results for each operation that was executed. Note that one or more operations might have failed while the others have been successfully executed.
69 | }
70 | @Override
71 | public void onFailure(Exception e) {
72 | //Called when the whole BulkRequest fails. In this case the raised exception is provided as an argument and no operation has been executed.
73 | }
74 | });
75 | ```
76 |
77 | ## Bulk 响应
78 |
79 | 返回的BulkResponse包含有关执行操作的信息,并允许对每个结果进行迭代,如下所示:
80 |
81 | ```
82 | for (BulkItemResponse bulkItemResponse : bulkResponse) { //迭代所有操作的结果
83 | DocWriteResponse itemResponse = bulkItemResponse.getResponse(); //Retrieve the response of the operation (successful or not), can be IndexResponse, UpdateResponse or DeleteResponse which can all be seen as DocWriteResponse instances
84 | if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
85 | || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
86 | // Handle the response of an index operation
87 | IndexResponse indexResponse = (IndexResponse) itemResponse;
88 | } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
89 | //Handle the response of a update operation
90 | UpdateResponse updateResponse = (UpdateResponse) itemResponse;
91 | } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
92 | // Handle the response of a delete operation
93 | DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
94 | }
95 | }
96 | ```
97 | 批量响应提供了一种快速检查一个或多个操作是否失败的方法:
98 |
99 | ```
100 | if (bulkResponse.hasFailures()) { // 只要有一个操作失败了,这个方法就返回 true
101 | }
102 | ```
103 |
104 | 在这种情况下,有必要迭代所有运算结果,以检查操作是否失败,如果是,则检索相应的故障:
105 |
106 | ```
107 | for (BulkItemResponse bulkItemResponse : bulkResponse) {
108 | if (bulkItemResponse.isFailed()) { //Indicate if a given operation failed
109 | BulkItemResponse.Failure failure = bulkItemResponse.getFailure(); //Retrieve the failure of the failed operation
110 | }
111 | }
112 | ```
113 |
114 | ## Bulk 处理器
115 |
116 | BulkProcessor通过提供允许索引/更新/删除操作在添加到处理器时透明执行的实用程序类来简化Bulk API的使用。
117 |
118 | 为了执行请求,BulkProcessor需要3个组件:
119 |
120 |
121 | - RestHighLevelClient
122 |
123 | 这个客户端用来执行 BulkRequest 并接收 BulkResponse 。
124 |
125 | - BulkProcessor.Listener
126 |
127 | 这个监听器会在每个 BulkRequest 执行之前和之后被调用,或者当 BulkRequest 失败时调用。
128 |
129 | - ThreadPool
130 |
131 | BulkRequest执行是使用这个池的线程完成的,允许BulkProcessor以非阻塞的方式工作,并允许在批量请求执行的同时接受新的索引/更新/删除请求。
132 |
133 | 然后 BulkProcessor.Builder 类可以被用来构建新的 BulkProcessor :
134 | ```
135 | ThreadPool threadPool = new ThreadPool(settings); // 使用已有的 Settings 对象创建 ThreadPool。
136 | BulkProcessor.Listener listener = new BulkProcessor.Listener() { // 创建 BulkProcessor.Listener
137 | @Override
138 | public void beforeBulk(long executionId, BulkRequest request) {
139 | //This method is called before each execution of a BulkRequest
140 | }
141 | @Override
142 | public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
143 | // This method is called after each execution of a BulkRequest
144 | }
145 | @Override
146 | public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
147 | //This method is called when a BulkRequest failed
148 | }
149 | };
150 | BulkProcessor bulkProcessor = new BulkProcessor.Builder(client::bulkAsync, listener, threadPool)
151 | .build(); // 通过调用 BulkProcessor.Builder 的 build() 方法创建 BulkProcessor。 RestHighLevelClient.bulkAsync() 将被用来执行 BulkRequest。
152 | ```
153 |
154 | BulkProcessor.Builder 提供了方法来配置 BulkProcessor 应该如何处理请求的执行:
155 |
156 | ```
157 | BulkProcessor.Builder builder = new BulkProcessor.Builder(client::bulkAsync, listener, threadPool);
158 | builder.setBulkActions(500); //Set when to flush a new bulk request based on the number of actions currently added (defaults to 1000, use -1 to disable it)
159 | builder.setBulkSize(new ByteSizeValue(1L, ByteSizeUnit.MB)); // Set when to flush a new bulk request based on the size of actions currently added (defaults to 5Mb, use -1 to disable it)
160 | builder.setConcurrentRequests(0); //Set the number of concurrent requests allowed to be executed (default to 1, use 0 to only allow the execution of a single request)
161 | builder.setFlushInterval(TimeValue.timeValueSeconds(10L)); // Set a flush interval flushing any BulkRequest pending if the interval passes (defaults to not set)
162 | builder.setBackoffPolicy(BackoffPolicy.constantBackoff(TimeValue.timeValueSeconds(1L), 3)); //Set a constant back off policy that initially waits for 1 second and retries up to 3 times. See BackoffPolicy.noBackoff(), BackoffPolicy.constantBackoff() and BackoffPolicy.exponentialBackoff() for more options.
163 | ```
164 | 一旦创建了BulkProcessor,可以向其添加请求:
165 |
166 | ```
167 | IndexRequest one = new IndexRequest("posts", "doc", "1").
168 | source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?");
169 | IndexRequest two = new IndexRequest("posts", "doc", "2")
170 | .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch");
171 | IndexRequest three = new IndexRequest("posts", "doc", "3")
172 | .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch");
173 | bulkProcessor.add(one);
174 | bulkProcessor.add(two);
175 | bulkProcessor.add(three);
176 | ```
177 |
178 | 这些请求将由 BulkProcessor 执行,它负责为每个批量请求调用 BulkProcessor.Listener 。
179 | 监听器提供方法接收 BulkResponse 和 BulkResponse :
180 |
181 | ```
182 | BulkProcessor.Listener listener = new BulkProcessor.Listener() {
183 | @Override
184 | public void beforeBulk(long executionId, BulkRequest request) {
185 | int numberOfActions = request.numberOfActions(); //Called before each execution of a BulkRequest, this method allows to know the number of operations that are going to be executed within the BulkRequest
186 | logger.debug("Executing bulk [{}] with {} requests", executionId, numberOfActions);
187 | }
188 | @Override
189 | public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
190 | if (response.hasFailures()) {
191 | //在每个 BulkRequest 执行之后调用,此方法允许获知 BulkResponse 是否包含错误
192 | logger.warn("Bulk [{}] executed with failures", executionId);
193 | } else {
194 | logger.debug("Bulk [{}] completed in {} milliseconds", executionId, response.getTook().getMillis());
195 | }
196 | }
197 | @Override
198 | public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
199 | logger.error("Failed to execute bulk", failure); //如果 BulkRequest 执行失败则调用,此方法可获知失败情况。
200 | }
201 | };
202 | ```
203 | 一旦将所有请求都添加到BulkProcessor,其实例需要使用两种可用的关闭方法之一关闭。
204 |
205 | 一旦所有请求都被添加到了 BulkProcessor, 它的实例就需要使用两种可用的关闭方法之一进行关闭。
206 |
207 | awaitClose() 可以被用来等待到所有请求都被处理,或者到指定的等待时间:
208 |
209 | ```
210 | boolean terminated = bulkProcessor.awaitClose(30L, TimeUnit.SECONDS);
211 | ```
212 |
213 | 如果所有批量请求完成,则该方法返回 true ,如果在完成所有批量请求之前等待时间过长,则返回 false 。
214 |
215 | close() 方法可以被用来立即关闭 BulkProcessor :
216 |
217 | ```
218 | bulkProcessor.close();
219 | ```
220 |
221 | 在关闭处理器之前,两个方法都会刷新已经被天教导处理器的请求,并禁止添加任何新的请求。
222 |
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis/clear-scroll-api.md:
--------------------------------------------------------------------------------
1 |
2 | # Clear Scroll API
3 |
4 | 使用 Search Scroll API 的搜索上下文在超过滚动时间时,会自动清除。但是,建议当不再需要使用 Clear Scroll API 后尽可能快的释放搜索上下文。
5 |
6 | ## Clear Scroll 请求
7 |
8 | ClearScrollRequest 可以像如下方式创建:
9 |
10 | ```
11 | ClearScrollRequest request = new ClearScrollRequest(); // 创建一个新的 ClearScrollRequest
12 | request.addScrollId(scrollId); // 添加一个滚动id到要清除的滚动标志列表里
13 | https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-clear-scroll.html
14 | ```
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis/delete-api.md:
--------------------------------------------------------------------------------
1 |
2 | # Delete API
3 |
4 | ## 删除请求对象
5 |
6 | DeleteRequest 需要下列参数:
7 |
8 | ```
9 | DeleteRequest request = new DeleteRequest(
10 | "posts", // index
11 | "doc", //Type
12 | "1"); // Document id
13 |
14 | ```
15 | 可选参数
16 |
17 | ## 提供下列可选参数:
18 |
19 | ```
20 | request.routing("routing"); // 路由值
21 | request.parent("parent"); //Parent 值
22 | request.timeout(TimeValue.timeValueMinutes(2)); // TimeValue 类型的等待主分片可用的超时时间
23 | request.timeout("2m"); // 字符串类型的等待主分片可用的超时时间
24 | request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); // Refresh policy as a WriteRequest.RefreshPolicy instance
25 | request.setRefreshPolicy("wait_for"); // Refresh policy as a String
26 | request.version(2); // Version
27 | request.versionType(VersionType.EXTERNAL); // Version type
28 | ```
29 |
30 | ## 同步执行
31 |
32 | ```
33 | DeleteResponse deleteResponse = client.delete(request);
34 | ```
35 |
36 | ## 异步执行
37 |
38 | ```
39 | client.deleteAsync(request, new ActionListener() {
40 | @Override
41 | public void onResponse(DeleteResponse deleteResponse) {
42 | //Called when the execution is successfully completed. The response is provided as an argument
43 | }
44 | @Override
45 | public void onFailure(Exception e) {
46 | //Called in case of failure. The raised exception is provided as an argument
47 | }
48 | });
49 | ```
50 |
51 | ## 删除操作的响应
52 |
53 | 返回的 DeleteResponse 对象允许通过执行以下操作获取相关信息:
54 |
55 | ```
56 | String index = deleteResponse.getIndex();
57 | String type = deleteResponse.getType();
58 | String id = deleteResponse.getId();
59 | long version = deleteResponse.getVersion();
60 | ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
61 | if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
62 | // Handle the situation where number of successful shards is less than total shards
63 | }
64 | if (shardInfo.getFailed() > 0) {
65 | for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
66 | String reason = failure.reason(); // Handle the potential failures
67 | }
68 | }
69 | ```
70 | 也可以检查文档是否被发现:
71 |
72 | ```
73 | DeleteRequest request = new DeleteRequest("posts", "doc", "does_not_exist");
74 | DeleteResponse deleteResponse = client.delete(request);
75 | if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
76 | //如果被删除的文档没有被找到,做某些操作
77 | }
78 | ```
79 |
80 | 如果有版本冲突,将抛出 ElasticsearchException 异常信息:
81 |
82 | ```
83 | try {
84 | DeleteRequest request = new DeleteRequest("posts", "doc", "1").version(2);
85 | DeleteResponse deleteResponse = client.delete(request);
86 | } catch (ElasticsearchException exception) {
87 | if (exception.status() == RestStatus.CONFLICT) {
88 | //由于版本冲突错误导致的异常
89 | }
90 | }
91 | ```
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis/get-api.md:
--------------------------------------------------------------------------------
1 |
2 | # Get API
3 |
4 | ## 获取请求对象
5 |
6 | GetRequest 需要以下参数:
7 |
8 | ```
9 | GetRequest getRequest = new GetRequest(
10 | "posts", // 索引
11 | "doc", // 类别
12 | "1"); // 文档id
13 | ```
14 |
15 | ### 可选参数
16 |
17 | 提供以下可选参数:
18 |
19 | ```
20 | request.fetchSourceContext(new FetchSourceContext(false)); //禁用检索源,默认为启用
21 |
22 | ```
23 | String[] includes = new String[]{"message", "*Date"};
24 | String[] excludes = Strings.EMPTY_ARRAY;
25 | FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
26 | request.fetchSourceContext(fetchSourceContext); //设置源包含的特定域
27 |
28 | ```
29 |
30 |
31 |
32 | ```
33 | String[] includes = Strings.EMPTY_ARRAY;
34 | String[] excludes = new String[]{"message"};
35 | FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
36 | request.fetchSourceContext(fetchSourceContext);
37 | Configure source exclusion for specific fields
38 | ```
39 |
40 | ```
41 | request.storedFields("message"); //Configure retrieval for specific stored fields (requires fields to be stored separately in the mappings)
42 | GetResponse getResponse = client.get(request);
43 | String message = (String) getResponse.getField("message").getValue(); //Retrieve the message stored field (requires the field to be stored separately in the mappings)
44 | ```
45 | ```
46 | request.routing("routing"); //Routing value
47 | request.parent("parent"); //Parent value
48 | request.preference("preference"); //Preference value
49 | request.realtime(false); //Set realtime flag to false (true by default)
50 | request.refresh(true); //Perform a refresh before retrieving the document (false by default)
51 | request.version(2); //Version
52 | request.versionType(VersionType.EXTERNAL); //Version type
53 | ```
54 |
55 | ### 同步执行
56 |
57 | ```
58 | GetResponse getResponse = client.get(getRequest);
59 | ```
60 |
61 | ### 异步执行
62 |
63 | ```
64 | client.getAsync(request, new ActionListener() {
65 | @Override
66 | public void onResponse(GetResponse getResponse) {
67 | //Called when the execution is successfully completed. The response is provided as an argument.
68 | }
69 | @Override
70 | public void onFailure(Exception e) {
71 | //Called in case of failure. The raised exception is provided as an argument.
72 | }
73 | });
74 | ```
75 |
76 | ## 获取响应
77 |
78 | The returned GetResponse allows to retrieve the requested document along with its metadata and eventually stored fields.
79 |
80 | ```
81 | String index = getResponse.getIndex();
82 | String type = getResponse.getType();
83 | String id = getResponse.getId();
84 | if (getResponse.isExists()) {
85 | long version = getResponse.getVersion();
86 | String sourceAsString = getResponse.getSourceAsString(); //Retrieve the document as a String
87 | Map sourceAsMap = getResponse.getSourceAsMap(); //Retrieve the document as a Map
88 | byte[] sourceAsBytes = getResponse.getSourceAsBytes(); //Retrieve the document as a byte[]
89 | } else {
90 | //Handle the scenario where the document was not found. Note that although the returned response has 404 status code, a valid GetResponse is returned rather than an exception thrown. Such response does not hold any source document and its isExists method returns false.
91 | }
92 | ```
93 |
94 | 当针对不存在的索引执行获取请求时,响应有404状态码,抛出一个 ElasticsearchException 异常,需要如下处理:
95 |
96 | ```
97 | GetRequest request = new GetRequest("does_not_exist", "doc", "1");
98 | try {
99 | GetResponse getResponse = client.get(request);
100 | } catch (ElasticsearchException e) {
101 | if (e.status() == RestStatus.NOT_FOUND) {
102 | // 处理因为索引不存在而抛出的异常,
103 | }
104 | }
105 | ```
106 |
107 | 如果请求了特定文档版本,但现有文档具有不同的版本号,则会引发版本冲突:
108 |
109 | ```
110 | try {
111 | GetRequest request = new GetRequest("posts", "doc", "1").version(2);
112 | GetResponse getResponse = client.get(request);
113 | } catch (ElasticsearchException exception) {
114 | if (exception.status() == RestStatus.CONFLICT) {
115 | //表示返回了版本冲突错误引发异常
116 | }
117 | }
118 | ```
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis/index-api.md:
--------------------------------------------------------------------------------
1 | # Index API
2 |
3 | ## Index 请求
4 |
5 | IndexRequest 要求下列参数:
6 |
7 | ```
8 | IndexRequest request = new IndexRequest(
9 | "posts", //Index
10 | "doc", //Type
11 | "1"); //Document id
12 | String jsonString = "{" +
13 | "\"user\":\"kimchy\"," +
14 | "\"postDate\":\"2013-01-30\"," +
15 | "\"message\":\"trying out Elasticsearch\"" +
16 | "}";
17 | request.source(jsonString, XContentType.JSON); /以字符串提供的 Document source
18 | ```
19 |
20 | ### 文档来源
21 |
22 | 文件来源可以以不同的方式提供:
23 |
24 | ```
25 | Map jsonMap = new HashMap<>();
26 | jsonMap.put("user", "kimchy");
27 | jsonMap.put("postDate", new Date());
28 | jsonMap.put("message", "trying out Elasticsearch");
29 | IndexRequest indexRequest = new IndexRequest("posts", "doc", "1").source(jsonMap); //Map 作为文档源,它可以自动转换为 JSON 格式。
30 | ```
31 |
32 | ```
33 | XContentBuilder builder = XContentFactory.jsonBuilder();
34 | builder.startObject();
35 | {
36 | builder.field("user", "kimchy");
37 | builder.field("postDate", new Date());
38 | builder.field("message", "trying out Elasticsearch");
39 | }
40 | builder.endObject();
41 | IndexRequest indexRequest = new IndexRequest("posts", "doc", "1").source(builder); //XContentBuilder 对象作为文档源,由 Elasticsearch 内置的帮助器生成 JSON 内容
42 |
43 | ```
44 |
45 | ```
46 | IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
47 | .source("user", "kimchy",
48 | "postDate", new Date(),
49 | "message", "trying out Elasticsearch"); //以键值对对象作为文档来源,它自动转换为 JSON 格式
50 | ```
51 |
52 | ### 可选参数
53 |
54 | 下列参数可选:
55 |
56 | ```
57 | request.routing("routing"); //Routing 值
58 | ```
59 |
60 | ```
61 | request.parent("parent"); //Parent 值
62 | ```
63 |
64 | ```
65 | request.timeout(TimeValue.timeValueSeconds(1)); //`TimeValue`类型的等待主分片可用的超时时间
66 | request.timeout("1s"); //`String` 类型的等待主分片可用的超时时间
67 | ```
68 |
69 | ```
70 | request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); //以 WriteRequest.RefreshPolicy 实例的刷新策略参数
71 | request.setRefreshPolicy("wait_for"); // 字符串刷新策略参数
72 | ```
73 |
74 | ```
75 | request.version(2); //版本
76 | ```
77 |
78 | ```
79 | request.versionType(VersionType.EXTERNAL); //版本类型
80 | ```
81 |
82 | ```
83 | request.opType(DocWriteRequest.OpType.CREATE); //提供一个 DocWriteRequest.OpType 值作为操作类型
84 | request.opType("create"); //字符串类型的操作类型参数: 可以是 create 或 update (默认值)
85 | ```
86 |
87 | ```
88 | request.setPipeline("pipeline"); //在索引文档之前要执行的摄取管道的名称
89 | ```
90 | ### 同步执行
91 |
92 | ```
93 | IndexResponse indexResponse = client.index(request);
94 | ```
95 |
96 | ### 异步执行
97 |
98 | ```
99 | client.indexAsync(request, new ActionListener() {
100 | @Override
101 | public void onResponse(IndexResponse indexResponse) {
102 | //当操作成功完成的时候被调用。响应对象以参数的形式传入。
103 | }
104 | @Override
105 | public void onFailure(Exception e) {
106 | //故障时被调用。异常对象以参数的形式传入
107 | }
108 | });
109 | ```
110 | ## Index 响应
111 |
112 | 返回的“IndexResponse”可以检索有关执行操作的信息,如下所示:
113 |
114 | ```
115 | String index = indexResponse.getIndex();
116 | String type = indexResponse.getType();
117 | String id = indexResponse.getId();
118 | long version = indexResponse.getVersion();
119 | if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
120 | //处理(如果需要)首次创建文档的情况
121 | } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
122 | //处理(如果需要)文档已经存在时被覆盖的情况
123 | }
124 | ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
125 | if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
126 | //处理成功碎片数少于总碎片的情况
127 | }
128 | if (shardInfo.getFailed() > 0) {
129 | for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
130 | String reason = failure.reason();//处理潜在的故障
131 | }
132 | }
133 | ```
134 |
135 | 如果存在版本冲突,将抛出 ElasticsearchException :
136 |
137 | ```
138 | IndexRequest request = new IndexRequest("posts", "doc", "1")
139 | .source("field", "value")
140 | .version(1);
141 | try {
142 | IndexResponse response = client.index(request);
143 | } catch(ElasticsearchException e) {
144 | if (e.status() == RestStatus.CONFLICT) {
145 | //表示是由于返回了版本冲突错误引发的异常
146 | }
147 | }
148 | ```
149 |
150 | 发生同样的情况发生在opType设置为create但是已经存在具有相同索引,类型和id的文档时:
151 |
152 | ```
153 | IndexRequest request = new IndexRequest("posts", "doc", "1")
154 | .source("field", "value")
155 | .opType(DocWriteRequest.OpType.CREATE);
156 | try {
157 | IndexResponse response = client.index(request);
158 | } catch(ElasticsearchException e) {
159 | if (e.status() == RestStatus.CONFLICT) {
160 | //表示由于返回了版本冲突错误引发的异常
161 | }
162 | }
163 | ```
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis/info-api.md:
--------------------------------------------------------------------------------
1 |
2 | # Info API
3 |
4 | ## 执行
5 |
6 | 集群信息可以通过 info() 方法被获取到:
7 |
8 | ```
9 | MainResponse response = client.info();
10 | ```
11 | ## 响应
12 |
13 | 返回的 MainResponse 提供了有关集群的各种信息:
14 |
15 | ```
16 | ClusterName clusterName = response.getClusterName(); // 获取包含集群名称信息的 ClusterName 对象
17 | String clusterUuid = response.getClusterUuid(); // 获取集群的唯一标识符
18 | String nodeName = response.getNodeName(); // 获取执行请求的节点的名称
19 | Version version = response.getVersion(); // 获取已执行请求的节点版本
20 | Build build = response.getBuild(); // 获取已执行请求的节点的构建信息
21 | ```
--------------------------------------------------------------------------------
/java-high-level-rest-client/supported-apis/search-api.md:
--------------------------------------------------------------------------------
1 |
2 | # Search API
3 |
4 | ## 搜索请求
5 |
6 | SearchRequest用于与搜索文档,聚合,建议有关的任何操作,并且还提供了在生成的文档上请求突出显示的方法。
7 | 在最基本的形式中,我们可以向请求添加一个查询:
8 |
9 | ```
10 | SearchRequest searchRequest = new SearchRequest(); //穿件SeachRequest,Without arguments this runs against all indices.
11 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// 大多数的搜索参数被添加到 SearchSourceBuilder 。它为每个进入请求体的每个东西都提供 setter 方法。
12 | searchSourceBuilder.query(QueryBuilders.matchAllQuery()); // 添加一个 match_all 查询到 searchSourceBuilder 。
13 |
14 | ```
15 |
16 | ### 可选参数
17 |
18 | 我们先看一下SearchRequest的一些可选参数:
19 |
20 | ```
21 | SearchRequest searchRequest = new SearchRequest("posts"); // 限制请求到某个索引上
22 | searchRequest.types("doc"); // 限制请求的类别
23 | ```
24 |
25 | 还有一些其他有趣的可选参数:
26 |
27 | ```
28 | searchRequest.routing("routing"); // 设置路由参数、
29 | searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); // 设置IndicesOptions控制如何解析不可用索引以及扩展通配符表达式
30 | searchRequest.preference("_local"); //使用首选参数,例如,执行搜索优先选择本地分片。 默认值是随机化分片。
31 | ```
32 |
33 | ### 使用 SearchSourceBuilder
34 |
35 | 可以在SearchSourceBuilder上设置大多数控制搜索行为的选项,其中包含或多或少相当于 Rest API 的搜索请求正文中的选项。
36 |
37 | 以下是一些常见选项的示例:
38 |
39 | ```
40 | SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //使用默认选项创建 SearchSourceBuilder 。
41 | sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy")); //设置查询对象。可以使任何类型的 QueryBuilder
42 | sourceBuilder.from(0); //设置from选项,确定要开始搜索的结果索引。 默认为0。
43 | sourceBuilder.size(5); //设置大小选项,确定要返回的搜索匹配数。 默认为10。
44 | sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //设置一个可选的超时时间,用于控制搜索允许的时间。
45 | ```
46 |
47 | 构建查询
48 |
49 | 搜索查询可以使用 QueryBuilder 对象创建。 对于Elasticsearch的 Query DSL 支持的每个搜索查询类型,都存在QueryBuilder。
50 | QueryBuilder 可以使用它的构造器创建:
51 |
52 | ```
53 | //创建一个字段“user”与文本“kimchy”相匹配的的全文匹配查询。
54 | MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "kimchy");
55 | ```
56 |
57 | 创建后,QueryBuilder对象提供了配置搜索查询选项的方法:
58 |
59 | ```
60 | matchQueryBuilder.fuzziness(Fuzziness.AUTO); //在匹配查询上启用模糊匹配
61 | matchQueryBuilder.prefixLength(3); //在匹配查询上设置前缀长度
62 | matchQueryBuilder.maxExpansions(10); //设置最大扩展选项以控制查询的模糊过程
63 |
64 | ```
65 |
66 | QueryBuilder 对象也可以使用 QueryBuilders 工具类创建。这个类提供了使用链式编程风格的辅助方法来创建 QueryBuilder 对象:
67 |
68 | ```
69 | QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
70 | .fuzziness(Fuzziness.AUTO)
71 | .prefixLength(3)
72 | .maxExpansions(10);
73 |
74 | ```
75 |
76 | 无论用于创建它的方法如何, QueryBuilder 对象必须添加到 SearchSourceBuilder 中,如下所示:
77 |
78 | ```
79 | searchSourceBuilder.query(matchQueryBuilder);
80 | ```
81 |
82 | “构建查询”页面列出了所有可用的搜索查询及其对应的QueryBuilder对象和QueryBuilders辅助方法。
83 |
84 | ### 指定排序
85 |
86 | SearchSourceBuilder允许添加一个或多个SortBuilder实例。 有四个特殊的实现(Field-,Score-,GeoDistance-和ScriptSortBuilder)。
87 |
88 | ```
89 | sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); // 按_score降序排序(默认值)
90 | sourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.ASC)); //也按_id字段排序升序
91 | ```
92 |
93 | 源过滤
94 |
95 |
96 | 默认情况下,搜索请求返回文档的内容,_source但像 Rest API 中的内容一样,您可以覆盖此行为。例如,您可以完全关闭 _source 检索:
97 |
98 | ```
99 | sourceBuilder.fetchSource(false);
100 | ```
101 |
102 | 该方法还接受一个或多个通配符模式的数组,以便以更精细的方式控制哪些字段包含或排除:
103 |
104 | ```
105 | String[] includeFields = new String[] {"title", "user", "innerObject.*"};
106 | String[] excludeFields = new String[] {"_type"};
107 | sourceBuilder.fetchSource(includeFields, excludeFields);
108 | ```
109 |
110 | ### 请求高亮
111 |
112 | 突出显示搜索结果可以通过设置来实现HighlightBuilder的 SearchSourceBuilder。可以通过向HighlightBuilder.Fielda 添加一个或多个实例来为每个字段定义不同的突出显示行为HighlightBuilder。
113 |
114 | ```
115 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
116 | HighlightBuilder highlightBuilder = new HighlightBuilder();//Creates a new HighlightBuilder
117 | HighlightBuilder.Field highlightTitle =
118 | new HighlightBuilder.Field("title");//Create a field highlighter for the title field
119 | highlightTitle.highlighterType("unified"); //Set the field highlighter type
120 | highlightBuilder.field(highlightTitle); //Add the field highlighter to the highlight builder
121 | HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
122 | highlightBuilder.field(highlightUser);
123 | searchSourceBuilder.highlighter(highlightBuilder);
124 | ```
125 |
126 | 有很多选项,这在Rest API文档中有详细的介绍。Rest API参数(例如,pre_tags)通常由具有相似名称的 setter(例如#preTags(String …))更改。
127 |
128 | 随后可以从 SearchResponse 中检索突出显示的文本片段。
129 |
130 | ### 请求聚合
131 |
132 | 可以通过首先创建适当的集合AggregationBuilder然后在其上设置集合来将搜索添加到搜索结果中 SearchSourceBuilder。在下面的示例中,我们terms在公司名称上创建一个聚合,其中包含公司员工平均年龄的子聚合:
133 |
134 | ```
135 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
136 | TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")
137 | .field("company.keyword");
138 | aggregation.subAggregation(AggregationBuilders.avg("average_age")
139 | .field("age"));
140 | searchSourceBuilder.aggregation(aggregation);
141 | ```
142 |
143 | “ 构建聚合”页面提供了所有可用聚合以及其相应AggregationBuilder对象和AggregationBuilders帮助方法的列表。
144 |
145 | 后面我们将看到如何访问聚合的 SearchResponse。
146 |
147 | ### 请求建议
148 |
149 | 要向搜索请求添加建议,请使用SuggestionBuilder从SuggestBuilders工厂类轻松访问的其中一个实现。建议构建者需要添加到顶层SuggestBuilder,本身可以设置在 顶层SearchSourceBuilder。
150 |
151 | ```
152 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
153 | SuggestionBuilder termSuggestionBuilder =
154 | SuggestBuilders.termSuggestion("user").text("kmichy"); //
155 | Creates a new TermSuggestionBuilder for the user field and the text kmichy
156 | SuggestBuilder suggestBuilder = new SuggestBuilder();
157 | suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder); //Adds the suggestion builder and names it suggest_user
158 | searchSourceBuilder.suggest(suggestBuilder);
159 | ```
160 |
161 | 后面我们将看到如何从 SearchResponse 获取建议。
162 |
163 | ### 自定义查询和聚合
164 |
165 | 该配置文件API可用于简档查询和聚集的执行针对特定搜索请求。为了使用它,配置文件标志必须设置为true SearchSourceBuilder:
166 |
167 | ```
168 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
169 | searchSourceBuilder.profile(true);
170 | ```
171 |
172 | 一旦SearchRequest执行,相应的SearchResponse将 包含分析结果。
173 |
174 | ### 同步执行
175 |
176 | SearchRequest以下列方式执行时,客户端SearchResponse在继续执行代码之前等待返回:
177 |
178 | ```
179 | SearchResponse searchResponse = client.search(searchRequest);
180 | ```
181 |
182 | ### 异步执行
183 |
184 | ```
185 | client.searchAsync(searchRequest, new ActionListener() {
186 | @Override
187 | public void onResponse(SearchResponse searchResponse) {
188 | //执行成功完成时调用。
189 | }
190 | @Override
191 | public void onFailure(Exception e) {
192 | //当整个SearchRequest失败时调用。
193 | }
194 | });
195 | ```
196 | ## 搜索响应
197 |
198 | 通过执行搜索返回的 SearchResponse 提供了关于搜索执行本身以及对返回的文档的访问的详细信息。 首先,有关于请求执行本身的有用信息,如HTTP状态代码,执行时间或请求提前终止或超时:
199 |
200 | ```
201 | RestStatus status = searchResponse.status();
202 | TimeValue took = searchResponse.getTook();
203 | Boolean terminatedEarly = searchResponse.isTerminatedEarly();
204 | boolean timedOut = searchResponse.isTimedOut();
205 | ```
206 |
207 | 其次,响应还通过提供关于搜索影响的分片总数以及成功与不成功的分片的统计信息,提供关于分片级别执行的信息。 可能的故障也可以通过遍历ShardSearchFailures上的数组进行迭代来处理,如下例所示:
208 |
209 | ```
210 | int totalShards = searchResponse.getTotalShards();
211 | int successfulShards = searchResponse.getSuccessfulShards();
212 | int failedShards = searchResponse.getFailedShards();
213 | for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
214 | // 故障应该在这里被处理
215 | }
216 | ```
217 |
218 | ### 检索 SearchHits
219 |
220 | 要访问返回的文档,我们需要首先得到响应中包含的 SearchHits :
221 |
222 | ```
223 | SearchHits hits = searchResponse.getHits();
224 | ```
225 |
226 | 将SearchHits提供命中的所有全局信息,比如命中总数或最大分数:
227 |
228 | ```
229 | long totalHits = hits.getTotalHits();
230 | float maxScore = hits.getMaxScore();
231 | ```
232 |
233 | 嵌套在 SearchHits 的各个搜索结果可以被迭代访问:
234 |
235 | ```
236 | SearchHit[] searchHits = hits.getHits();
237 | for (SearchHit hit : searchHits) {
238 | // do something with the SearchHit
239 | }
240 | ```
241 |
242 | SearchHit可以访问基本信息,如索引,类型,文档ID 以及每个搜索匹配的分数:
243 |
244 | ```
245 | String index = hit.getIndex();
246 | String type = hit.getType();
247 | String id = hit.getId();
248 | float score = hit.getScore();
249 | ```
250 |
251 | 此外,它可以让您将文档源作为简单的JSON-String或键/值对的映射。 在该映射中,字段名为键,含字段值为值。 多值字段作为对象的列表返回,嵌套对象作为另一个键/值映射。 这些情况需要相应地执行:
252 |
253 | ```
254 | String sourceAsString = hit.getSourceAsString();
255 | Map sourceAsMap = hit.getSourceAsMap();
256 | String documentTitle = (String) sourceAsMap.get("title");
257 | List