Elasticsearch 为文档编制索引(即添加样本)
示例
通过以下步骤安装必要的Python库:
$ pip install elasticsearch
连接到Elasticsearch,创建一个文档(例如,数据条目),然后使用Elasticsearch对文档进行“索引”。
from datetime import datetime
from elasticsearch import Elasticsearch
# Connect to Elasticsearch using default options (localhost:9200)
es = Elasticsearch()
# Define a simple Dictionary object that we'll index to make a document in ES
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
# Write a document
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])
# Fetch the document
res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])
# Refresh the specified index (or indices) to guarantee that the document
# is searchable (avoid race conditions with near realtime search)
es.indices.refresh(index="test-index")
# Search for the document
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
# Show each "hit" or search response (max of 10 by default)
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练