0

我正在使用 Elastic search 6.1 版本 我的数据附加正确,我在请求末尾添加了 '\n'。

我的代码如下:

def insert_in_bulk(self, filee, rtype):
    U = urljoin(self.args.host, "/_bulk")
    body = []
    f = open(filee)
    for line in f:
        action = {
                'index' :{
                '_index' : self.args.index,
                '_type' : rtype,
                  }
                }
        item = {
            'word' : line.strip()
            }

        body.append(json.dumps(action))
        body.append(json.dumps(item))

    f.close()

    body = '\n'.join(body)+'\n'
    success = False
    try:
        r = requests.post(U, data=body)
        self.log.info("after request")
        if r.status_code == 200:
            success = True
        r = r.json()
        self.log.info("inserted %s items of type = %s", self.args.index , rtype)
    except (SystemExit, KeyboardInterrupt): raise
    except:
          self.log.exception("during bulk index")

    if not success:
             self.log.error("failed to index records of type = %s", rtype)

我正在使用 python 连接到弹性搜索。

我从这个链接 Bulk index document from JSON file into ElasticSearch得到了答案

我必须将标头作为 application/x-ndjson 传递给请求。

4

1 回答 1

1

虽然问了很长时间的问题,但我想给出一个在大多数情况下对我有用的解决方案,

def insert_in_bulk(self, filee, rtype):
    U = urljoin(self.args.host, "/_bulk")
    body = []
    f = open(filee)
    for line in f:
        action = {
                'index' :{
                '_index' : self.args.index,
                '_type' : rtype,
                  }
                }
        item = {
            'word' : line.strip()
            }

        body.append(json.dumps(action))
        body.append(json.dumps(item))

    f.close()


    payload = ""
    for l in body:
        payload = payload + f"{l} \n"
    data = payload.encode('utf-8')

    r = requests.post(U, data=data, headers={"Content-Type": "application/x-ndjson"})
    print(r.text)
于 2018-05-17T11:25:31.480 回答