1
1
package org .elasticsearch .plugin .nlpcn .client ;
2
2
3
3
import co .elastic .clients .elasticsearch .ElasticsearchClient ;
4
- import co .elastic .clients .json .JsonpMapper ;
5
- import co .elastic .clients .json .JsonpSerializable ;
4
+ import com .google .common .collect .Maps ;
6
5
import org .elasticsearch .action .ActionListener ;
7
6
import org .elasticsearch .action .ActionRequest ;
8
7
import org .elasticsearch .action .ActionResponse ;
9
8
import org .elasticsearch .action .ActionType ;
10
- import org .elasticsearch .action .admin .cluster .node .info .NodesInfoAction ;
11
- import org .elasticsearch .action .admin .cluster .node .info .NodesInfoRequest ;
12
- import org .elasticsearch .action .admin .cluster .settings .ClusterUpdateSettingsAction ;
13
- import org .elasticsearch .action .admin .cluster .settings .ClusterUpdateSettingsRequest ;
14
- import org .elasticsearch .action .admin .cluster .settings .ClusterUpdateSettingsResponse ;
15
- import org .elasticsearch .action .admin .cluster .state .ClusterStateAction ;
16
- import org .elasticsearch .action .admin .cluster .state .ClusterStateRequest ;
17
- import org .elasticsearch .action .admin .indices .create .CreateIndexAction ;
18
- import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
19
- import org .elasticsearch .action .admin .indices .create .CreateIndexResponse ;
20
- import org .elasticsearch .action .admin .indices .delete .DeleteIndexAction ;
21
- import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
22
- import org .elasticsearch .action .admin .indices .get .GetIndexAction ;
23
- import org .elasticsearch .action .admin .indices .get .GetIndexRequest ;
24
- import org .elasticsearch .action .admin .indices .mapping .put .PutMappingAction ;
25
- import org .elasticsearch .action .admin .indices .mapping .put .PutMappingRequest ;
26
- import org .elasticsearch .action .admin .indices .refresh .RefreshAction ;
27
- import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
28
- import org .elasticsearch .action .admin .indices .refresh .RefreshResponse ;
29
- import org .elasticsearch .action .bulk .BulkAction ;
30
- import org .elasticsearch .action .bulk .BulkRequest ;
31
- import org .elasticsearch .action .bulk .BulkResponse ;
32
- import org .elasticsearch .action .search .MultiSearchAction ;
33
- import org .elasticsearch .action .search .MultiSearchRequest ;
34
- import org .elasticsearch .action .search .MultiSearchResponse ;
35
- import org .elasticsearch .action .search .SearchAction ;
36
- import org .elasticsearch .action .search .SearchRequest ;
37
- import org .elasticsearch .action .search .SearchResponse ;
38
- import org .elasticsearch .action .search .SearchScrollAction ;
39
- import org .elasticsearch .action .search .SearchScrollRequest ;
40
- import org .elasticsearch .action .support .master .AcknowledgedResponse ;
41
9
import org .elasticsearch .client .internal .support .AbstractClient ;
42
- import org .elasticsearch .core .CheckedFunction ;
43
- import org .elasticsearch .index .reindex .BulkByScrollResponse ;
44
- import org .elasticsearch .index .reindex .DeleteByQueryAction ;
45
- import org .elasticsearch .index .reindex .DeleteByQueryRequest ;
10
+ import org .elasticsearch .plugin .nlpcn .client .handler .ActionHandler ;
11
+ import org .elasticsearch .plugin .nlpcn .client .handler .BulkActionHandler ;
12
+ import org .elasticsearch .plugin .nlpcn .client .handler .ClusterStateActionHandler ;
13
+ import org .elasticsearch .plugin .nlpcn .client .handler .ClusterUpdateSettingsActionHandler ;
14
+ import org .elasticsearch .plugin .nlpcn .client .handler .CreateIndexActionHandler ;
15
+ import org .elasticsearch .plugin .nlpcn .client .handler .DeleteByQueryActionHandler ;
16
+ import org .elasticsearch .plugin .nlpcn .client .handler .DeleteIndexActionHandler ;
17
+ import org .elasticsearch .plugin .nlpcn .client .handler .GetIndexActionHandler ;
18
+ import org .elasticsearch .plugin .nlpcn .client .handler .MultiSearchActionHandler ;
19
+ import org .elasticsearch .plugin .nlpcn .client .handler .NodesInfoActionHandler ;
20
+ import org .elasticsearch .plugin .nlpcn .client .handler .PutMappingActionHandler ;
21
+ import org .elasticsearch .plugin .nlpcn .client .handler .RefreshActionHandler ;
22
+ import org .elasticsearch .plugin .nlpcn .client .handler .SearchActionHandler ;
23
+ import org .elasticsearch .plugin .nlpcn .client .handler .SearchScrollActionHandler ;
46
24
47
25
import java .io .IOException ;
48
26
import java .io .UncheckedIOException ;
27
+ import java .util .Map ;
49
28
import java .util .Objects ;
50
29
51
30
/**
55
34
* @version V1.0
56
35
* @since 2022-12-19 21:16
57
36
*/
58
- public class ElasticsearchRestClient extends AbstractClient {
37
+ public class ElasticsearchRestClient extends AbstractClient implements AutoCloseable {
59
38
60
39
private final ElasticsearchClient client ;
61
- private final RequestConverter requestConverter ;
62
- private final ResponseConverter responseConverter ;
40
+ private final Map <String , ActionHandler <ActionRequest , ?, ?, ActionResponse >> handlers = Maps .newHashMap ();
63
41
64
42
public ElasticsearchRestClient (ElasticsearchClient client ) {
65
43
super (null , null );
66
44
67
45
this .client = client ;
68
- JsonpMapper jsonpMapper = client ._jsonpMapper ();
69
- this .requestConverter = new RequestConverter (jsonpMapper );
70
- this .responseConverter = new ResponseConverter (jsonpMapper );
46
+ registerHandler (client );
71
47
}
72
48
73
49
@ SuppressWarnings ("unchecked" )
74
50
@ Override
75
51
protected <Request extends ActionRequest , Response extends ActionResponse > void doExecute (ActionType <Response > action , Request request , ActionListener <Response > listener ) {
76
52
try {
77
53
String name = action .name ();
78
- ActionResponse response ;
79
- switch (name ) {
80
- case ClusterUpdateSettingsAction .NAME :
81
- response = doExecute (client -> client .cluster ().putSettings (requestConverter .putClusterSettingsRequest ((ClusterUpdateSettingsRequest ) request )),
82
- r -> responseConverter .parseJson (r , ClusterUpdateSettingsResponse ::fromXContent ));
83
- break ;
84
- case ClusterStateAction .NAME :
85
- response = doExecute (client -> client .cluster ().state (requestConverter .stateRequest ((ClusterStateRequest ) request )),
86
- responseConverter ::clusterStateResponse );
87
- break ;
88
- case NodesInfoAction .NAME :
89
- response = doExecute (client -> client .nodes ().info (requestConverter .nodesInfoRequest ((NodesInfoRequest ) request )),
90
- responseConverter ::nodesInfoResponse );
91
- break ;
92
- case DeleteIndexAction .NAME :
93
- response = doExecute (client -> client .indices ().delete (requestConverter .deleteIndexRequest ((DeleteIndexRequest ) request )),
94
- r -> responseConverter .parseJson (r , AcknowledgedResponse ::fromXContent ));
95
- break ;
96
- case PutMappingAction .NAME :
97
- response = doExecute (client -> client .indices ().putMapping (requestConverter .putMappingRequest ((PutMappingRequest ) request )),
98
- r -> responseConverter .parseJson (r , AcknowledgedResponse ::fromXContent ));
99
- break ;
100
- case GetIndexAction .NAME :
101
- response = doExecute (client -> client .indices ().get (requestConverter .getIndexRequest ((GetIndexRequest ) request )),
102
- responseConverter ::getIndexResponse );
103
- break ;
104
- case CreateIndexAction .NAME :
105
- response = doExecute (client -> client .indices ().create (requestConverter .createIndexRequest ((CreateIndexRequest ) request )),
106
- r -> responseConverter .parseJson (r , CreateIndexResponse ::fromXContent ));
107
- break ;
108
- case RefreshAction .NAME :
109
- response = doExecute (client -> client .indices ().refresh (requestConverter .refreshRequest ((RefreshRequest ) request )),
110
- r -> responseConverter .parseJson (r , RefreshResponse ::fromXContent ));
111
- break ;
112
- case BulkAction .NAME :
113
- response = doExecute (client -> client .bulk (requestConverter .bulkRequest ((BulkRequest ) request )),
114
- r -> responseConverter .parseJson (r , BulkResponse ::fromXContent ));
115
- break ;
116
- case SearchAction .NAME :
117
- response = doExecute (client -> client .search (requestConverter .searchRequest ((SearchRequest ) request ), Object .class ),
118
- r -> responseConverter .parseJson (r , SearchResponse ::fromXContent ));
119
- break ;
120
- case SearchScrollAction .NAME :
121
- response = doExecute (client -> client .scroll (requestConverter .scrollRequest ((SearchScrollRequest ) request ), Object .class ),
122
- r -> responseConverter .parseJson (r , SearchResponse ::fromXContent ));
123
- break ;
124
- case MultiSearchAction .NAME :
125
- response = doExecute (client -> client .msearch (requestConverter .msearchRequest ((MultiSearchRequest ) request ), Object .class ),
126
- r -> responseConverter .parseJson (r , MultiSearchResponse ::fromXContext ));
127
- break ;
128
- case DeleteByQueryAction .NAME :
129
- response = doExecute (client -> client .deleteByQuery (requestConverter .deleteByQueryRequest ((DeleteByQueryRequest ) request )),
130
- r -> responseConverter .parseJson (r , BulkByScrollResponse ::fromXContent ));
131
- break ;
132
- default :
133
- listener .onFailure (new UnsupportedOperationException ("elasticsearch rest client doesn't support action[" + name + "]" ));
134
- return ;
54
+ ActionHandler <ActionRequest , ?, ?, ActionResponse > handler = handlers .get (name );
55
+ if (Objects .isNull (handler )) {
56
+ listener .onFailure (new UnsupportedOperationException ("elasticsearch rest client doesn't support action[" + name + "]" ));
57
+ return ;
135
58
}
136
59
60
+ ActionResponse response = handler .handle (request );
137
61
listener .onResponse ((Response ) response );
138
62
} catch (Exception e ) {
139
63
listener .onFailure (e );
@@ -149,11 +73,24 @@ public void close() {
149
73
}
150
74
}
151
75
152
- public <T extends JsonpSerializable , R extends ActionResponse > R doExecute (CheckedFunction <ElasticsearchClient , T , IOException > clientCallback , CheckedFunction <T , R , IOException > responseCallback ) throws IOException {
153
- Objects .requireNonNull (clientCallback , "clientCallback must not be null" );
154
- Objects .requireNonNull (responseCallback , "responseCallback must not be null" );
76
+ protected void registerHandler (ElasticsearchClient client ) {
77
+ doRegisterHandler (new BulkActionHandler (client ));
78
+ doRegisterHandler (new ClusterStateActionHandler (client ));
79
+ doRegisterHandler (new ClusterUpdateSettingsActionHandler (client ));
80
+ doRegisterHandler (new CreateIndexActionHandler (client ));
81
+ doRegisterHandler (new DeleteByQueryActionHandler (client ));
82
+ doRegisterHandler (new DeleteIndexActionHandler (client ));
83
+ doRegisterHandler (new GetIndexActionHandler (client ));
84
+ doRegisterHandler (new MultiSearchActionHandler (client ));
85
+ doRegisterHandler (new NodesInfoActionHandler (client ));
86
+ doRegisterHandler (new PutMappingActionHandler (client ));
87
+ doRegisterHandler (new RefreshActionHandler (client ));
88
+ doRegisterHandler (new SearchActionHandler (client ));
89
+ doRegisterHandler (new SearchScrollActionHandler (client ));
90
+ }
155
91
156
- T response = clientCallback .apply (client );
157
- return responseCallback .apply (response );
92
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
93
+ private void doRegisterHandler (ActionHandler actionHandler ) {
94
+ handlers .put (actionHandler .getName (), actionHandler );
158
95
}
159
96
}
0 commit comments