这是我的映射(一些字段已重命名/删除),我正在使用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
}
但是它只是抛出一个上下文,必须是关键字或文本错误。我是否必须放弃做一个全新的属性username-autocomplete?还是有某种神奇的方式可以让我在同一属性上有一个上下文完成建议器和另一个字段,并且能够像其他多字段属性一样进行索引?
最佳答案
第二种方法是正确的方法(即在上下文中使用路径),但是您需要将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
}
关于elasticsearch - 是否不可能为一个属性具有多个字段的文档建立索引,其中一个字段是具有上下文的完成类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55736011/