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

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"
                }
            }
        ]
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值