es python search 返回_Elasticsearch - python操作es,以及curl命令查询es的总结
#coding=utf-8fromdatetime importdatetime
fromelasticsearch importElasticsearch
# 連接elasticsearch,默認是9200es = Elasticsearch()
# 創建索引,索引的名字是my-index,如果已經存在了,就返回個400,# 這個索引可以現在創建,也可以在后面插入數據的時候再臨時創建# es.indices.create(index='my-index', ignore)#es.indices.create(index='my-index', )# 也可以,在插入數據的時候再創建索引"my_index",單個插入12條數據# es.index(index="my-index", doc_type="my-type", id=1, body={"name": "xiaoming", "age": 18})# es.index(index="my-index", doc_type="my-type", id=2, body={"name": "daming", "age": 18})# es.index(index="my-index", doc_type="my-type", id=3, body={"name": "xiaoxue", "age": 19})# es.index(index="my-index", doc_type="my-type", id=4, body={"name": "daxue", "age": 19})# es.index(index="my-index", doc_type="my-type", id=5, body={"name": "xiaojun", "age": 20})# es.index(index="my-index", doc_type="my-type", id=6, body={"name": "dajun", "age": 20})# es.index(index="my-index", doc_type="my-type", id=7, body={"name": "xiaohua", "age": 21})# es.index(index="my-index", doc_type="my-type", id=8, body={"name": "dahua", "age": 21})# es.index(index="my-index", doc_type="my-type", id=9, body={"name": "xiaozhang", "age": 22})# es.index(index="my-index", doc_type="my-type", id=10, body={"name": "dazhang", "age": 22})# es.index(index="my-index", doc_type="my-type", id=11, body={"name": "xiaomei", "age": 23})# es.index(index="my-index", doc_type="my-type", id=12, body={"name": "damei", "age": 23})#根據id刪除數據# es.delete(index="my-index", doc_type="my-type", id=1)# 獲取age=18的所有值match# res = es.search(index="my-index", body={'query': {'match': {'age':18}}})# 獲取所有值match_allres = es.search(index="my-index", body={'query': {'match_all': {}}})
printres
print'*'*100# 查詢數據,兩種get and search# get獲取# res = es.get(index="my-index", doc_type="my-type", id=01)# print'res_001 = ',resprint'*'*100# search獲取# res = es.search(index="test-index", body={"query": {"match_all": {}}})print'*'*100# CURL的操作# ES查詢# curl是利用URL語法在命令行方式下工作的開源文件傳輸工具,使用curl可以簡單實現常見的get/post請求。簡單的認為是可以在命令行下面訪問url的一個工具。# curl# -X 指定http的請求方法 有HEAD GET POST PUT DELETE# -d 指定要傳輸的數據# -H 指定http請求頭信息# elasticsearch rest api遵循的格式為:# curl -X:///# 檢查es版本信息# curl IP:9200# 查看集群是否健康# curl http://IP:9200/_cat/health?v# 查看節點列表# curl http://IP:9200/_cat/nodes?v# 列出所有索引及存儲大小# curl http://IP:9200/_cat/indices?v# 創建索引# 創建索引名為XX,默認會有5個分片,1個索引# curl -XPUT 'IP:9200/XX?pretty'# 常見后查看,列出所有索引及存儲大小# curl http://IP:9200/_cat/indices?v# 示例 es.index(index="my-index", doc_type="my-type", id=1, body={"name": "xiaoming", "age": 18})數據格式如此所示,由以上文中python代碼插入的數據# 01、顯示es的版本等信息# curl -XGET http://localhost:9200# 02、curl命令查詢某個索引庫index下的所有數據(在url后面加上一個pretty則會對返回結果進行格式化)# curl -XGET http://localhost:9200/索引/_search?pretty# curl -XGET http://localhost:9200/my-index/_search?pretty# 03、curl命令查詢某個type下的所有數據(在url后面加上一個pretty則會對返回結果進行格式化)# curl -XGET http://localhost:9200/索引/類型/_search?pretty# curl -XGET http://localhost:9200/my-index/my-type/_search?pretty## 04、根據id查詢具體的一條記錄:# curl -XGET http://localhost:9200/my-index/my-type/1?pretty# 輸出# {# "_index" : "my-index",# "_type" : "my-type",# "_id" : "1",# "_version" : 2,# "found" : true,# "_source" : {# "age" : 18,# "name" : "xiaoming"# }# }# 05、查詢一條索引文檔中的具體的字段:# curl -XGET http://localhost:9200/my-index/my-type/1?_source=name# 輸出# {"_index":"my-index","_type":"my-type","_id":"1","_version":2,"found":true,"_source":{"name":"xiaoming"}}# 06、如果要查詢多個字段,使用","進行隔開# curl -XGET http://localhost:9200/my-index/my-type/1?_source=name,age# 輸出# {"_index":"my-index","_type":"my-type","_id":"1","_version":2,"found":true,"_source":{"name":"xiaoming","age":18}}# 07、獲取source所有數據# curl -XGET http://localhost:9200/my-index/my-type/1?_source# 輸出# {"_index":"my-index","_type":"my-type","_id":"1","_version":2,"found":true,"_source":{"name":"xiaoming","age":18}}# 08、根據條件進行查詢name是xiaoming的# curl -XGET http://localhost:9200/my-index/my-type/_search?q=name:xiaoming# 輸出# {"took":14,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my-index","_type":"my-type","_id":"1","_score":0.2876821,"_source":{"age":18,"name":"xiaoming"}}]}}# 09、根據條件進行查詢name是xiaoming和name是xiaoxue的,xiaoming和xiaoxue之間用","隔開# curl -XGET http://localhost:9200/my-index/my-type/_search?q=name:xiaoming, xiaoxue# 輸出# {"took":134,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":0.9808292,"hits":[{"_index":"my-index","_type":"my-type","_id":"2","_score":0.9808292,"_source":{"age":19,"name":"xiaoxue"}},{"_index":"my-index","_type":"my-type","_id":"1","_score":0.2876821,"_source":{"age":18,"name":"xiaoming"}}]}}# ES更新########################################### 報406錯誤,未解決######################################################## ES使用PUT或者POST對文檔進行更新,如果指定ID的文檔已經存在,則執行更新操作# 注意:執行更新操作的時候,ES首先將舊的文檔標記為刪除狀態,然后添加新的文檔,舊的文檔不會立即消失,但是你也無法訪問,ES會繼續添加更多數據的時候在后臺清理已經標記為刪除狀態的文檔。# 局部更新# 可以添加新字段或者更新已經存在字段(必須使用POST)# curl -XPOST http://localhost:9200/my-index/my-type/1/_update -d '{"doc":{"name": "xuexue", "age": "11"}}'########################################### 報406錯誤,未解決######################################################## 10、使用文件的方式# curl - XPOST / PUT http: // master:9200 / index / type / _bulk - -data - binary @ path# 比如# curl - XPOST 'http://master:9200/bank/account/_bulk --data-binary @/home/uplooking/Documents/accounts.json
總結
以上是生活随笔為你收集整理的es python search 返回_Elasticsearch - python操作es,以及curl命令查询es的总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手绘黑板粉笔主题教学课件PPT模板
- 下一篇: 南方人,在北京,夏秋冬