forked from meilisearch/meilisearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexesHandler.java
More file actions
153 lines (137 loc) · 5.34 KB
/
IndexesHandler.java
File metadata and controls
153 lines (137 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.meilisearch.sdk;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.http.URLBuilder;
import com.meilisearch.sdk.model.IndexesQuery;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.TaskInfo;
import java.util.HashMap;
/**
* Class covering the Meilisearch Index API.
*
* @see <a href="https://www.meilisearch.com/docs/reference/api/indexes">API specification</a>
*/
public class IndexesHandler {
private final HttpClient httpClient;
/**
* Creates and sets up an instance of IndexesHandler to simplify Meilisearch API calls to manage
* indexes
*
* @param config Meilisearch configuration
*/
protected IndexesHandler(Config config) {
this.httpClient = config.httpClient;
}
/**
* Creates an index with a unique identifier
*
* @param uid Unique identifier of the index
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if an error occurs
*/
TaskInfo createIndex(String uid) throws MeilisearchException {
return this.createIndex(uid, null);
}
/**
* Creates an index with a unique identifier
*
* @param uid Unique identifier of the index
* @param primaryKey Field to use as the primary key for documents in that index
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if an error occurs
*/
TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
HashMap<String, String> index = new HashMap<String, String>();
index.put("uid", uid);
index.put("primaryKey", primaryKey);
return httpClient.post(indexesPath().getURL(), index, TaskInfo.class);
}
/**
* Gets an index from its uid
*
* @param uid Unique identifier of the index to get
* @return Meilisearch API response as Index instance
* @throws MeilisearchException if an error occurs
*/
Index getIndex(String uid) throws MeilisearchException {
return httpClient.get(indexesPath().addSubroute(uid).getURL(), Index.class);
}
/**
* Gets indexes in the current Meilisearch instance
*
* @return Results containing a list of indexes
* @throws MeilisearchException if an error occurs
*/
Results<Index> getIndexes() throws MeilisearchException {
return httpClient.get(indexesPath().getURL(), Results.class, Index.class);
}
/**
* Gets indexes in the current Meilisearch instance
*
* @param params parameters accepted by the indexes route
* @return Results containing a list of indexes
* @throws MeilisearchException if an error occurs
*/
Results<Index> getIndexes(IndexesQuery params) throws MeilisearchException {
return httpClient.get(
indexesPath().addQuery(params.toQuery()).getURL(), Results.class, Index.class);
}
/**
* Gets indexes in the current Meilisearch instance
*
* @return List of indexes as String
* @throws MeilisearchException if an error occurs
*/
String getRawIndexes() throws MeilisearchException {
return httpClient.get(indexesPath().getURL(), String.class);
}
/**
* Gets indexes in the current Meilisearch instance
*
* @param params parameters accepted by the indexes route
* @return List of indexes as String
* @throws MeilisearchException if an error occurs
*/
String getRawIndexes(IndexesQuery params) throws MeilisearchException {
return httpClient.get(indexesPath().addQuery(params.toQuery()).getURL(), String.class);
}
/**
* Updates the primary key of an index in the Meilisearch instance
*
* @param uid Unique identifier of the index to update
* @param primaryKey New primary key field to use for documents in that index
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if an error occurs
*/
TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchException {
HashMap<String, String> index = new HashMap<String, String>();
index.put("primaryKey", primaryKey);
return httpClient.patch(indexesPath().addSubroute(uid).getURL(), index, TaskInfo.class);
}
/**
* Rename an index by changing its uid.
*
* @param uid Unique identifier of the index to rename
* @param indexUid New unique identifier for the index
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if an error occurs
*/
TaskInfo updateIndexUid(String uid, String indexUid) throws MeilisearchException {
HashMap<String, String> body = new HashMap<>();
body.put("uid", indexUid);
return httpClient.patch(indexesPath().addSubroute(uid).getURL(), body, TaskInfo.class);
}
/**
* Deletes an index in the Meilisearch instance
*
* @param uid Unique identifier of the index to delete
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if an error occurs
*/
TaskInfo deleteIndex(String uid) throws MeilisearchException {
return httpClient.delete(indexesPath().addSubroute(uid).getURL(), TaskInfo.class);
}
/** Creates an URLBuilder for the constant route indexes */
private URLBuilder indexesPath() {
return new URLBuilder("/indexes");
}
}