使用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']}}]}}