我试图在弹性搜索中定义动态模板,以自动为当前未定义的翻译属性设置分析器。
E.g。以下内容完全符合我的要求,即将lang.en.title设置为使用英文分析器:
PUT /cl
{
"mappings" : {
"titles" : {
"properties" : {
"id" : {
"type" : "integer",
"index" : "not_analyzed"
},
"lang" : {
"type" : "object",
"properties" : {
"en" : {
"type" : "object",
"properties" : {
"title" : {
"type" : "string",
"index" : "analyzed",
"analyzer" : "english"
}
}
}
}
}
}
}
}
}
如预期的那样阻止了lang.en.title,例如
GET /cl/_analyze?field=lang.en.title&text=knocked
{
"tokens": [
{
"token": "knock",
"start_offset": 0,
"end_offset": 7,
"type": "<ALPHANUM>",
"position": 1
}
]
}
但是我想要做的是将lang.en的所有未来字符串属性设置为使用英语分析器使用动态模板,我似乎无法工作......
PUT /cl
{
"mappings" : {
"titles" : {
"dynamic_templates" : [{
"lang_en" : {
"path_match" : "lang.en.*",
"mapping" : {
"type" : "string",
"index" : "analyzed",
"analyzer" : "english"
}
}
}],`enter code here`
"properties" : {
"id" : {
"type" : "integer",
"index" : "not_analyzed"
},
"lang" : {
"type" : "object"
}
}
}
}
}
英语分析器并没有被应用为lang.en.title并不是按照需要进行的 -
GET /cl/_analyze?field=lang.en.title&text=knocked
{
"tokens": [
{
"token": "knocked",
"start_offset": 0,
"end_offset": 7,
"type": "<ALPHANUM>",
"position": 1
}
]
}
我缺少什么? :)
答案 0 :(得分:3)
您的动态模板已正确定义。问题是,在动态模板应用适当的映射之前,您需要为其中包含lang.en.title
字段的文档编制索引。我在本地问题中运行了您在上面定义的相同动态映射,并获得了与您相同的结果。
但是,我在索引中添加了一个文档。
POST /cl/titles/1
{
"lang.en.title": "Knocked out"
}
添加文档后,我再次运行了分析器,得到了预期的输出:
GET /cl/_analyze?field=lang.en.title&text=knocked
{
"tokens": [
{
"token": "knock",
"start_offset": 0,
"end_offset": 7,
"type": "<ALPHANUM>",
"position": 1
}
]
}
索引需要插入一个文档,以便它可以为插入的字段执行定义的映射模板。一旦该字段存在于索引中并且已应用动态映射,_analyze
API调用将按预期执行。