Elasticsearch -- python 使用

示例:

from elasticsearch import Elasticsearch

es = Elasticsearch(hosts="http://101.132.42.187:9200")

content = {
    "name": "sdfs",
    "age": 16,
    "xx": [11, 223, 33, 44]
}

# 创建索引并插入数据, 索引存在则更新数据
# result = es.index(index='test-01', document=content)
# print result
# print result['result']

# 获取索引test-01 下 id=1 的数据
# print es.get(index="test-01", id="1")


# 匹配 name = 小明 的数据
a = {
    "query": {
        'match': {
            "name": '小明'
        }
    }
}


print es.search(body=a)

# 匹配 索引test-01 中 name = sdfs 的数据
# a = {
#     "query": {
#         'match': {
#             'name': 'sdfs'
#         }
#     }
# }
# resp = es.search(body=a, index='test-01')
# print(resp)
# print(resp['_shards'])

 

posted @ 2022-07-21 15:47  萤huo虫  阅读(65)  评论(0)    收藏  举报