python 操作 ElasticSearch 基础入门

使用python操作ElasticSearch
from elasticsearch import Elasticsearch
#连接ES
es = Elasticsearch([{‘host’:‘10.101.12.19’,‘port’:9200}], timeout=3600)
#若需验证
#es = Elasticsearch([‘10.101.12.19’], http_auth=(‘xiao’, ‘123456’), timeout=3600)

查询

query = {
  "query": {
    "match_all": {}
  }
}
result = es.search(index="megacorp", body=query)
print(result)

结果:

{'took': 1, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 3, 'max_score': 1.0, 'hits': [{'_index': 'megacorp', '_type': 'employee', '_id': '2', '_score': 1.0, '_source': {'first_name': 'Jane', 'last_name': 'Smith', 'age': 32, 'about': 'I like to collect rock albums', 'interests': ['music']}}, {'_index': 'megacorp', '_type': 'employee', '_id': '1', '_score': 1.0, '_source': {'first_name': 'John', 'last_name': 'Smith', 'age': 25, 'about': 'I love to go rock climbing', 'interests': ['sports', 'music']}}, {'_index': 'megacorp', '_type': 'employee', '_id': '3', '_score': 1.0, '_source': {'first_name': 'Douglas', 'last_name': 'Fir', 'age': 35, 'about': 'I like to build cabinets', 'interests': ['forestry']}}]}}

使用DSL语句查询

term 过滤–term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经切词的文本数据类型)

query = {
    "query": {
        "term":{
            'age': 32
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# first_name 可能经过切词了
query = {
    "query": {
        "term":{
            'first_name': 'Jane'
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)

terms 过滤–terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配

query = {
    "query": {
        "terms":{
            'age': [32, 25]
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
#first_name 可能经过切词了
query = {
    "query": {
        "terms":{
            'first_name': ['Jane','John']
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
{'took': 8, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 2, 'max_score': 1.0, 'hits': [{'_index': 'megacorp', '_type': 'employee', '_id': '2', '_score': 1.0, '_source': {'first_name': 'Jane', 'last_name': 'Smith', 'age': 32, 'about': 'I like to collect rock albums', 'interests': ['music']}}, {'_index': 'megacorp', '_type': 'employee', '_id': '1', '_score': 1.0, '_source': {'first_name': 'John', 'last_name': 'Smith', 'age': 25, 'about': 'I love to go rock climbing', 'interests': ['sports', 'music']}}]}}
{'took': 2, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 0, 'max_score': None, 'hits': []}}

range 过滤–按照指定范围查找一批数据
gt : 大于gte : 大于等于lt : 小于lte : 小于等于

     query = {
    "query": {
        "range":{
            'age': {
                "gt":34
            }
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
{'took': 1, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score': 1.0, 'hits': [{'_index': 'megacorp', '_type': 'employee', '_id': '3', '_score': 1.0, '_source': {'first_name': 'Douglas', 'last_name': 'Fir', 'age': 35, 'about': 'I like to build cabinets', 'interests': ['forestry']}}]}}

exists 和 missing 过滤–查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件

query = {
    "query": {
        "exists":   {
            "field":    "first_name"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
{'took': 0, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 3, 'max_score': 1.0, 'hits': [{'_index': 'megacorp', '_type': 'employee', '_id': '2', '_score': 1.0, '_source': {'first_name': 'Jane', 'last_name': 'Smith', 'age': 32, 'about': 'I like to collect rock albums', 'interests': ['music']}}, {'_index': 'megacorp', '_type': 'employee', '_id': '1', '_score': 1.0, '_source': {'first_name': 'John', 'last_name': 'Smith', 'age': 25, 'about': 'I love to go rock climbing', 'interests': ['sports', 'music']}}, {'_index': 'megacorp', '_type': 'employee', '_id': '3', '_score': 1.0, '_source': {'first_name': 'Douglas', 'last_name': 'Fir', 'age': 35, 'about': 'I like to build cabinets', 'interests': ['forestry']}}]}}

bool 过滤–合并多个过滤条件查询结果的布尔逻辑
must :: 多个查询条件的完全匹配,相当于 and;must_not :: 多个查询条件的相反匹配,相当于 not;should :: 至少有一个查询条件匹配, 相当于 or。

query = {
    "query": {
         "bool": {
             "must": {
                 "term": { "_score": 1 },
                 "term": { "age": 32 }
                },
             }
         }
}
result = es.search(index="megacorp", body=query)
print(result)
query = {
    "query": {
         "bool": {
             "must": {
                 "term": { "age": 32 }
                },
             "must_not":{
                 "exists":   {
                    "field":    "name"
                }
             }
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
{'took': 0, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score': 1.0, 'hits': [{'_index': 'megacorp', '_type': 'employee', '_id': '2', '_score': 1.0, '_source': {'first_name': 'Jane', 'last_name': 'Smith', 'age': 32, 'about': 'I like to collect rock albums', 'interests': ['music']}}]}}
{'took': 0, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score': 1.0, 'hits': [{'_index': 'megacorp', '_type': 'employee', '_id': '2', '_score': 1.0, '_source': {'first_name': 'Jane', 'last_name': 'Smith', 'age': 32, 'about': 'I like to collect rock albums', 'interests': ['music']}}]}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值