这是我的映射(一些字段重命名/删除),我使用的是 ES 6.0
{
"mappings": {
"_doc" :{
"properties" : {
"username" : {
"type": "keyword",
"fields": {
"suggest" : {
"type" : "completion",
"contexts": [
{
"name": "user_id",
"type": "category"
}
]
}
}
},
"user_id": {
"type": "integer"
}
}
}
}
}
现在,当我尝试使用
PUT usernames/_doc/1
{
"username" : "JOHN",
"user_id": 1
}
或
PUT usernames/_doc/1
{
"username" : {
"input": "JOHN",
"contexts: {
"user_id": 1
}
}
"user_id": 1
}
第一个不与上下文索引,第二个只是失败。我试图添加这样的路径,
{
"mappings": {
"_doc" :{
"properties" : {
"username" : {
"type": "keyword",
"fields": {
"suggest" : {
"type" : "completion",
"contexts": [
{
"name": "user_id",
"type": "category",
"path": "user_id",
}
]
}
}
},
"user_id": {
"type": "integer"
}
}
}
}
}
并再次尝试编制索引
PUT usernames/_doc/1
{
"username" : "JOHN",
"user_id": 1
}
但它只是抛出一个上下文必须是关键字或文本错误。我是否必须放弃并创建一个全新的属性用户名自动完成?或者是否有某种神奇的方法,我可以在同一属性上拥有一个上下文完成建议器和另一个字段,并且能够像其他多字段属性一样进行索引?
第二种方法是正确的(即在上下文中使用路径),但您需要将user_id
字段设置为keyword
,它将起作用:
{
"mappings": {
"_doc" :{
"properties" : {
"username" : {
"type": "keyword",
"fields": {
"suggest" : {
"type" : "completion",
"contexts": [
{
"name": "user_id",
"type": "category",
"path": "user_id",
}
]
}
}
},
"user_id": {
"type": "keyword" <--- change this
}
}
}
}
}
然后,您可以在不创建其他字段的情况下为文档编制索引,如下所示:
PUT usernames/_doc/1
{
"username" : "JOHN",
"user_id": "1" <--- wrap in double quotes
}