将 Numpy 数组索引到 Elasticsearch 中

Apr*_*cot 1 python arrays elasticsearch

我正在使用 Elasticsearch 6.4 和 Python 3。我正在处理图像,输出之一是 Numpy 数组。我正在尝试将 Numpy 数组以及图像分析中的其他数据字段索引到 Elasticsearch 中。我有三个字段:

  1. 图片名
  2. 时间戳
  3. Numpy 数组。

它们看起来如下:

imagename: 123def321_1548492175.jpg 
time_stamp: 1548492175 [
encod:
array([ -1.42405510e-01,   8.58794246e-03,   4.45950478e-02,
        -1.81895699e-02,  -5.53448014e-02,  -1.73689388e-02,
        -4.21237871e-02,  -8.25227201e-02,   1.56264022e-01,
        -3.99713218e-02,   1.60366639e-01,   4.53100577e-02,
        -2.09424138e-01,  -5.07910103e-02,  -4.65360470e-04,
         8.38596523e-02,  -1.19933985e-01,  -1.71518624e-01,
        -1.26374453e-01  ])]
Run Code Online (Sandbox Code Playgroud)

实际的数组长度要长得多。当我获取索引时,它的定义如下:

{
  "g6jy834005er" : {
    "aliases" : { },
    "mappings" : {
      "images" : {
        "dynamic" : "false",
        "properties" : {
          "encod" : {
            "type" : "nested"
          },
          "imagename" : {
            "type" : "text"
          },
          "time_stamp" : {
            "type" : "integer"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1548499595840",
        "number_of_shards" : "1",
        "number_of_replicas" : "0",
        "uuid" : "OP-qab-XRfGQ_oZZEvTClw",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "g6jy834005er"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的 Python 代码中,我使用以下内容来索引值:

doc = {'imagename': name, 'time_stamp': tm,'encod':fenc}
es.index(index=indx, doc_type="images", body=doc)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

elasticsearch.exceptions.SerializationError: ({'imagename': '123def321_1548492174.jpg', 'time_stamp': '1548492174', 'encod': [array([-0.16764426,  0.01966568,  0.04131121, -0.01985365, -0.07606418,....

TypeError("Unable to serialize array([-0.16764426,  0.01966568,  0.04131121, -0.01985365,.....(type: <class 'numpy.ndarray'>)",))
Run Code Online (Sandbox Code Playgroud)

我仅在过去两周使用 Elasticsearch。我尝试将编码字段的数据类型更改为longnestedobjecttext等。但没有任何帮助。

Vis*_*ddy 6

Elasticsearch 公开 JSON API,因此pyelasticsearch支持 JSON 对象中有效的数据类型。显然,numpy.ndarray不是其中之一。

如果您必须插入 numpy 数组,这是一个可能的解决方法:

encod_np_array = np.array([ -1.42405510e-01,   8.58794246e-03,   4.45950478e-02,
        -1.81895699e-02,  -5.53448014e-02,  -1.73689388e-02,
        -1.26374453e-01  ])
encod_list = encod_np_array.tolist()
doc = {'imagename': name, 'time_stamp': tm,'encod':encod_list}
Run Code Online (Sandbox Code Playgroud)

然后打电话es.index()