存储 JSON 数组字符串 elasticsearch Bug

Storing JSON array string elasticsearch Bug

我观察到 Elasticsearch 5.2 出现了一些奇怪的行为,并且无法调试——因为没有抛出任何错误,我也无法在网上找到类似的 issues/documentation。

我在 elasticsearch 中将 JSON 数组存储为 "string"(使用 python 的 json.dumps())——长话短说,我有这样做。但是,当我执行 DSL 查询时,仅显示包含 1 个对象的 JSON 数组(存储为单个字符串)。如果超过 1,那么它只是 returns 一个空桶 0 个对象。我将它们存储在一个名为 "metadata" 的字段中。

我很困惑为什么只显示了数据的一个子集,而忽略了其他数据(json 数组中有超过 1 个对象)。数据被编码为字符串。我确实知道索引中存储的数据。我可以在 kibana "discovery" 中看到它——因为我可以看到带有多个对象的大 JSON 字符串。

示例 1(JSON 带 1 个对象的字符串):

[{"score": 0.8829717636108398, "height": 0.875460147857666, "width": 0.3455989360809326, "y": 0.08105117082595825, "x": 0.5616265535354614, "note": "box1"}]

示例 2:

[{"score": 0.8829717636108398, "height": 0.875460147857666, "width": 0.3455989360809326, "y": 0.08105117082595825, "x": 0.5616265535354614, "note": "box1"}, {"score": 0.6821991136108398, "height": 0.875460147857666, "width": 0.3455989360809326, "y": 0.08105117082595825, "x": 0.5616265535354614, "note": "box2"}]

这是我的查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "analyze_wildcard": true,
            "query": "*"
          }
        },
        {
          "range": {
            "created_at": {
              "gte": 1508012482796,
              "lte": 1508014282797,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "must_not": []
    }
  },
  "size": 0,
  "_source": {
    "excludes": []
  },
  "aggs": {
    "5": {
      "terms": {
        "field": "metadata.keyword",
        "size": 31,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}

此查询仅 returns 个包含 1 个对象的字符串。见下文:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4214,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "5": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 35,
      "buckets": [
        {
          "key": "[]",
          "doc_count": 102
        },
        {
          "key": "{}",
          "doc_count": 8
        },
        {
          "key": "[{\"score\": 0.9015679955482483, \"height\": 0.8632315695285797, \"width\": 0.343660831451416, \"y\": 0.08102986216545105, \"x\": 0.5559845566749573, \"note\": \"box11\"}]",
          "doc_count": 6
        },
        {
          "key": "[{\"score\": 0.6365205645561218, \"height\": 0.9410756528377533, \"width\": 0.97696852684021, \"y\": 0.04701271653175354, \"x\": 0.013666868209838867, \"note\": \"box17\"}]",
          "doc_count": 4
        },
...
}

据观察,只有 JSON 个字符串和 1 个对象(即 [{..}])的数据是 returned/visible。它完全忽略了具有多个对象的字符串(即 [{...},{...}])。

更多说明:

如果您使用的是默认映射,这很可能是因为您的关键字映射具有 ignore_above: 256 设置并且如下所示:

{
  "mappings": {
    "my_type": {
      "properties": {
        "metadata": {
          "type":  "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

您可以增加该限制以便索引您的 JSON 字符串超过 256 个字符。