如何修复python Elasticsearch批量的RequestError?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复python Elasticsearch批量的RequestError?相关的知识,希望对你有一定的参考价值。
我试图从一个网站读取数据并保存到Elasticsearch。但我得到了以下错误。我想这是因为我的文档不正确。我遵循了批量弹性文档。但我还是在纠结如何解决这个问题。谁能帮帮我?
我的错误。
RequestError.RequestError(400, 'action_request') RequestError(400, 'action_request_validation_exception', 'Validation Failed: 1: type is missing;2: type is missing;')
我的代码是
import os
import requests
import time
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import json
es = Elasticsearch('http://ip:port',timeout=600)
while (True):
df = requests.get("https://my url").json()
del df["positions"][1]
print(df)
def gendata(df):
for word in df:
yield
"_index": "chapter",
"doc": "info":
"satnam":"type":"text",
"satid":"type":"integer",
"transactionscount":"type":"integer",
"positions": "satlatitude":"type":"float",
"satlongitude":"type":"float",
"sataltitude":"type":"float",
"azimuth":"type":"float",
"elevation":"type":"float",
"ra": "type":"float",
"dec": "type":"float",
"timestamp":"type":"datetime"
bulk(es,gendata(df))
time.sleep(10)`
我在网站上的Json文件是:
"info":"satname": "SPACE STATION", "satid":00000, "transactionscount":0, "position":"satlatitude":-50.00807313, "satlongitude":-37. 47024176, "sataltitude":435.04, "方位角":178.02, "海拔":-47.22, "ra":132.83647708, "dec":-72.05784906, "timestamp":1589984178。
主要是:你在做一个 bulk()
为你的映射。你应该对你的文档对象进行重构。
所以这就是你的代码应该如何重构。
创建 "chapter "索引
为'chapter'索引创建你所期望的文档的映射。
然后开始将文档索引到索引中。
还有一件事:你不需要用json风格声明映射--更好的方法是使用 elasticsearch-dsl
=> https:/elasticsearch-dsl.readthedocs.ioenlatest。
所以你要定义一个这样的类(请看上面参考资料中的细节)。
class Chapter(Document):
satnam=Text()
satid=Integer()
transactionscount=Integer()
.
.
.
class Index:
name = 'chapter'
这个类主要是给mapping -detailsindex -nameanalyzers等。
一旦你调用 Chapter.init()
- 索引 "chapter "将被创建。
然后是你的代码,用于将文档批量索引到'chapter'索引中,你刚刚使用init()创建了这个索引。
我相信你的批量插入弹性查询格式是错误的,预期的查询语法应该是
"index": "_index": "chapter"
"info":
"satnam":"type":"text",
"satid":"type":"integer",
"transactionscount":"type":"integer",
"positions": "satlatitude":"type":"float",
"satlongitude":"type":"float",
"sataltitude":"type":"float",
"azimuth":"type":"float",
"elevation":"type":"float",
"ra": "type":"float",
"dec": "type":"float",
"timestamp":"type":"datetime"
参考。https:/www.elastic.coguideenelasticsearchreferencecurrentdocs-bulk.html
以上是关于如何修复python Elasticsearch批量的RequestError?的主要内容,如果未能解决你的问题,请参考以下文章
AWS 上的 Elasticsearch:如何修复未分配的分片?
Elasticsearch:高级调优 - 查找和修复慢速 Elasticsearch 查询