python 配合 es 查詢數據

1.python腳本python

[root@do1cloud03 ~]# cat  python-es.py
#!/usr/bin/env python3

from elasticsearch import Elasticsearch
from json import dumps

es = Elasticsearch(
    ['127.0.0.1:9200'],
    # 認證信息
    # http_auth=('elastic', 'elastic')
)

#print(es.ping())
#print(es.cat.indices())

response = es.search(
    index="logstash-2019.10.17", # 索引名
    body={             # 請求體
      "query": {       # 關鍵字,把查詢語句給 query
          "bool": {    # 關鍵字,表示使用 filter 查詢,沒有匹配度
                "must": [      # 表示裏面的條件必須匹配,多個匹配元素能夠放在列表裏
                    {
                        "match": {  # 關鍵字,表示須要匹配的元素
                            #"message": '/mgr/minutes/getMeetingConfigByType.mvc'
                            "message": "/mgr/getRootTree.mvc"
                        }
                    },
                    {

                        "match": {
                             "profile": "prod"
                        }
                    },
                    {
                        "match": {  # 關鍵字,表示須要匹配的元素
                            #"message": '/mgr/minutes/getMeetingConfigByType.mvc'
                            "message": "耗時"
                        }
                    }
                      ],
            }
        }
  },
)
#print(response)
#for v in response.values():
#    print(v)
res_lst = response["hits"]["hits"]
for i in res_lst:
    print(dumps(i))

 

用一個例子來演示會更加清晰
相關文章
相關標籤/搜索