简体   繁体   English

Elasticsearch 模式接受数组 object 或字符串(反之亦然)

[英]Elasticsearch schema to accept array object or string (vice versa)

Need to store array data in Elasticsearch.需要在Elasticsearch中存储数组数据。 The Array can contain a string or object.该数组可以包含一个字符串或 object。 So, we can accept both array types.所以,我们可以接受这两种数组类型。 Currently, I'm able to do this by defining individual array types.目前,我可以通过定义单独的数组类型来做到这一点。 But I need a generic solution that accepts strings or objects in an array.但我需要一个接受数组中的字符串或对象的通用解决方案。

Below object to store in Elasticsearch - object 下面存放在 Elasticsearch -

{
  "anotherData": [
     {
        "someData": {
            "testingName": [
                {
                    "alternateName": "Another data",
                    "areaCategory": "some data"
                }
            ]
        }
     }
   ]
}

Below is the Elasticsearch schema for storing the above data下面是用于存储上述数据的 Elasticsearch 架构

{
  ....
  "mappings": {
    "properties": {
      "testingName": {
        "properties": {
          "alternateName": {
            "type": "keyword"
          },
          "areaCategory": {
            "type": "keyword"
          }
        }
      },
      ....
    }
  }
}

. .

Another below example of an object to store in Elasticsearch -下面的另一个 object 示例存储在 Elasticsearch 中 -

{
"anotherData": [
    {
        "someData": {
            "testingName": [
                "this is only a array string"
            ]
        }
    }
  ]
}

For the above object, the Elasticsearch schema would be like the below -对于上述 object,Elasticsearch 架构将如下所示 -

{
  ....
  "mappings": {
    "properties": {
      "testingName": {
        "type": "keyword"
      },
      ....
    }
  }
}

Now, I need to combine both in the schema as "any of one" condistion.现在,我需要将模式中的两者结合为“任何一个”条件。 We can store any of the above two objects which could be an array of strings or an array of objects to store in Elasticsearch.我们可以存储上述两个对象中的任何一个,它们可以是字符串数组或对象数组以存储在 Elasticsearch 中。

So, Please do share the Elasticsearch schema that can work for both types.所以,请分享适用于这两种类型的 Elasticsearch 架构。

The only way to store either objects or string in the same field is by using the following mapping, ie a disabled object同一字段中存储对象或字符串的唯一方法是使用以下映射,即禁用 object

  "testingName": {
    "type": "object",
    "enabled": false
  }

This means that your source documents will be allowed to have a testingName field which contains either a string or an object but the BIG drawback is that you will not be able to query on that field.这意味着您的源文档将被允许具有包含字符串或 object 的testingName字段,但最大的缺点是您将无法在该字段上进行查询

In terms of data design, it makes no sense to have such "bicephalic" fields, at least ES doesn't support that.在数据设计方面,有这样的“双头”字段是没有意义的,至少 ES 不支持。 You would be better off having two different fields, one for strings and another for objects.最好有两个不同的字段,一个用于字符串,另一个用于对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM