首页 > 解决方案 > 是否不可能索引属性具有多个字段的文档,其中一个是具有上下文的完成类型?

问题描述

这是我的映射(一些字段重命名/删除),我使用的是 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 ?或者有什么神奇的方法可以让我在同一个属性上拥有一个上下文完成建议器和另一个字段,并且能够像其他多字段属性一样进行索引?

标签: elasticsearch

解决方案


第二种方法是正确的(即在上下文中使用路径),但是您需要将user_id字段设置为 akeyword并且它将起作用:

{
    "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
}

推荐阅读