gpt4 book ai didi

elasticsearch - Elasticsearch 中的嵌套文档

转载 作者:行者123 更新时间:2023-11-29 02:43:37 25 4
gpt4 key购买 nike

我正在编写 Assets 管理应用程序。它允许用户通过向 Assets 添加文本字段、选择菜单等 html 控件来存储任意 Assets 属性。该属性的 JSON 表示随后成为存储在 couchdb 中的 Assets JSON 文档的一部分。 Assets 在 couchdb 中具有以下结构:

{
"_id": "9399fb27448b1e5dfdca0181620418d4",
"_rev": "12-fa50eae8b50f745f9852e9fab30ef5d9",
"type": "asset",
"attributes": [
{
"id": "9399fb27448b1e5dfdca01816203d609",
"type": "text",
"heading": "Brand",
"data": "",
"requiredBySystem": true
},
{
"id": "9399fb27448b1e5dfdca01816203e68e",
"type": "userSelectMenu",
"heading": "Assigned To",
"data": "",
"requiredBySystem": true
},
{
"id": "9399fb27448b1e5dfdca01816203e9c9",
"type": "categories",
"heading": "Categories",
"data": [
"0d7e6233e5f48b4f55c5376bf00b1be5",
"0d7e6233e5f48b4f55c5376bf00d94cf"
],
"requiredBySystem": true
},
{
"id": "9399fb27448b1e5dfdca01816207uy5a",
"type": "radio",
"heading": "Radio Buttons",
"data": [
{
"text": "Button 1",
"checked": false
},
{
"text": "Button 2",
"checked": true
}
],
"requiredBySystem": true
},
{
"id": "9399fb27448b1e5dfdca01816205tgh6",
"type": "checkboxes",
"heading": "Checkboxes",
"data": [
{
"text": "Box 1",
"checked": false
},
{
"text": "Box 2",
"checked": true
}
],
"requiredBySystem": true
},
{
"id": "9399fb27448b1e5dfdca0181620k81gt",
"type": "select",
"heading": "Select Menu",
"data": [
{
"text": "Option 1",
"checked": false
},
{
"text": "Option 2",
"checked": true
}
],
"requiredBySystem": true
}
]
}

我不确定将属性放入数组中是否是允许根据属性值搜索 Assets 的最佳方式。将属性作为属性直接附加到 Assets 会更好吗?我现在正在 elasticsearch 中进行试验。如果我尝试按原样存储文档,elasticsearch 会返回错误:

“错误”:“MapperParsingException[无法解析 [attributes.data]];嵌套:ElasticSearchIllegalArgumentException[未知属性 [text]];”

我正在使用以下映射:

"mappings" : {
"asset" : {
"properties" : {
"_id": {
"type" : "string",
"index" : "not_analyzed"
},
"_rev": {
"type" : "string",
"index" : "not_analyzed"
},
"type": {
"type" : "string",
"index" : "not_analyzed"
},
"attributes": {
"properties" : {
"id" : {
"type" : "string"
},
"type" : {
"type" : "string",
"index" : "not_analyzed"
},
"heading" : {
"type" : "string"
},
"data" : {
"type" : "string"
}
}
}
}
}
}

不确定我哪里出错了。感谢您的帮助!

特洛伊

最佳答案

问题出在您文档的结构方式上。 attribute.data 既是一个字符串/字符串数组,也是一个完整的内部对象。 ES 不允许更改属性的“类型”。

基本上,你不能有这个:

"data": [
"0d7e6233e5f48b4f55c5376bf00b1be5",
"0d7e6233e5f48b4f55c5376bf00d94cf"
],

还有这个:


"data":[
{
"text":"Button 1",
"checked":false
},
{
"text":"Button 2",
"checked":true
}
],

在同一个文档中。 data 的第一个实例告诉 ES “data 是一个字符串数组”。但是 data 的第二个实例说“嘿,我是一个对象!”,这就是 ES 抛出错误的原因。

您可以通过将 data 显式声明为对象并设置 enabled: false 来回避这个问题,但这可能不是您想要的解决方案(因为这只是告诉 ES将 data 存储为文本字段,不进行解析。

另一种选择是重组您的数据,或者将数据拆分到它的文档中(例如父/子映射)

关于elasticsearch - Elasticsearch 中的嵌套文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15583032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com