elasticsearch - 是否不可能索引属性具有多个字段的文档,其中一个是具有上下文的完成类型?
问题描述
这是我的映射(一些字段重命名/删除),我使用的是 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
字段设置为 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
}
推荐阅读
- ruby-on-rails - 用于 nil:NilClass 的未定义方法“每个”,用于 @topics
- python-3.x - Tensorflow v1.12 - ImportError:无法导入名称“云”
- javascript - 泛型类型,在流类型中动态派生
- android - Android X 向后兼容性
- c - 一个变量的属性另一个不同类型的变量
- java - 更改双向链表中泛型类型节点的值
- excel - Excel中的嵌套IF语句或更好的公式
- spring-rabbit - Spring-boot 2 rabbitmq MessageConverter无法正常工作
- python - 在 Heroku 上重新启动时,如何避免 GroupMe 机器人两次读取相同的消息?
- php - 在 CentOS 7 中使用 PHP 连接数据库时访问被拒绝错误