【发布时间】:2018-04-09 20:12:54
【问题描述】:
我在尝试将 Ingest Attachment Processor Plugin 与 ElasticSearch 一起使用时遇到了麻烦(AWS 上为 5.5,本地为 5.6)。我正在使用 Python (3.6) 进行开发,并且正在使用 elasticsearch-dls library。
我正在使用Persistence,并且我的班级设置如下:
import base64
from elasticsearch_dsl.field import Attachment, Text
from elasticsearch_dsl import DocType, analyzer
lower_keyword = analyzer('keyword', tokenizer="keyword", filter=["lowercase"])
class ExampleIndex(DocType):
class Meta:
index = 'example'
doc_type = 'Example'
id = Text()
name = Text(analyzer=lower_keyword)
my_file = Attachment()
然后我有一个这样的函数,我调用它来创建索引并保存文档。
def index_doc(a_file):
# Ensure that the Index is created before any documents are saved
try:
i = Index('example')
i.doc_type(ExampleIndex)
i.create()
# todo - Pipeline creation needs to go here - But how do you do it?
except Exception:
pass
# Check for existing index
indices = ExampleIndex()
try:
s = indices.search()
r = s.query('match', name=a_file.name).execute()
if r.success():
for h in r:
indices = ExampleIndex.get(id=h.meta.id)
break
except NotFoundError:
pass
except Exception:
logger.exception("Something went wrong")
raise
# Populate the document
indices.name = a_file.name
with open(a_file.path_to_file, 'rb') as f:
contents = f.read()
indices.my_file = base64.b64encode(contents).decode("ascii")
indices.save(pipeline="attachment") if indices.my_file else indices.save()
我有一个包含内容的文本文件这是一个测试文档。当它的内容是 base64 编码时,它们变成 VGhpcyBpcyBhIHRlc3QgZG9jdW1lbnQK
如果我直接使用 CURL,那么它可以工作:
创建管道:
curl -XPUT 'localhost:9200/_ingest/pipeline/attachment?pretty' -H 'Content-Type: application/json' -d' { "description" : "Extract attachment information", "processors" : [
{
"attachment" : {
"field" : "my_file"
}
} ] }
放数据
curl -XPUT 'localhost:9200/example/Example/AV9nkyJMZAQ2lQ3CtsLb?pipeline=attachment&pretty'\
-H 'Content-Type: application/json' \
-d '{"my_file": "VGhpcyBpcyBhIHRlc3QgZG9jdW1lbnQK"}'
获取数据 http://localhost:9200/example/Example/AV9nkyJMZAQ2lQ3CtsLb?pretty
{
"_index" : "example",
"_type" : "Example",
"_id" : "AV9nkyJMZAQ2lQ3CtsLb",
"_version" : 4,
"found" : true,
"_source" : {
"my_file" : "VGhpcyBpcyBhIHRlc3QgZG9jdW1lbnQK",
"attachment" : {
"content_type" : "text/plain; charset=ISO-8859-1",
"language" : "en",
"content" : "This is a test document",
"content_length" : 25
}
}
}
问题是我看不到如何使用 elasticsearch-dsl Python 库重新创建它
更新: 除了最初创建管道之外,我现在可以让一切正常工作。如果我使用 CURL 创建管道,则只需将 .save() 方法调用更改为 .save(pipeline="attachment") 即可使用它。我更新了我之前的函数以显示这一点,并对管道创建需要去哪里发表评论。
这里是创建管道的 CURL 实现示例
curl - XPUT 'localhost:9200/_ingest/pipeline/attachment?pretty' \
- H 'Content-Type: application/json' \
- d '"description": "Extract attachment information","processors": [{"attachment": {"field": "my_field"}}]}'
【问题讨论】:
标签: python python-3.x elasticsearch elasticsearch-plugin elasticsearch-dsl