私はpythonでelasticsearchを使用しています。dsl
Pythonでドライバーを使用します。
私のスクリプトは以下の通りです。
import time
from elasticsearch_dsl import DocType, String
from elasticsearch import exceptions as es_exceptions
from elasticsearch_dsl.connections import connections
ELASTICSEARCH_INDEX = 'test'
class StudentDoc(DocType):
student_id = String(required=True)
tags = String(null_value=[])
class Meta:
index = ELASTICSEARCH_INDEX
def save(self, **kwargs):
'''
Override to set metadata id
'''
self.meta.id = self.student_id
return super(StudentDoc, self).save(**kwargs)
# Define a default Elasticsearch client
connections.create_connection(hosts=['localhost:9200'])
# create the mappings in elasticsearch
StudentDoc.init()
student_doc_obj = \
StudentDoc(
student_id=str(1),
tags=['test'])
try:
student_doc_obj.save()
except es_exceptions.SerializationError as ex:
# catch both exception raise by elasticsearch
LOGGER.error('Error while creating elasticsearch data')
LOGGER.exception(ex)
else:
print "*"*80
print "Student Created:", student_doc_obj
print "*"*80
search_docs = \
StudentDoc \
.search().query('ids',
values=["1"])
try:
student_docs = search_docs.execute()
except es_exceptions.NotFoundError as ex:
LOGGER.error('Unable to get data from elasticsearch')
LOGGER.exception(ex)
else:
print "$"*80
print student_docs
print "$"*80
time.sleep(2)
search_docs = \
StudentDoc \
.search().query('ids',
values=["1"])
try:
student_docs = search_docs.execute()
except es_exceptions.NotFoundError as ex:
LOGGER.error('Unable to get data from elasticsearch')
LOGGER.exception(ex)
else:
print "$"*80
print student_docs
print "$"*80
このスクリプトでは、作成StudentDoc
時に同じドキュメントにアクセスしようとしています。レコードでempty
行うと、応答が得られます。search
出力
********************************************************************************
Student Created: {'student_id': '1', 'tags': ['test']}
********************************************************************************
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<Response: []>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<Response: [{u'student_id': u'1', u'tags': [u'test']}]>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
save
コマンドを実行してデータを保存してから、search
tat データを返さないのはなぜですか。2
2 回目のスリープの後、データを返します。:(
curl
コマンド、同じ出力で同じことを試みました。
echo "Create Data"
curl http://localhost:9200/test/student_doc/2 -X PUT -d '{"student_id": "2", "tags": ["test"]}' -H 'Content-type: application/json'
echo
echo "Search ID"
curl http://localhost:9200/test/student_doc/_search -X POST -d '{"query": {"ids": {"values": ["2"]}}}' -H 'Content-type: application/json'
echo
データをelasticsearchに保存するのに遅延はありますか?