定界词频 Token 过滤器
`delimited_term_freq` token 过滤器根据提供的定界符将 token 流分离为带有相应词频的 token。一个 token 由定界符前的所有字符组成,词频是定界符后的整数。例如,如果定界符是 |
,那么对于字符串 foo|5
,foo
是 token,5
是其词频。如果没有定界符,该 token 过滤器不会修改词频。
您可以使用预配置的 delimited_term_freq
token 过滤器,或者创建一个自定义的。
预配置的 delimited_term_freq
token 过滤器
预配置的 delimited_term_freq
token 过滤器使用默认定界符 |
。要使用预配置的 token 过滤器分析文本,请向 _analyze
端点发送以下请求:
POST /_analyze
{
"text": "foo|100",
"tokenizer": "keyword",
"filter": ["delimited_term_freq"],
"attributes": ["termFrequency"],
"explain": true
}
attributes
数组指定您希望过滤 explain
参数的输出,使其仅返回 termFrequency
。响应包含原始 token 以及包含词频的 token 过滤器的解析输出。
{
"detail": {
"custom_analyzer": true,
"charfilters": [],
"tokenizer": {
"name": "keyword",
"tokens": [
{
"token": "foo|100",
"start_offset": 0,
"end_offset": 7,
"type": "word",
"position": 0,
"termFrequency": 1
}
]
},
"tokenfilters": [
{
"name": "delimited_term_freq",
"tokens": [
{
"token": "foo",
"start_offset": 0,
"end_offset": 7,
"type": "word",
"position": 0,
"termFrequency": 100
}
]
}
]
}
}
自定义 delimited_term_freq
token 过滤器
要配置自定义的 delimited_term_freq
token 过滤器,首先在映射请求中指定定界符,本例中为 ^
。
PUT /testindex
{
"settings": {
"analysis": {
"filter": {
"my_delimited_term_freq": {
"type": "delimited_term_freq",
"delimiter": "^"
}
}
}
}
}
然后使用您创建的自定义 token 过滤器分析文本。
POST /testindex/_analyze
{
"text": "foo^3",
"tokenizer": "keyword",
"filter": ["my_delimited_term_freq"],
"attributes": ["termFrequency"],
"explain": true
}
响应包含原始 token 以及带有词频的解析版本。
{
"detail": {
"custom_analyzer": true,
"charfilters": [],
"tokenizer": {
"name": "keyword",
"tokens": [
{
"token": "foo|100",
"start_offset": 0,
"end_offset": 7,
"type": "word",
"position": 0,
"termFrequency": 1
}
]
},
"tokenfilters": [
{
"name": "delimited_term_freq",
"tokens": [
{
"token": "foo",
"start_offset": 0,
"end_offset": 7,
"type": "word",
"position": 0,
"termFrequency": 100
}
]
}
]
}
}
将 delimited_token_filter
与脚本结合使用
您可以编写 Painless 脚本来计算结果中文档的自定义分数。
首先,创建一个索引并提供以下映射和设置。
PUT /test
{
"settings": {
"number_of_shards": 1,
"analysis": {
"tokenizer": {
"keyword_tokenizer": {
"type": "keyword"
}
},
"filter": {
"my_delimited_term_freq": {
"type": "delimited_term_freq",
"delimiter": "^"
}
},
"analyzer": {
"custom_delimited_analyzer": {
"tokenizer": "keyword_tokenizer",
"filter": ["my_delimited_term_freq"]
}
}
}
},
"mappings": {
"properties": {
"f1": {
"type": "keyword"
},
"f2": {
"type": "text",
"analyzer": "custom_delimited_analyzer",
"index_options": "freqs"
}
}
}
}
test
索引使用关键字分词器、定界词频 Token 过滤器(其中定界符为 ^
),以及一个包含关键字分词器和定界词频 Token 过滤器的自定义分析器。映射指定字段 f1
是一个关键字字段,字段 f2
是一个文本字段。字段 f2
使用设置中定义的自定义分析器进行文本分析。此外,指定 index_options
会通知 OpenSearch 将词频添加到倒排索引。您将使用词频为包含重复词元的文档赋予更高的分数。
接下来,使用批量上传索引两个文档。
POST /_bulk?refresh=true
{"index": {"_index": "test", "_id": "doc1"}}
{"f1": "v0|100", "f2": "v1^30"}
{"index": {"_index": "test", "_id": "doc2"}}
{"f2": "v2|100"}
以下查询搜索索引中的所有文档,并计算文档得分,其值为字段 f2
中词元 v1
的词频。
GET /test/_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"script_score": {
"script": {
"source": "termFreq(params.field, params.term)",
"params": {
"field": "f2",
"term": "v1"
}
}
}
}
}
}
在响应中,文档 1 的分数为 30,因为字段 f2
中词元 v1
的词频为 30。文档 2 的分数为 0,因为词元 v1
未出现在 f2
中。
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 30,
"hits": [
{
"_index": "test",
"_id": "doc1",
"_score": 30,
"_source": {
"f1": "v0|100",
"f2": "v1^30"
}
},
{
"_index": "test",
"_id": "doc2",
"_score": 0,
"_source": {
"f2": "v2|100"
}
}
]
}
}
参数
下表列出了 delimited_term_freq
支持的所有参数。
参数 | 必需/可选 | 描述 |
---|---|---|
定界符 | 可选 | 用于将 token 与词频分离的定界符。必须是单个非空字符。默认值为 | 。 |