映射 parent/child 与 _id mongo river elasticsearch

mapping parent/child with _id mongo river elasticsearch

我的数据库中有 3 个 collections mongo:演出 - 场馆 - 下拉菜单

节目映射如下

"show": {
    "properties" : {
        "description": {
              "type": "string"
        },
        "image": {
              "type": "string"
         },
        "site": {
              "type": "string"
         },
        "title" : {
                "type" : "multi_field",
                "fields" : {
                    "title" : {"type" : "string", "index" : "analyzed"},
                    "raw_title" : {"type" : "string", "index" : "not_analyzed", "store": "no"}
                }
            }
    }
}

这样的场地

"venues": {
        "properties" : {
           "name" : {
                    "type" : "multi_field",
                    "fields" : {
                        "name" : {"type" : "string", "index" : "analyzed"},
                        "raw_name" : {"type" : "string", "index" : "not_analyzed", "store": "no"}
                    }
                },
            "city" : {
                    "type" : "multi_field",
                    "fields" : {
                        "city" : {"type" : "string", "index" : "analyzed"},
                        "raw_city" : {"type" : "string", "index" : "not_analyzed", "store": "no"}
                    }
                },
            "region" : {
                    "type" : "multi_field",
                    "fields" : {
                        "region" : {"type" : "string", "index" : "analyzed"},
                        "raw_region" : {"type" : "string", "index" : "not_analyzed", "store": "no"}
                    }
                },
                "state" : {
                    "type": "boolean"
                }
        }
    }

我在 mongo 中有这个模型用于下拉菜单:

{
  created: {
    type: Date,
    default: Date.now
  },
  analytics: {
    type: String,
    default: '',
    trim: true
  },
  state: {
    type: Boolean,
    default: false,
    index: true
  },
  show: {
      type: Schema.ObjectId,
      ref: 'Show'
  },
  venues:[{
    venue:{
      type: Schema.ObjectId,
      ref: 'Venue',
      index: true
    },
    site: {
      type: String,
      trim: true,
      index: true
    }
  }]
}

我会将带有 parent/child 架构的下拉列表映射到我的索引中,但我无法理解是否可以使用 ObjectId,因为我已经尝试过此映射:

"dropdown": {
            "properties" : {
                "state": {
                     "type": "boolean"
                },
                "analytics": {
                    "type": "string"
                },
                        "_parent":{
                            "type" : "show"
                        },
                "venues" : {
                    "properties" : {
                        "venue" : {
                            "_parent": {
                                "type" : "venues"
                            }
                        }
                    },
                    "site" : {"type" : "string"}
                    }
                }
        }

但是我收到了这个错误:

MapperParsingException[No type specified for property [show]]

有没有办法正确设置我的索引?

问题是您指定的 _parent 不正确。您必须不在 properties 字段中设置它,而是在它旁边设置。请参阅 documentation 和示例:

PUT /company
{
  "mappings": {
    "branch": {},
    "employee": {
      "_parent": {
        "type": "branch" 
      }
    }
  }
}

所以按照这个逻辑,我已经采用了你的映射,稍微简化了它并使它起作用:

PUT /test
{
  "mappings": {
    "show": {
      "properties": {
        "description": {
          "type": "string"
        },
        "image": {
          "type": "string"
        },
        "site": {
          "type": "string"
        },
        "title": {
          "type": "multi_field",
          "fields": {
            "title": {
              "type": "string",
              "index": "analyzed"
            },
            "raw_title": {
              "type": "string",
              "index": "not_analyzed",
              "store": "no"
            }
          }
        }
      }
    },
    "venues": {
      "properties": {
        "name": {
          "type": "multi_field",
          "fields": {
            "name": {
              "type": "string",
              "index": "analyzed"
            },
            "raw_name": {
              "type": "string",
              "index": "not_analyzed",
              "store": "no"
            }
          }
        },
        "city": {
          "type": "multi_field",
          "fields": {
            "city": {
              "type": "string",
              "index": "analyzed"
            },
            "raw_city": {
              "type": "string",
              "index": "not_analyzed",
              "store": "no"
            }
          }
        },
        "region": {
          "type": "multi_field",
          "fields": {
            "region": {
              "type": "string",
              "index": "analyzed"
            },
            "raw_region": {
              "type": "string",
              "index": "not_analyzed",
              "store": "no"
            }
          }
        },
        "state": {
          "type": "boolean"
        }
      }
    },
    "dropdown": {
      "_parent": {
        "type": "show"
      },
      "properties": {
        "state": {
          "type": "boolean"
        },
        "analytics": {
          "type": "string"
        },
        "venues": {
          "type": "object",
          "_parent": {
            "type": "venues"
          },
          "site": {
            "type": "string"
          }
        }
      }
    }
  }
}

我自己在 Elasticsearch 1.7.1 上尝试过,效果很好。

但是,我不确定您是否可以像在场所中那样在嵌套文档中声明 _parent 关系。我的映射查询没有抛出错误并接受了它。然而,看看它是如何在 head 插件中被解析的——_parent 被删除了,只剩下 object 部分,如屏幕截图所示:

如果我试图在不指定类型的情况下对其进行索引 - 将抛出此错误:

"MapperParsingException[mapping [dropdown]]; nested: MapperParsingException[No type specified for property [venues]];