Nil*_*esh 4 python elasticsearch elasticsearch-dsl elasticsearch-py
我在用
弹性搜索用python。并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
Run Code Online (Sandbox Code Playgroud)
在此脚本中,我正在创建,StudentDoc并在创建时尝试访问相同的文档。记录下来empty时我会得到回应search。
输出值
********************************************************************************
Student Created: {'student_id': '1', 'tags': ['test']}
********************************************************************************
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<Response: []>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<Response: [{u'student_id': u'1', u'tags': [u'test']}]>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
在将数据存储到Elasticsearch时是否有延迟?
是的,一旦您为新文档建立索引,该索引将在刷新索引后才可用。不过,您有几个选择,主要是。
答:您可以refresh将test使用底层的连接只是保存后的索引student_doc_obj和搜索之前:
connections.get_connection.indices.refresh(index= ELASTICSEARCH_INDEX)
Run Code Online (Sandbox Code Playgroud)
B.您可以get直接搜索文档而不是搜索文档,因为a get是完全实时的,不需要等待刷新:
student_docs = StudentDoc.get("1")
Run Code Online (Sandbox Code Playgroud)
同样,使用curl,您只需在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'
Run Code Online (Sandbox Code Playgroud)
或者,您也可以通过id简单地获取文档
echo "GET ID"
curl -XGET http://localhost:9200/test/student_doc/2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
632 次 |
| 最近记录: |