測試環境 ElasticSearch7.7.0 python 3.8
1. 基本查詢數據
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"match_all": {
}
}
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查詢索引爲movies type爲movie的所有數據
term 和terms的用法
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"term": {
"title": "Kill"
}
}
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查詢查詢索引爲movies type爲movie的 且title等於kill的所有數據
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"terms": {
"title":
[
"Kill", "Lawrence of Arabia"
]
}
}
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查詢查詢索引爲movies type爲movie的 且title等於kill或title等於Lawrence of Arabia的所有數據
match與multi_match
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"match": {
"title": "kill"
}
}
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查詢查詢索引爲movies type爲movie的 且title包含kill的所有數據
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"multi_match": {
"query": "kill",
"fields": ["title", "genres"]
}
}
}
res = es.search(index='movies', doc_type='movie', body=query)
print(res)
#查詢查詢索引爲movies type爲movie的 條件是在title和genres中查詢包含kill的所有數據
ids
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"ids": {
"type": "movie",
"values": [
"1", "2"
]
}
}
}
res = es.search(index='movies', body=query)
print(res)
#查詢查詢索引爲movies type爲movie的 id爲1或者2的說有的值
複合查詢
bool有3類查詢關係,must(都滿足),should(其中一個滿足),must_not(都不滿足)
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"bool": {
"must": [{
"match": {
"city": "Brogan"
}
},
{
"match": {
"state": "IL"
}
}
]
}
}
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
#查詢索引bank type是typeName city包含Brogan且state包含IL的所有數據,
#如果把match換成term就是精準查找
切片查詢 from size
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"match_all": {},
},
"from": 2,
"size": 4
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
#from 從什麼地方開始,size查詢幾條記錄 類似於sql中的skip 和limit的用法
範圍查詢 range
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"range": {
"age": {
"gte": 20,
"lte": 25
}
},
}
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
# 查詢年齡在20-25之間的所有數據
根據前綴查詢
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"prefix": {
"title": "T"
}
}
}
res = es.search(index='movies', doc_type="movie", body=query)
print(res)
查詢title 前綴是T開頭的說有數據
排序
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"query": {
"match_all": {}
},
"sort": {
"age": { # 根據age字段升序排序
"order": "asc" # asc升序,desc降序
}
}
}
res = es.search(index='bank', doc_type="typeName", body=query)
print(res)
按照年齡升序排列
響應過濾 和mongo的查詢之後顯示部分字段類似
# 只獲取自己想要的字段
from elasticsearch import Elasticsearch
es = Elasticsearch()
query=["hits.hits._id"]
es.search(index="bank",doc_type="typeName",filter_path=query)
# 獲取所有數據
query=["hits.hits._*"]
es.search(index="bank",doc_type="typeName",filter_path=query)
查詢匹配數量
es.count(index="bank",doc_type="typeName")