2016-07-25 14 views
3

私はをPythonで使用しています。 Pythonでdslドライバを使用してください。すぐにストアと検索の弾性検索遅延

私のスクリプトは以下の通りです。このスクリプトで

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を作成し、作成するときに、同じドキュメントにアクセスしようとしています。私は応答を得るときにsearchレコードに。

OUTPUT

******************************************************************************** 
Student Created: {'student_id': '1', 'tags': ['test']} 
******************************************************************************** 
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
<Response: []> 
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
<Response: [{u'student_id': u'1', u'tags': [u'test']}]> 
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 

saveコマンドを実行し、ストアデータは、その後も、なぜsearch報復データを返しません。 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するデータを格納するの遅れはありますか?

答えて

2

はい、あなたはインデックス新しい文書を一度、それがされるまで使用できません。インデックスのリフレッシュが発生しますが、主なものはいくつかあります。

A. testインデックスは、保存直後の基本接続を使用していますstudent_doc_objおよびそれを検索する前に:getは完全にリアルタイムでリフレッシュを待つ必要がないとしてあなたは、代わりにそれを検索する文書をgetでき

connections.get_connection.indices.refresh(index= ELASTICSEARCH_INDEX) 

B.:

student_docs = StudentDoc.get("1") 

同様に、カールを使用して、単にあなたのPUTコール

echo "Create Data" 
curl 'http://localhost:9200/test/student_doc/2?refresh=true' -X PUT -d '{"student_id": "2", "tags": ["test"]}' -H 'Content-type: application/json' 

それとも、単にすることにより、文書を取得することができますにrefreshクエリ文字列パラメータを追加することができますid

echo "GET ID" 
curl -XGET http://localhost:9200/test/student_doc/2 
+1

ありがとうございました。私のコードでは、 'save(refresh = True)'としてコードを変更し、インデックスを更新しています。 – Nilesh

+1

うれしい! – Val