python es 基本查詢
pip install elasticsearch
index = 'my_test_index' # 自定義index名稱
本文涉及到 條數size, 匹配條件 match, exists, 範圍 range, in, 排序 sort,返回結果過濾 includes, 求和 sum, 統計 count
1. 基本查詢體結構
查詢所有, 默認返回10條數據
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"query": {"match_all": {}}
}
search_result = es.search(index=index, body=body)
2. 指定返回的條數(size)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"query": {"match_all": {}}
}
search_result = es.search(index=index, body=body, size=100)
3.指定匹配條件 (match)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"query": {
"bool": {
"must": [
{"match": {"id": 11}}, # 匹配條件
{"match": {"is_deleted": 0}},
{"exists": {"field": "age"}} # age字段的值不爲空
]
},
},
}
search_result = es.search(index=index, body=body, size=10000)
4. 查詢範圍控制 (range)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"query": {
"bool": {
"must": [
{"match": {"is_deleted": 0}},
],
"filter": [
{"range": {"creation_date": {"gte": starttime, "lte": lasttime}}} # 範圍查詢
]
},
}
}
search_result = es.search(index=index, body=body, size=10000)
5.字段的值爲指定某幾個值(in)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"query": {
"bool": {
"must": [
{"match": {"is_deleted": 0}},
{"exists": {"field": "age"}},
],
"filter": [
{"terms": {"age": [18, 19]}}, # age值爲18或19
]
},
},
}
search_result = es.search(index=index, body=body, size=10000)
6.排序(sort)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"query": {
"bool": {
"must": [
{"match": {"project_id": 11}}
]
},
},
"sort": [{"creation_date": {"order": "desc"}}] # 排序,按照creation_date倒敘
}
search_result = es.search(index=self.agile_issue_log_index, body=body, size=10000)
7.查詢返回結果中字段過濾(includes)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"_source": {
"includes": ["id", "name"], # 設置只返回指定字段
"excludes": []
},
"query": {"match_all": {}}
}
search_result = es.search(index=index, body=body, size=10000)
8.求和(sum)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
sum_filed = "sum_ages" # 自定義求和後的字段名
body = {
"query": {"match_all":{}},
"aggs": {
sum_filed: {"sum": {"field": "age"}}, # 對age值求和
},
}
search_result = es.search(index=index, body=body)
sum_ages = search_result["aggregations"][sum_filed]["value"]
9.統計記錄條目數(count)
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"query": {"match_all":{}},
}
search_result = es.count(index=index, body=body)
結合1-7所有的查詢體結構如下:
from elasticsearch.client import Elasticsearch
es = Elasticsearch('127.0.0.1:90201')
body = {
"_source": {
"includes": ["id", "name"],
"excludes": []
},
"query": {
"bool": {
"must": [
{"match": {"is_deleted": 0}},
{"exists": {"field": "age"}},
],
"filter": [
{"terms": {"age": [18, 19]}},
{"range": {"creation_date": {"gte": starttime, "lte": lasttime}}}
]
},
},
"sort": [{"creation_date": {"order": "desc"}}]
}
search_result = es.search(index=self.agile_issue_log_index, body=body, size=10000)