弹性搜索中的嵌套文档 [英] Nested documents in Elasticsearch

查看:153
本文介绍了弹性搜索中的嵌套文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写资产管理应用程序。它允许用户通过向资产添加诸如文本字段,选择菜单等的HTML控件来存储任意资产属性。该属性的JSON表示然后成为存储在couchdb中的资产JSON文档的一部分。资产在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,
标题:类别,
数据:[
0d7e6233e5f48b4f55c5376bf00b1be5,
0d7e6233e5f48b4f55c5376bf00d94cf
],
requiredBySystem:true
},
{
id:9399fb27448b1e5dfdca01816207uy5a,
type:radio,
title:Radio Buttons,
data:[
{
text:Button 1,
checked:false
} ,
{
text:Button 2,
checked:true
}
],
requiredBySystem:true
$ b {
id:9399fb27448b1e5dfdca01816205tgh6,
type:checkboxes,
heading:Checkboxes,
数据:[
{
text:Box 1,
checked:false
},
{
text Box 2,
checked:true
}
],
requiredBySystem:true
},
{
id:9399fb27448b1e5dfdca0181620k81gt,
type:select,
:选择菜单,
data:[
{
text:选项1,
checked:false
},
{
text:选项2,
checked:true
}
],
requiredBySystem:true
}
]
}

我不知道如果把属性放在数组是允许基于属性值搜索资产的最佳方式。将属性直接附加到资产作为属性会更好吗?我现在在弹性搜索中试验。如果我按照原样尝试存储文档,则弹性搜索会返回错误:



error:MapperParsingException [解析失败[attributes.data]];嵌套:ElasticSearchIllegalArgumentException [未知的财产[text]];



我使用以下映射:

 mappings:{
asset:{
properties:{
_id:{
type
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
},
标题:{
type:string
},
data:{
type:string
}
}
}
}
}
}





Troy

解决方案

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



基本上你不能这样做:

 
data:[
0d7e6233e5f48b4f55c5376bf00b1be5,
0d7e6233e5f48b4f55c5376bf00d94cf
],



并且:



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

],



data 的第一个实例告诉ES数据是字符串数组。但是,第二个数据表示嘿,我是一个对象!,这就是为什么ES抛出错误。



您可以通过明确声明数据作为对象并设置启用:false 来回避此问题,但这可能不是解决方案(因为这只是告诉ES将数据存储为文本字段,而不需要解析。



另一种选择是重组数据,或将数据分割到其文档中(例如,父/子映射)


I'm writing an asset management application. It lets users store arbitrary asset attributes by adding an html control such as a text field, select menu, etc. to the asset. A JSON representation of the attribute then becomes part of the asset JSON document stored in couchdb. An asset has the following structure in 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
       }
   ]
}

I'm not sure if putting attributes in an array is the best way to allow searching for an asset based on an attribute value. Would it be better to attach the attribute directly to the asset as a property? I'm experimenting now in elasticsearch. If I try and store the document as is, elasticsearch returns an error:

"error" : "MapperParsingException[Failed to parse [attributes.data]]; nested: ElasticSearchIllegalArgumentException[unknown property [text]]; "

I"m using the following mapping:

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

Not sure where I'm going wrong here. Thanks for your help!

Troy

解决方案

The problem is arising from the way your document is structured. attribute.data is both a string/array of strings and a full, inner object. ES doesn't allow the "type" of a property to change.

Basically, you can't have this:

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

and this:


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

in the same document. The first instance of data tells ES that "data is an array of strings". But then the second instance of data says "Hey, I'm an object!", which is why ES is throwing an error.

You can sidestep this issue by explicitly declaring data as an object and setting enabled: false, but this is probably not the solution you want (since that just tells ES to store data as a text field, with no parsing.

The other option is to either restructure your data, or split data into it's document (e.g. Parent/Child mapping)

这篇关于弹性搜索中的嵌套文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