Skip to content

Commit 0b76a66

Browse files
authored
Merge pull request #4 from WayneCao/main
support sparse vector && binary vector && HNSWPQ && case sensitivity && search iterator
2 parents 4c34b32 + 998a41d commit 0b76a66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2100
-253
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.baidu</groupId>
88
<artifactId>mochow-sdk-java</artifactId>
9-
<version>2.0.1</version>
9+
<version>2.2.1</version>
1010
<name>mochow-sdk-java</name>
1111
<description>Java SDK for mochow.</description>
1212
<url>https://cloud.baidu.com/doc/VDB/index.html</url>

src/main/java/com/baidu/mochow/client/MochowClient.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111
* specific language governing permissions and limitations under the License.
1212
*/
13+
1314
package com.baidu.mochow.client;
1415

1516
import org.slf4j.Logger;
@@ -72,10 +73,13 @@
7273
import com.baidu.mochow.model.BatchSearchRequest;
7374
import com.baidu.mochow.model.BatchSearchResponse;
7475
import com.baidu.mochow.model.UpsertResponse;
75-
import com.baidu.mochow.model.SearchRequest.BM25SearchRequestInterface;
76-
import com.baidu.mochow.model.SearchRequest.HybridSearchRequestInterface;
77-
import com.baidu.mochow.model.SearchRequest.SearchRequestInterface;
78-
import com.baidu.mochow.model.SearchRequest.VectorSearchRequestInterface;
76+
import com.baidu.mochow.model.ModifyTableRequest;
77+
import com.baidu.mochow.model.SearchIterator;
78+
import com.baidu.mochow.model.SearchIteratorArgs;
79+
import com.baidu.mochow.model.VectorSearchRequestInterface;
80+
import com.baidu.mochow.model.BM25SearchRequestInterface;
81+
import com.baidu.mochow.model.HybridSearchRequestInterface;
82+
import com.baidu.mochow.model.SearchRequestInterface;
7983

