pyhton+ES6.0数据查询(二)

来源:互联网 发布:东方财富指标源码 编辑:程序博客网 时间:2024/06/14 06:03

1、查询所有的信息

# 查询所有数据数据中的前5条body = {    "query": {        'match_all': {}    },    # "size": 50,#不填写的话,默认为10}res = es.search(index='awlogs', doc_type='location_as_domain_log', body=body)soucelist= res['hits']['hits']print "the lens of soucelist:    ",len(soucelist)for each in soucelist:    print each["_source"]the lens of soucelist:     10

2、term与terms的查询

1、term查询

#查询testip='123.56.11.75'的所有数据body = {    "query": {        'term': {            "testip":"123.56.11.75",        },    },}res = es.search(index='awlogs', doc_type='location_as_domain_log', body=body)print res

2、terms查询

#查询testip='123.56.11.75'或者testip='8.8.8.8'的所有数据body = {    "query": {        'terms': {            "testip":["123.56.11.75","8.8.8.8"]        },    },}res = es.search(index='awlogs', doc_type='location_as_domain_log', body=body)

3、match与multi_match查询

样例数据如下:

这里写图片描述

1、match

查询name字段包含郑州关键字的所有信息。

body = {    "query": {        'match': {            "name": "郑州"        },    },}res = es.search(index='awlogs', doc_type='location_as_domain_log', body=body)

查询结果如下:

{    "took": 9,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 1,        "max_score": 0.5753642,        "hits": [            {                "_index": "test_index",                "_type": "test_doc",                "_id": "1",                "_score": 0.5753642,                "_source": {                    "name": "郑州埃文",                    "addr": "郑州市航航路",                    "com_id": "001"                }            }        ]    }}

2、multi_match

查询name或者addr字段中包含郑州的所有数据

body = {    "query": {        'multi_match': {            "query": "郑州",            "fields": ["name", "addr"]        },    },}

查询结果如下:

{    "took": 9,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 2,        "max_score": 0.5753642,        "hits": [            {                "_index": "test_index",                "_type": "test_doc",                "_id": "2",                "_score": 0.5753642,                "_source": {                    "name": "武汉埃文",                    "addr": "郑州市航海路",                    "com_id": "002"                }            },            {                "_index": "test_index",                "_type": "test_doc",                "_id": "1",                "_score": 0.5753642,                "_source": {                    "name": "郑州埃文",                    "addr": "郑州市航航路",                    "com_id": "001"                }            }        ]    }}