Jieba
The jieba tokenizer processes Chinese text by breaking it down into its component words.
The jieba tokenizer preserves punctuation marks as separate tokens in the output. For example, "你好!世界。" becomes ["你好", "!", "世界", "。"]. To remove these standalone punctuation tokens, use the removepunct filter.
Configuration
Milvus supports two configuration approaches for the jieba tokenizer: a simple configuration and a custom configuration.
Simple configuration
With the simple configuration, you only need to set the tokenizer to "jieba". For example:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Simple configuration: only specifying the tokenizer name
analyzer_params = {
"tokenizer": "jieba", # Use the default settings: dict=["_default_"], mode="search", hmm=True
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "jieba");
analyzerParams := map[string]any{"tokenizer": "jieba"}
use milvus::v2::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
let analyzer_params = serde_json::json!({"tokenizer": "jieba"});
Ok(())
}
nlohmann::json analyzer_params = {
{"tokenizer", "jieba"}
};
const analyzer_params = {
"tokenizer": "jieba",
};
# restful
analyzerParams='{
"tokenizer": "jieba"
}'
This simple configuration is equivalent to the following custom configuration:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Custom configuration equivalent to the simple configuration above
analyzer_params = {
"tokenizer": { # Tokenizer configuration
"type": "jieba", # Tokenizer type, fixed as "jieba"
"dict": ["_default_"], # Use the default dictionary
"mode": "search", # Use search mode for improved recall (see mode details below)
"hmm": True # Enable HMM for probabilistic segmentation
}
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", new HashMap<String, Object>() {{
put("type", "jieba");
put("dict", Collections.singletonList("_default_"));
put("mode", "search");
put("hmm", true);
}});
analyzerParams := map[string]any{
"tokenizer": map[string]any{
"type": "jieba",
"dict": []any{"_default_"},
"mode": "search",
"hmm": true,
},
}
let analyzer_params = serde_json::json!({
"tokenizer": {
"type": "jieba",
"dict": ["_default_"],
"mode": "search",
"hmm": true
}
});
nlohmann::json analyzer_params = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"_default_"}},
{"mode", "search"},
{"hmm", true}
}}
};
const analyzer_params = {
tokenizer: {
type: "jieba",
dict: ["_default_"],
mode: "search",
hmm: true,
},
};
# restful
export analyzerParams='{
"tokenizer": {
"type": "jieba",
"dict": ["_default_"],
"mode": "search",
"hmm": true
}
}'
For details on parameters, refer to Custom configuration.
Custom configuration
For more control, you can provide a custom configuration that allows you to specify a custom dictionary, select the segmentation mode, and enable or disable the Hidden Markov Model (HMM). For example:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Custom configuration with user-defined settings
analyzer_params = {
"tokenizer": {
"type": "jieba", # Fixed tokenizer type
"dict": ["customDictionary"], # Custom dictionary list; replace with your own terms
"mode": "exact", # Use exact mode (non-overlapping tokens)
"hmm": False # Disable HMM; unmatched text will be split into individual characters
}
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", new HashMap<String, Object>() {{
put("type", "jieba");
put("dict", Arrays.asList("customDictionary"));
put("mode", "exact");
put("hmm", false);
}});
analyzerParams := map[string]interface{}{
"tokenizer": map[string]interface{}{
"type": "jieba",
"dict": []string{"customDictionary"},
"mode": "exact",
"hmm": false,
},
}
let analyzer_params = serde_json::json!({
"tokenizer": {
"type": "jieba",
"dict": ["customDictionary"],
"mode": "exact",
"hmm": false
}
});
nlohmann::json analyzerParams = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"customDictionary"}},
{"mode", "exact"},
{"hmm", false}
}}
};
const analyzer_params = {
tokenizer: {
type: "jieba",
dict: ["customDictionary"],
mode: "exact",
hmm: false,
},
};
# restful
export analyzerParams='{
"tokenizer": {
"type": "jieba",
"dict": ["customDictionary"],
"mode": "exact",
"hmm": false
}
}'
Parameter | Description | Default Value |
|---|---|---|
| The type of tokenizer. This is fixed to |
|
| A list of dictionaries that the analyzer will load as its vocabulary source. Built-in options:
You can also mix the built‑in dictionary with any number of custom dictionaries. Example: |
|
| The segmentation mode. Possible values:
For more information, refer to Jieba GitHub Project. |
|
| A boolean flag indicating whether to enable the Hidden Markov Model (HMM) for probabilistic segmentation of words not found in the dictionary. |
|
After defining analyzer_params, you can apply them to a VARCHAR field when defining a collection schema. This allows Zilliz Cloud to process the text in that field using the specified analyzer for efficient tokenization and filtering. For details, refer to Example use.
Examples
Before applying the analyzer configuration to your collection schema, verify its behavior using the run_analyzer method.
Analyzer configuration
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
analyzer_params = {
"tokenizer": {
"type": "jieba",
"dict": ["结巴分词器"],
"mode": "exact",
"hmm": False
}
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", new HashMap<String, Object>() {{
put("type", "jieba");
put("dict", Arrays.asList("结巴分词器"));
put("mode", "exact");
put("hmm", false);
}});
analyzerParams := map[string]interface{}{
"tokenizer": map[string]interface{}{
"type": "jieba",
"dict": []string{"结巴分词器"},
"mode": "exact",
"hmm": false,
},
}
let analyzer_params = serde_json::json!({
"tokenizer": {
"type": "jieba",
"dict": ["结巴分词器"],
"mode": "exact",
"hmm": false
}
});
nlohmann::json analyzerParams = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"结巴分词器"}},
{"mode", "exact"},
{"hmm", false}
}}
};
const analyzer_params = {
tokenizer: {
type: "jieba",
dict: ["结巴分词器"],
mode: "exact",
hmm: false,
},
};
# restful
export analyzerParams='{
"tokenizer": {
"type": "jieba",
"dict": ["结巴分词器"],
"mode": "exact",
"hmm": false
}
}'
Verification using run_analyzer
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
from pymilvus import (
MilvusClient,
)
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# Sample text to analyze
sample_text = "milvus结巴分词器中文测试"
# Run the standard analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Standard analyzer output:", result)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.RunAnalyzerReq;
import io.milvus.v2.service.vector.response.RunAnalyzerResp;
import java.util.ArrayList;
import java.util.List;
ConnectConfig config = ConnectConfig.builder()
.uri("YOUR_CLUSTER_ENDPOINT")
.token("YOUR_CLUSTER_TOKEN")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
List<String> texts = new ArrayList<>();
texts.add("milvus结巴分词器中文测试");
RunAnalyzerResp resp = client.runAnalyzer(RunAnalyzerReq.builder()
.texts(texts)
.analyzerParams(analyzerParams)
.build());
List<RunAnalyzerResp.AnalyzerResult> results = resp.getResults();
import (
"context"
"encoding/json"
"fmt"
"github.com/milvus-io/milvus/client/v3/milvusclient"
)
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: "YOUR_CLUSTER_ENDPOINT",
APIKey: "YOUR_CLUSTER_TOKEN",
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
bs, _ := json.Marshal(analyzerParams)
texts := []string{"milvus结巴分词器中文测试"}
option := milvusclient.NewRunAnalyzerOption(texts...).
WithAnalyzerParamsStr(string(bs))
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
use milvus::v2::prelude::*;
let resp = client.run_analyzer(RunAnalyzerRequest::builder()
.texts(vec!["milvus结巴分词器中文测试"])
.analyzer_params(analyzer_params)
.build()?).await?;
println!("{:?}", resp.results());
#include <iostream>
#include <string>
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
milvus::ConnectParam connect_param{"YOUR_CLUSTER_ENDPOINT", "YOUR_CLUSTER_TOKEN"};
auto status = client->Connect(connect_param);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
std::string text = "milvus结巴分词器中文测试";
auto request = milvus::RunAnalyzerRequest()
.AddText(text)
.WithAnalyzerParams(analyzer_params);
milvus::RunAnalyzerResponse response;
status = client->RunAnalyzer(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
const analyzer_params = {
tokenizer: {
type: "jieba",
dict: ["结巴分词器"],
mode: "exact",
hmm: false,
},
};
const result = await client.runAnalyzer({
text: "milvus结巴分词器中文测试",
analyzer_params,
});
console.log(result.results);
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/common/run_analyzer" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--data '{
"text": ["milvus结巴分词器中文测试"],
"analyzerParams": "{\"tokenizer\":{\"type\":\"jieba\",\"dict\":[\"结巴分词器\"],\"mode\":\"exact\",\"hmm\":false}}"
}'
Expected output
['milvus', '结巴分词器', '中', '文', '测', '试']