ES使用示例

ES使用的一些示例:

# -*- coding: utf-8 -*-

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-25 10:45  萤huo虫  阅读(135)  评论(0编辑  收藏  举报