路漫漫其修远兮
吾将上下而求索

python学习:py操作es

python操作es
https://pypi.org/project/elasticsearch/5.4.0/#files

更新操作有问题
https://stackoverflow.com/questions/30598152/how-to-update-a-document-using-elasticsearch-py
要使用下面的
data = {
    'title': '美国留给伊拉克的是个烂摊子吗',
    'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm',
    'date': '2011-12-17'
}
result = es.index(index='news', doc_type='politics', id=3, body={'doc':data})


安装分词器注意版本如果不同,需要修改
vim plugin-descriptor.properties
elasticsearch.version=5.6.16

下面有现成的脚本供使用
10.16.22.100 /home/workspace


线上es地址:http://10.16.15.17:9200/


python操作es:https://cuiqingcai.com/6214.html
es权威指南:https://es.xiaoleilu.com/010_Intro/00_README.html

下面是nginx访问原始日志,截取三个域名的日志进行测试。条数一共30条。不多,便于精确测试
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

result = es.indices.create(index='ngx-log', ignore=400)
print(result)

datas = [
    {"time": "2019-03-28T00:00:13+08:00", "remote_addr": "10.216.5.222", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "152", "geoip_region_name": "-", "request_time": "0.222",
     "upstream_response_time": "0.223", "upstream_addr": "10.16.5.197:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "es_online_15_28", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:14+08:00", "remote_addr": "10.16.45.202", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "152", "geoip_region_name": "-", "request_time": "0.019",
     "upstream_response_time": "0.018", "upstream_addr": "10.16.5.179:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "kvm_server_45_15", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:14+08:00", "remote_addr": "10.16.8.63",
     "request": "/api/bankCard/getUserBankCardListByUid", "status": "200", "request_method": "POST", "size": "50",
     "geoip_region_name": "-", "request_time": "0.004", "upstream_response_time": "0.005",
     "upstream_addr": "10.16.5.197:8081", "http_forward": "-", "http_referer": "-", "domain": "zwhite.andblog.com",
     "hostname": "es_online_15_28", "agrs": "random=1553702414948046c83c9a058433cbf7cc0a5399c92b0",
     "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_112)"},
    {"time": "2019-03-28T00:00:14+08:00", "remote_addr": "10.16.8.63",
     "request": "/api/bankCard/getUserBankCardListByUid", "status": "200", "request_method": "POST", "size": "50",
     "geoip_region_name": "-", "request_time": "0.007", "upstream_response_time": "0.006",
     "upstream_addr": "10.16.5.179:8081", "http_forward": "-", "http_referer": "-", "domain": "zwhite.andblog.com",
     "hostname": "es_online_15_28", "agrs": "random=15537024149481e7193a90a4943d7b2d66dec80c12003",
     "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_112)"},
    {"time": "2019-03-28T00:00:14+08:00", "remote_addr": "10.216.5.221", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "152", "geoip_region_name": "-", "request_time": "0.025",
     "upstream_response_time": "0.024", "upstream_addr": "10.16.5.197:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "es_online_15_28", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:14+08:00", "remote_addr": "10.216.5.221", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "152", "geoip_region_name": "-", "request_time": "0.027",
     "upstream_response_time": "0.027", "upstream_addr": "10.16.5.179:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "es_online_15_28", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:14+08:00", "remote_addr": "10.216.5.221", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "152", "geoip_region_name": "-", "request_time": "0.028",
     "upstream_response_time": "0.028", "upstream_addr": "10.16.5.197:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "es_online_15_28", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:14+08:00", "remote_addr": "10.216.5.221", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "172", "geoip_region_name": "-", "request_time": "0.024",
     "upstream_response_time": "0.023", "upstream_addr": "10.16.5.179:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "es_online_15_28", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:15+08:00", "remote_addr": "10.16.45.202", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "152", "geoip_region_name": "-", "request_time": "0.014",
     "upstream_response_time": "0.014", "upstream_addr": "10.16.5.197:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "kvm_server_45_15", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:15+08:00", "remote_addr": "10.16.45.203", "request": "/api/pay/toPayShowCheck",
     "status": "200", "request_method": "POST", "size": "152", "geoip_region_name": "-", "request_time": "0.008",
     "upstream_response_time": "0.008", "upstream_addr": "10.16.5.179:8081", "http_forward": "-", "http_referer": "-",
     "domain": "zwhite.andblog.com", "hostname": "kvm_server_45_15", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-", "http_user_agent": "Java/1.8.0_112"},

    #ams.andblog.com
    {"time": "2019-03-28T00:00:07+08:00", "remote_addr": "35.199.181.138",
     "request": "/AMS/hire/hireContract!detailHireContract.action", "status": "200", "request_method": "GET",
     "size": "3070", "geoip_region_name": "Michigan", "request_time": "0.113", "upstream_response_time": "0.045",
     "upstream_addr": "10.16.25.7:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/rent/rentContract!listRentContract.action", "domain": "ams.andblog.com",
     "hostname": "kvm_online_35_15", "agrs": "token=NjAwMDQxNDg=", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
    {"time": "2019-03-28T00:00:07+08:00", "remote_addr": "35.199.181.138",
     "request": "/AMS/hire/hireContract!detailHireContract.action", "status": "302", "request_method": "GET",
     "size": "0", "geoip_region_name": "Michigan", "request_time": "0.018", "upstream_response_time": "0.018",
     "upstream_addr": "10.16.25.70:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/rent/rentContract!listRentContract.action", "domain": "ams.andblog.com",
     "hostname": "kvm_online_35_15", "agrs": "id=60004148", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
    {"time": "2019-03-28T00:00:07+08:00", "remote_addr": "35.199.181.138",
     "request": "/AMS/hire/hireContract!detailHirePeople.action", "status": "302", "request_method": "GET", "size": "0",
     "geoip_region_name": "Michigan", "request_time": "0.277", "upstream_response_time": "0.020",
     "upstream_addr": "10.16.25.221:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/rent/rentContract!listRentContract.action", "domain": "ams.andblog.com",
     "hostname": "kvm_online_35_15", "agrs": "id=60004189", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
    {"time": "2019-03-28T00:00:08+08:00", "remote_addr": "111.197.235.41",
     "request": "/AMS/rent/rentContract!listRentContract.action", "status": "200", "request_method": "GET",
     "size": "9599", "geoip_region_name": "Beijing", "request_time": "0.416", "upstream_response_time": "0.416",
     "upstream_addr": "10.16.25.70:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/security/security!getResources.action?parentIds=23,28,36,43,45,50,54,58,65,73,80,83,89,2,5,11,17,883,926,2000,953,60000220,562,571,582,620,690,746,863,871,933,672,790,60000440,60000642",
     "domain": "ams.andblog.com", "hostname": "es_online_15_29", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (Linux; U; Android 7.1.1; zh-CN; OPPO R11 Build/NMF26X) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.3.6.1016 Mobile Safari/537.36"},
    {"time": "2019-03-28T00:00:08+08:00", "remote_addr": "114.254.56.105",
     "request": "/AMS/contractDetail/contractDetail!searchHouseVacant.action", "status": "200", "request_method": "GET",
     "size": "8537", "geoip_region_name": "Beijing", "request_time": "0.071", "upstream_response_time": "0.071",
     "upstream_addr": "10.16.25.70:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/security/security!getResources.action?parentIds=23,28,36,43,45,50,54,58,65,73,80,83,89,2,5,11,17,883,926,2000,953,60000220,562,571,582,620,690,746,863,871,933,672,790,60000440,60000642",
     "domain": "ams.andblog.com", "hostname": "kvm_online_15_16", "agrs": "-", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"},
    {"time": "2019-03-28T00:00:08+08:00", "remote_addr": "35.199.181.138",
     "request": "/AMS/hire/hireContract!detailHirePeople.action", "status": "200", "request_method": "GET",
     "size": "1903", "geoip_region_name": "Michigan", "request_time": "0.416", "upstream_response_time": "0.043",
     "upstream_addr": "10.16.25.7:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/rent/rentContract!listRentContract.action", "domain": "ams.andblog.com",
     "hostname": "kvm_online_35_15", "agrs": "token=NjAwMDQxODk=", "http_protocol": "HTTP/1.1",
     "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
    {"time": "2019-03-28T00:00:08+08:00", "remote_addr": "35.199.181.138",
     "request": "/AMS/hire/hireContract!detailHouseManage.action", "status": "302", "request_method": "GET",
     "size": "0", "geoip_region_name": "Michigan", "request_time": "0.459", "upstream_response_time": "0.011",
     "upstream_addr": "10.16.25.7:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/rent/rentContract!listRentContract.action", "domain": "ams.andblog.com",
     "hostname": "kvm_online_35_15", "agrs": "id=60003934", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
    {"time": "2019-03-28T00:00:08+08:00", "remote_addr": "35.199.181.138",
     "request": "/AMS/rent/rentContract!detailRentContract.action", "status": "200", "request_method": "GET",
     "size": "4463", "geoip_region_name": "Michigan", "request_time": "0.493", "upstream_response_time": "0.214",
     "upstream_addr": "10.16.25.70:8081", "http_forward": "-",
     "http_referer": "http://ams.andblog.com/AMS/rent/rentContract!listRentContract.action", "domain": "ams.andblog.com",
     "hostname": "kvm_online_35_15", "agrs": "id=201486740", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},

    #account.andblog.com
    {"time": "2019-03-28T00:00:04+08:00", "remote_addr": "10.16.15.139",
     "request": "/ZRAccount/account/rfrozenAccount.do", "status": "200", "request_method": "POST", "size": "493",
     "geoip_region_name": "-", "request_time": "0.035", "upstream_response_time": "0.036",
     "upstream_addr": "10.16.35.98:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:04+08:00", "remote_addr": "10.16.15.139",
     "request": "/ZRAccount/account/rfrozenAccount.do", "status": "200", "request_method": "POST", "size": "493",
     "geoip_region_name": "-", "request_time": "0.048", "upstream_response_time": "0.049",
     "upstream_addr": "10.16.35.97:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:04+08:00", "remote_addr": "10.16.15.139",
     "request": "/ZRAccount/account/rfrozenAccount.do", "status": "200", "request_method": "POST", "size": "493",
     "geoip_region_name": "-", "request_time": "0.050", "upstream_response_time": "0.049",
     "upstream_addr": "10.16.35.98:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:04+08:00", "remote_addr": "10.16.15.139",
     "request": "/ZRAccount/account/rfrozenAccount.do", "status": "200", "request_method": "POST", "size": "493",
     "geoip_region_name": "-", "request_time": "0.052", "upstream_response_time": "0.052",
     "upstream_addr": "10.16.35.97:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:04+08:00", "remote_addr": "10.16.15.139",
     "request": "/ZRAccount/account/rfrozenAccount.do", "status": "200", "request_method": "POST", "size": "493",
     "geoip_region_name": "-", "request_time": "0.055", "upstream_response_time": "0.055",
     "upstream_addr": "10.16.35.97:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:00:04+08:00", "remote_addr": "10.16.15.139",
     "request": "/ZRAccount/account/rfrozenAccount.do", "status": "200", "request_method": "POST", "size": "493",
     "geoip_region_name": "-", "request_time": "0.056", "upstream_response_time": "0.055",
     "upstream_addr": "10.16.35.97:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.8.0_112"},
    {"time": "2019-03-28T00:11:34+08:00", "remote_addr": "10.16.35.46",
     "request": "/ZRAccount/account/frozenAccount.do", "status": "200", "request_method": "POST", "size": "494",
     "geoip_region_name": "-", "request_time": "0.020", "upstream_response_time": "0.019",
     "upstream_addr": "10.16.35.98:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.7.0_71"},
    {"time": "2019-03-28T00:11:36+08:00", "remote_addr": "10.16.45.203",
     "request": "/ZRAccount/account/lineWithdrawMoney/getUidCityDetails.do", "status": "200", "request_method": "POST",
     "size": "29", "geoip_region_name": "-", "request_time": "0.007", "upstream_response_time": "0.007",
     "upstream_addr": "10.16.35.97:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "random=1553703096762ead22d58dcb74bcba8ee0c99295e04c9",
     "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Apache-HttpClient/4.4.1 (Java/1.8.0_112)"},
    {"time": "2019-03-28T00:11:36+08:00", "remote_addr": "10.16.8.196",
     "request": "/ZRAccount/account/withdrawMoney/getAllDetails.do", "status": "200", "request_method": "GET",
     "size": "1146", "geoip_region_name": "-", "request_time": "0.009", "upstream_response_time": "0.009",
     "upstream_addr": "10.16.35.97:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10",
     "agrs": "encryption=kZtHw%2BEMOX3wTP%2BcIPbXDvoGODpcSkNy4653H3N0NNx1d9p95pibtMYvWzl0E8GsrhqI%2Bd1CrviCHngL5Ay39%2BDRO%2FEVLkJmnsTnuvNnFbI%3D",
     "http_protocol": "HTTP/1.1", "upstream_cache_status": "-", "http_user_agent": "Yii2-Curl-Agent"},
    {"time": "2019-03-28T00:11:36+08:00", "remote_addr": "10.16.8.197",
     "request": "/ZRAccount/account/bankAccount/getUserBankCardAccount.do", "status": "200", "request_method": "GET",
     "size": "358", "geoip_region_name": "-", "request_time": "0.009", "upstream_response_time": "0.008",
     "upstream_addr": "10.16.35.98:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10",
     "agrs": "encryption=kZtHw%2BEMOX3wTP%2BcIPbXDvoGODpcSkNy4653H3N0NNx1d9p95pibtMYvWzl0E8GsrhqI%2Bd1CrviCHngL5Ay39%2BDRO%2FEVLkJmnsTnuvNnFbI%3D",
     "http_protocol": "HTTP/1.1", "upstream_cache_status": "-", "http_user_agent": "Yii2-Curl-Agent"},
    {"time": "2019-03-28T00:11:37+08:00", "remote_addr": "10.16.35.46",
     "request": "/ZRAccount/account/frozenAccount.do", "status": "200", "request_method": "POST", "size": "494",
     "geoip_region_name": "-", "request_time": "0.016", "upstream_response_time": "0.015",
     "upstream_addr": "10.16.35.97:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "-", "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Java/1.7.0_71"},
    {"time": "2019-03-28T00:11:37+08:00", "remote_addr": "10.16.45.202",
     "request": "/ZRAccount/account/lineWithdrawMoney/getUidCityDetails.do", "status": "200", "request_method": "POST",
     "size": "29", "geoip_region_name": "-", "request_time": "0.010", "upstream_response_time": "0.010",
     "upstream_addr": "10.16.35.98:8081", "http_forward": "-", "http_referer": "-", "domain": "account.andblog.com",
     "hostname": "kvm_online_35_10", "agrs": "random=1553703097757c6931ebafa3646b9a97006268b6b9787",
     "http_protocol": "HTTP/1.1", "upstream_cache_status": "-",
     "http_user_agent": "Apache-HttpClient/4.4.1 (Java/1.8.0_112)"}
]


for data in datas:
    es.index(index='ngx-log', doc_type='log', body=data)

####################
查询所有的记录,这里要指定size,否则只显示10条记录

from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

dsl = {
    'query': {
        'match_all': {

        }
    },
    "size": 500
}

result = es.search(index='ngx-log', doc_type='log', body=dsl)
print(json.dumps(result, indent=2, ensure_ascii=False))


下面就是看具体的es查询语法了



12.1 查询准确值  es将字段的每个字符都分割了,要告诉es这个为一个整体,需要将已有数据删除,索引也删除,执行下面,会自动创建索引,并将http_user_agent字段里字符串作为整体
put http://192.168.170.20:9200/ngx-log/
{
    "mappings" : {
        "log" : {
            "properties" : {
                "http_user_agent" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                }
            }
        }
    }

}

下面为精确查找,字段的值多一个字符或者少一个字符都不会查找到,有的字段没有执行上面的设置,也可以查找到,应该不保险
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

dsl = {
    "query": {
        "term": {
            "http_user_agent": "Java/1.8.0_112"
        }
    },
    "size": 500
}

result = es.search(index='ngx-log', doc_type='log', body=dsl)
print(json.dumps(result, indent=2, ensure_ascii=False))


12.2 为了和教程一样,这里使用教程的例子
put http://192.168.170.20:9200/my_store/
{
    "mappings" : {
        "products" : {
            "properties" : {
                "productID" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                }
            }
        }
    }

}

插入数据
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

datas = [
    { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" },
    { "price" : 20, "productID" : "KDKE-B-9947-#kL5" },
    { "price" : 30, "productID" : "JODL-X-1937-#pV7" },
    { "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
]


for data in datas:
    es.index(index='my_store', doc_type='products', body=data)

查询语句:
SELECT product
FROM   products
WHERE  (price = 20 OR productID = "XHDK-A-1293-#fJ3")
  AND  (price != 30)

具体如下
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

dsl = {
    "query": {
       'bool': {
           'should': [
               {'term': {'price': 30}},
               {'term': {'productID': 'XHDK-A-1293-#fJ3'}}
           ],
           'must_not': {
               'term': {'productID': 'QQPX-R-3956-#aD8'}
           }
       }

    },
    "size": 500
}

result = es.search(index='my_store', doc_type='products', body=dsl)
print(json.dumps(result, indent=2, ensure_ascii=False))

查询语句: 
SELECT document
FROM   products
WHERE  productID      = "KDKE-B-9947-#kL5"
  OR (     productID = "JODL-X-1937-#pV7"
       AND price     = 30 )
	   
查询条件如下
"bool" : {
  "should" : [
	{ "term" : {"productID" : "KDKE-B-9947-#kL5"}},
	{ "bool" : {
	  "must" : [
		{ "term" : {"productID" : "JODL-X-1937-#pV7"}},
		{ "term" : {"price" : 30}}
	  ]
	}}
  ]
}


12.3 查询多个准确值
下面的意思是price的值为20或者30,而不是一个范围
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

dsl = {
    "query": {
        'terms':{
            'price': [20,30]
        }
    },
    "size": 500
}

result = es.search(index='my_store', doc_type='products', body=dsl)
print(json.dumps(result, indent=2, ensure_ascii=False))


12.4 包含,而不是相等
不理解

12.5 范围
range 过滤器既能包含也能排除范围,通过下面的选项:
gt: > 大于
lt: < 小于
gte: >= 大于或等于
lte: <= 小于或等于

下面是示例
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

dsl = {
    "query": {
        'range': {
            'price': {
                'gte': 20,
                'lt': 40
            }
        }
    },
    "size": 500
}

result = es.search(index='my_store', doc_type='products', body=dsl)
print(json.dumps(result, indent=2, ensure_ascii=False))


查询日期范围也可以
这里使用ngx-log这个索引来测试
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

dsl = {
    "query": {
        'range': {
           'time': {
               'gte': '2019-03-28T00:00:04+08:00',
               'lte': '2019-03-28T00:00:07+08:00'
           }
        }
    },
    "size": 500
}

result = es.search(index='ngx-log', doc_type='log', body=dsl)
print(json.dumps(result, indent=2, ensure_ascii=False))

即使size字段为字符串,也可以进行计算
from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

from datetime import datetime
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(["192.168.170.20"])

dsl = {
    "query": {
        'range': {
           'request_time': {
               'gte': 0.0,
               'lte': 0.01
           }
        }
    },
    "size": 500
}

result = es.search(index='ngx-log', doc_type='log', body=dsl)
a = json.dumps(result, indent=2, ensure_ascii=False)
# print(a)
for i in result['hits']['hits']:
    b = i['_source']['size']
    print(b)


12.6 处理null

未经允许不得转载:江哥架构师笔记 » python学习:py操作es

分享到:更多 ()

评论 抢沙发

评论前必须登录!