python - Elasticsearch延迟存储并立即搜索

标签 python elasticsearch elasticsearch-dsl elasticsearch-py

我正在使用 用 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 时,我得到 empty 响应。

输出

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

save命令执行并存储数据,那也是为什么search不返回tat数据。 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存数据有延迟吗?

最佳答案

是的,一旦你索引了一个新文档,它在索引刷新之前是不可用的。不过,您有几个选择,主要的是。

一个。您可以在保存 student_doc_obj 之后和搜索之前使用底层连接刷新 test 索引:

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

B.您可以get 文档而不是搜索它,因为get 是完全实时的,不需要等待刷新:

student_docs = StudentDoc.get("1")

同样,使用 curl,您只需在 PUT 调用中添加 refresh 查询字符串参数

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'

或者您可以简单地通过 id 获取文档

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

关于python - Elasticsearch延迟存储并立即搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575892/

相关文章:

elasticsearch - 如何设置elasticsearch并发处理20k POST请求?

python - 使用 Pandas 将三个或更多数据帧合并为单个数据帧并保留空值

python - 在控制台而不是 Web 浏览器上显示 Django 错误

elasticsearch - 如何通过REST创建新的Kibana可视化?

每个文档的 Python elasticsearch DSL 聚合/嵌套值度量

python - 使用elasticsearch-dsl-py在子字段(包含在另一个字段中的字段)上创建查询?

elasticsearch - 在嵌套文档上查询关键字类型的范围

python - Pyqt5 Qgraphicsview 在负载上适当缩放

python - 将元组字典映射到 DataFrame 的多列

Elasticsearch 2.3 与 elasticsearch 1.7 Java 堆空间