|
@@ -8,13 +8,20 @@ import io.milvus.param.ConnectParam;
|
8
|
8
|
import io.milvus.param.IndexType;
|
9
|
9
|
import io.milvus.param.MetricType;
|
10
|
10
|
import io.milvus.param.collection.*;
|
|
11
|
+import io.milvus.param.dml.DeleteParam;
|
11
|
12
|
import io.milvus.param.highlevel.collection.ListCollectionsParam;
|
12
|
13
|
import io.milvus.param.highlevel.collection.response.ListCollectionsResponse;
|
|
14
|
+import io.milvus.param.highlevel.dml.QuerySimpleParam;
|
|
15
|
+import io.milvus.param.highlevel.dml.response.QueryResponse;
|
13
|
16
|
import io.milvus.param.index.CreateIndexParam;
|
|
17
|
+import io.milvus.response.QueryResultsWrapper;
|
14
|
18
|
|
15
|
19
|
import java.text.SimpleDateFormat;
|
|
20
|
+import java.util.ArrayList;
|
|
21
|
+import java.util.Arrays;
|
16
|
22
|
import java.util.List;
|
17
|
23
|
import java.util.TimeZone;
|
|
24
|
+import java.util.stream.Collectors;
|
18
|
25
|
|
19
|
26
|
public class MilvusService {
|
20
|
27
|
private MilvusServiceClient milvusClient;
|
|
@@ -48,6 +55,12 @@ public class MilvusService {
|
48
|
55
|
.withMaxLength(256)
|
49
|
56
|
.build();
|
50
|
57
|
|
|
58
|
+ FieldType fieldTypeField = FieldType.newBuilder()
|
|
59
|
+ .withName("file_type")
|
|
60
|
+ .withDataType(DataType.VarChar)
|
|
61
|
+ .withMaxLength(256)
|
|
62
|
+ .build();
|
|
63
|
+
|
51
|
64
|
FieldType contentField = FieldType.newBuilder()
|
52
|
65
|
.withName("content")
|
53
|
66
|
.withDataType(DataType.VarChar)
|
|
@@ -65,6 +78,7 @@ public class MilvusService {
|
65
|
78
|
.withDescription(description)
|
66
|
79
|
.addFieldType(idField)
|
67
|
80
|
.addFieldType(fileNameField)
|
|
81
|
+ .addFieldType(fieldTypeField)
|
68
|
82
|
.addFieldType(contentField)
|
69
|
83
|
.addFieldType(vectorField)
|
70
|
84
|
.build();
|
|
@@ -92,12 +106,14 @@ public class MilvusService {
|
92
|
106
|
ListCollectionsResponse listResponse = milvusClient.listCollections(listParam).getData();
|
93
|
107
|
if (listResponse != null) {
|
94
|
108
|
List<String> collectionNames = milvusClient.listCollections(listParam).getData().collectionNames;
|
95
|
|
- for (int i = 0; i < collectionNames.size(); i++) {
|
|
109
|
+ for (String collectionName : collectionNames) {
|
96
|
110
|
JSONObject jsonObject = new JSONObject();
|
97
|
|
- DescribeCollectionParam describeParam = DescribeCollectionParam.newBuilder().withCollectionName(collectionNames.get(i)).build();
|
|
111
|
+ DescribeCollectionParam describeParam = DescribeCollectionParam.newBuilder()
|
|
112
|
+ .withCollectionName(collectionName)
|
|
113
|
+ .build();
|
98
|
114
|
DescribeCollectionResponse describeResponse = milvusClient.describeCollection(describeParam).getData();
|
99
|
115
|
jsonObject.put("collectionId", describeResponse.getCollectionID());
|
100
|
|
- jsonObject.put("collectionName", collectionNames.get(i));
|
|
116
|
+ jsonObject.put("collectionName", collectionName);
|
101
|
117
|
SimpleDateFormat beijingFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
102
|
118
|
beijingFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
103
|
119
|
String beijingTime = beijingFormat.format(describeResponse.getCreatedUtcTimestamp());
|
|
@@ -126,10 +142,53 @@ public class MilvusService {
|
126
|
142
|
*/
|
127
|
143
|
public void deleteCollectionName(String collectionName) {
|
128
|
144
|
DropCollectionParam param = DropCollectionParam.newBuilder()
|
129
|
|
- .withCollectionName(collectionName).build();
|
|
145
|
+ .withCollectionName(collectionName)
|
|
146
|
+ .build();
|
130
|
147
|
milvusClient.dropCollection(param);
|
131
|
148
|
}
|
132
|
149
|
|
|
150
|
+ /**
|
|
151
|
+ * 查询知识库文件
|
|
152
|
+ */
|
|
153
|
+ public List<String> listDocument(String collectionName, String fileType) {
|
|
154
|
+ List<String> documentList = new ArrayList<>();
|
|
155
|
+ loadCollectionName(collectionName);
|
|
156
|
+ QuerySimpleParam queryParam = QuerySimpleParam.newBuilder()
|
|
157
|
+ .withCollectionName(collectionName)
|
|
158
|
+ .withFilter(String.format("file_type == \"%s\"", fileType))
|
|
159
|
+ .withOutputFields(Arrays.asList("file_type", "file_name"))
|
|
160
|
+ .withLimit(16384L)
|
|
161
|
+ .build();
|
|
162
|
+ QueryResponse queryResults = milvusClient.query(queryParam).getData();
|
|
163
|
+ List<QueryResultsWrapper.RowRecord> rowRecordList = queryResults.getRowRecords();
|
|
164
|
+ for (QueryResultsWrapper.RowRecord rowRecord : rowRecordList) {
|
|
165
|
+ documentList.add(rowRecord.get("file_name").toString());
|
|
166
|
+ }
|
|
167
|
+ releaseCollectionName(collectionName);
|
|
168
|
+ return documentList.stream().distinct().collect(Collectors.toList());
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ /**
|
|
172
|
+ * 删除知识库文件
|
|
173
|
+ */
|
|
174
|
+ public void removeDocument(String collectionName, String fileName) {
|
|
175
|
+ DeleteParam deleteParam = DeleteParam.newBuilder()
|
|
176
|
+ .withCollectionName(collectionName)
|
|
177
|
+ .withExpr(String.format("file_name == \"%s\"", fileName))
|
|
178
|
+ .build();
|
|
179
|
+ milvusClient.delete(deleteParam);
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ /**
|
|
183
|
+ * 加载知识库Collection
|
|
184
|
+ */
|
|
185
|
+ public void loadCollectionName(String collectionName) {
|
|
186
|
+ LoadCollectionParam param = LoadCollectionParam.newBuilder()
|
|
187
|
+ .withCollectionName(collectionName)
|
|
188
|
+ .build();
|
|
189
|
+ milvusClient.loadCollection(param);
|
|
190
|
+ }
|
|
191
|
+
|
133
|
192
|
/**
|
134
|
193
|
* 释放知识库Collection
|
135
|
194
|
*/
|