条件令牌过滤器
编辑条件令牌过滤器
编辑将一组令牌过滤器应用于与提供的谓词脚本中的条件匹配的令牌。
此过滤器使用 Lucene 的 ConditionalTokenFilter。
示例
编辑以下 分析 API 请求使用 condition
过滤器匹配 THE QUICK BROWN FOX
中少于 5 个字符的令牌。然后,它将 lowercase
过滤器应用于那些匹配的令牌,将其转换为小写。
resp = client.indices.analyze( tokenizer="standard", filter=[ { "type": "condition", "filter": [ "lowercase" ], "script": { "source": "token.getTerm().length() < 5" } } ], text="THE QUICK BROWN FOX", ) print(resp)
response = client.indices.analyze( body: { tokenizer: 'standard', filter: [ { type: 'condition', filter: [ 'lowercase' ], script: { source: 'token.getTerm().length() < 5' } } ], text: 'THE QUICK BROWN FOX' } ) puts response
const response = await client.indices.analyze({ tokenizer: "standard", filter: [ { type: "condition", filter: ["lowercase"], script: { source: "token.getTerm().length() < 5", }, }, ], text: "THE QUICK BROWN FOX", }); console.log(response);
GET /_analyze { "tokenizer": "standard", "filter": [ { "type": "condition", "filter": [ "lowercase" ], "script": { "source": "token.getTerm().length() < 5" } } ], "text": "THE QUICK BROWN FOX" }
过滤器生成以下令牌:
[ the, QUICK, BROWN, fox ]
可配置参数
编辑自定义并添加到分析器
编辑要自定义 condition
过滤器,请复制它以创建新自定义令牌过滤器的基础。您可以使用其可配置参数修改过滤器。
例如,以下 创建索引 API 请求使用自定义 condition
过滤器来配置新的 自定义分析器。自定义 condition
过滤器匹配流中的第一个令牌。然后,它使用 reverse
过滤器反转该匹配令牌。
resp = client.indices.create( index="palindrome_list", settings={ "analysis": { "analyzer": { "whitespace_reverse_first_token": { "tokenizer": "whitespace", "filter": [ "reverse_first_token" ] } }, "filter": { "reverse_first_token": { "type": "condition", "filter": [ "reverse" ], "script": { "source": "token.getPosition() === 0" } } } } }, ) print(resp)
response = client.indices.create( index: 'palindrome_list', body: { settings: { analysis: { analyzer: { whitespace_reverse_first_token: { tokenizer: 'whitespace', filter: [ 'reverse_first_token' ] } }, filter: { reverse_first_token: { type: 'condition', filter: [ 'reverse' ], script: { source: 'token.getPosition() === 0' } } } } } } ) puts response
const response = await client.indices.create({ index: "palindrome_list", settings: { analysis: { analyzer: { whitespace_reverse_first_token: { tokenizer: "whitespace", filter: ["reverse_first_token"], }, }, filter: { reverse_first_token: { type: "condition", filter: ["reverse"], script: { source: "token.getPosition() === 0", }, }, }, }, }, }); console.log(response);
PUT /palindrome_list { "settings": { "analysis": { "analyzer": { "whitespace_reverse_first_token": { "tokenizer": "whitespace", "filter": [ "reverse_first_token" ] } }, "filter": { "reverse_first_token": { "type": "condition", "filter": [ "reverse" ], "script": { "source": "token.getPosition() === 0" } } } } } }