8084
/**
8185
* Provides the client for accessing the Baidu VDB Service.
@@ -104,6 +108,7 @@ public class MochowClient extends AbstractMochowClient {
104108
private static final String QUERY = "query";
105109
private static final String SEARCH = "search";
106110
private static final String BATCH_SEARCH = "batchSearch";
111+
private static final String MULTI_VECTOR_SEARCH = "multiVectorSearch";
107112
private static final String UPDATE = "update";
108113
private static final String SELECT = "select";
109114

@@ -175,7 +180,7 @@ public boolean hasTable(String databaseName, String tableName) throws MochowClie
175180

176181
public void dropTable(String databaseName, String tableName) throws MochowClientException {
177182
InternalRequest internalRequest = this.createRequest(
178-
new AbstractMochowRequest() {}, HttpMethodName.DELETE, TABLE_PREFIX);
183+
new AbstractMochowRequest() {}, HttpMethodName.DELETE, TABLE_PREFIX);
179184
internalRequest.addParameter("database", databaseName);
180185
internalRequest.addParameter("table", tableName);
181186
this.invokeHttpClient(internalRequest, AbstractMochowResponse.class);
@@ -369,6 +374,17 @@ public SelectResponse select(SelectRequest request) throws MochowClientException
369374
return this.invokeHttpClient(internalRequest, SelectResponse.class);
370375
}
371376

377+
public void modifyTable(ModifyTableRequest request) throws MochowClientException {
378+
InternalRequest internalRequest = this.createRequest(request, HttpMethodName.POST, TABLE_PREFIX);
379+
internalRequest.addParameter(MODIFY, "");
380+
fillPayload(internalRequest, request);
381+
this.invokeHttpClient(internalRequest, AbstractMochowResponse.class);
382+
}
383+
384+
public SearchIterator searchIterator(SearchIteratorArgs args) {
385+
return new SearchIterator(this, args);
386+
}
387+
372388
/**
373389
* Creates and initializes a new request object for the specified resource.
374390
*

src/main/java/com/baidu/mochow/examples/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.baidu.mochow.auth.Credentials;
44
import com.baidu.mochow.client.ClientConfiguration;
5+
import com.baidu.mochow.model.enums.IndexType;
56

67
public class Main {
78
public static void main(String[] args) {
@@ -12,9 +13,9 @@ public static void main(String[] args) {
1213
ClientConfiguration clientConfiguration = new ClientConfiguration();
1314
clientConfiguration.setCredentials(new Credentials(account, apiKey));
1415
clientConfiguration.setEndpoint(endpoint);
15-
MochowExample example = new MochowExample(clientConfiguration);
16+
MochowExample example = new MochowExample(clientConfiguration, IndexType.HNSW);
1617
example.example();
1718
System.out.println("Finish to execute mochow example");
1819
System.exit(0);
1920
}
20-
}
21+
}

src/main/java/com/baidu/mochow/examples/MochowExample.java

Lines changed: 437 additions & 21 deletions
Large diffs are not rendered by default.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025 Baidu, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.baidu.mochow.model;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
public class BM25SearchFields extends SearchCommonFields {
20+
public String indexName;
21+
public String searchText;
22+
23+
public void fillSearchFields(Map<String, Object> fields) {
24+
for (Map.Entry<String, Object> entry : toMap().entrySet()) {
25+
fields.put(entry.getKey(), entry.getValue());
26+
}
27+
28+
Map<String, Object> params = new HashMap<>();
29+
params.put("indexName", indexName);
30+
params.put("searchText", searchText);
31+
32+
fields.put("BM25SearchParams", params);
33+
}
34+
}

src/main/java/com/baidu/mochow/model/BM25SearchRequest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import java.util.List;
1818
import java.util.Map;
1919

20-
import com.baidu.mochow.model.SearchRequest.BM25SearchFields;
21-
import com.baidu.mochow.model.SearchRequest.BM25SearchRequestInterface;
2220
import com.baidu.mochow.model.entity.GeneralParams;
2321
import com.baidu.mochow.model.enums.ReadConsistency;
2422
import com.fasterxml.jackson.annotation.JsonValue;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2025 Baidu, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.baidu.mochow.model;
15+
16+
public interface BM25SearchRequestInterface extends SearchRequestInterface {
17+
}

src/main/java/com/baidu/mochow/model/CreateIndexRequest.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,63 @@
1313

1414
package com.baidu.mochow.model;
1515

16+
import java.util.ArrayList;
1617
import java.util.List;
1718

1819
import com.fasterxml.jackson.annotation.JsonInclude;
1920
import lombok.AllArgsConstructor;
20-
import lombok.Builder;
2121
import lombok.Getter;
22+
import lombok.NoArgsConstructor;
2223
import lombok.Setter;
2324

2425
import com.baidu.mochow.model.entity.IndexField;
2526

2627
@Getter
2728
@Setter
28-
@Builder
29+
@NoArgsConstructor
2930
@AllArgsConstructor
3031
@JsonInclude(JsonInclude.Include.NON_EMPTY)
3132
public class CreateIndexRequest extends AbstractMochowRequest {
3233
private String database;
3334
private String table;
3435
private List<IndexField> indexes;
36+
37+
private CreateIndexRequest(CreateIndexRequest.Builder builder) {
38+
this.database = builder.database;
39+
this.table = builder.table;
40+
this.indexes = builder.indexes;
41+
}
42+
43+
public static CreateIndexRequest.Builder builder() {
44+
return new CreateIndexRequest.Builder();
45+
}
46+
47+
public static class Builder {
48+
private String database;
49+
private String table;
50+
private List<IndexField> indexes;
51+
52+
private Builder() {
53+
this.indexes = new ArrayList<>();
54+
}
55+
56+
public CreateIndexRequest.Builder database(String database) {
57+
this.database = database;
58+
return this;
59+
}
60+
61+
public CreateIndexRequest.Builder table(String table) {
62+
this.table = table;
63+
return this;
64+
}
65+
66+
public CreateIndexRequest.Builder addIndex(IndexField field) {
67+
this.indexes.add(field);
68+
return this;
69+
}
70+
71+
public CreateIndexRequest build() {
72+
return new CreateIndexRequest(this);
73+
}
74+
}
3575
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 Baidu, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.baidu.mochow.model;
15+
16+
import java.util.Map;
17+
18+
public class HybridSearchFields extends SearchCommonFields {
19+
VectorSearchRequestInterface vectorRequest;
20+
BM25SearchRequestInterface bm25Request;
21+
float vectorWeight;
22+
float bm25Weight;
23+
24+
public void fillSearchFields(Map<String, Object> fields) {
25+
if (vectorRequest != null) {
26+
vectorRequest.toMap().forEach(fields::put);
27+
}
28+
29+
if (bm25Request != null) {
30+
bm25Request.toMap().forEach(fields::put);
31+
}
32+
33+
for (Map.Entry<String, Object> entry : toMap().entrySet()) {
34+
fields.put(entry.getKey(), entry.getValue());
35+
}
36+
37+
if (fields.containsKey("anns")) {
38+
@SuppressWarnings("unchecked")
39+
Map<String, Object> anns = (Map<String, Object>) fields.get("anns");
40+
anns.put("weight", vectorWeight);
41+
}
42+
43+
if (fields.containsKey("BM25SearchParams")) {
44+
@SuppressWarnings("unchecked")
45+
Map<String, Object> bm25 = (Map<String, Object>) fields.get("BM25SearchParams");
46+
bm25.put("weight", bm25Weight);
47+
}
48+
}
49+
}

src/main/java/com/baidu/mochow/model/HybridSearchRequest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
import java.util.List;
1818
import java.util.Map;
1919

20-
import com.baidu.mochow.model.SearchRequest.BM25SearchRequestInterface;
21-
import com.baidu.mochow.model.SearchRequest.HybridSearchFields;
22-
import com.baidu.mochow.model.SearchRequest.HybridSearchRequestInterface;
23-
import com.baidu.mochow.model.SearchRequest.VectorSearchRequestInterface;
2420
import com.baidu.mochow.model.entity.GeneralParams;
2521
import com.baidu.mochow.model.enums.ReadConsistency;
2622
import com.fasterxml.jackson.annotation.JsonValue;

0 commit comments

Comments
 (0)