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 @   萤huo虫  阅读(146)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示