搜索所有數據 es.search(index="my_index",doc_type="test_type") # 或者 body = { "query":{ "match_all":{} } } es.search(index="my_index",doc_type="test_type",body=body) term與terms # term body = { "query":{ "term":{"name":"python"} } } # 查詢name="python"的所有數據 es.search(index="my_index",doc_type="test_type",body=body) # terms body = { "query":{ "terms":{"name":["python","android"]} } } # 搜索出name="python"或name="android"的所有數據 es.search(index="my_index",doc_type="test_type",body=body) match與multi_match # match:匹配name包含python關鍵字的數據 body = { "query":{ "match":{"name":"python"} } } # 查詢name包含python關鍵字的數據 es.search(index="my_index",doc_type="test_type",body=body) # multi_match:在name和addr里匹配包含深圳關鍵字的數據 body = { "query":{ "multi_match":{ "query":"深圳", "fields":["name","addr"] } } } # 查詢name和addr包含"深圳"關鍵字的數據 es.search(index="my_index",doc_type="test_type",body=body) ids body = { "query":{ "ids":{ "type":"test_type", "values":["1","2"] } } } # 搜索出id為1或2d的所有數據 es.search(index="my_index",doc_type="test_type",body=body) 復合查詢bool bool有3類查詢關系,must(都滿足),should(其中一個滿足),must_not(都不滿足) body = { "query":{ "bool":{ "must":[{"term":{"name":"python"}},{"term":{"age":18}}] } } } # 獲取name="python"並且age=18的所有數據 es.search(index="my_index",doc_type="test_type",body=body) 切片式查詢 body = { "query":{ "match_all":{}
} "from":2 # 從第二條數據開始 "size":4 # 獲取4條數據 } # 從第2條數據開始,獲取4條數據 es.search(index="my_index",doc_type="test_type",body=body) 范圍查詢 body = { "query":{ "range":{"age":{"gte":18, # >=18 "lte":30 # <=30}} } } # 查詢18<=age<=30的所有數據 es.search(index="my_index",doc_type="test_type",body=body) 前綴查詢 body = { "query":{ "prefix":{"name":"p"} } } # 查詢前綴為"趙"的所有數據 es.search(index="my_index",doc_type="test_type",body=body) 通配符查詢 body = { "query":{ "wildcard":{"name":"*id"} } } # 查詢name以id為后綴的所有數據 es.search(index="my_index",doc_type="test_type",body=body) 排序 body = { "query":{ "match_all":{} }, "sort":{ "age":{ # 根據age字段升序排序 "order":"asc" # asc升序,desc降序 } } } filter_path 響應過濾 # 只需要獲取_id數據,多個條件用逗號隔開 es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._id"]) # 獲取所有數據 es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._*"]) count 執行查詢並獲取該查詢的匹配數 # 獲取數據量 es.count(index="my_index",doc_type="test_type") 度量類聚合 獲取最小值 body = { "query":{ "match_all":{} }, "aggs":{ # 聚合查詢 "min_age":{ # 最小值的key "min":{ # 最小 "field":"age" # 查詢"age"的最小值 } } } } # 搜索所有數據,並獲取age最小的值 es.search(index="my_index",doc_type="test_type",body=body) 獲取最大值 body = { "query":{"match_all":{}}, "aggs":{ # 聚合查詢 "max_age":{ # 最大值的key "max":{ # 最大 "field":"age" # 查詢"age"的最大值 } } } } # 搜索所有數據,並獲取age最大的值 es.search(index="my_index",doc_type="test_type",body=body) 獲取和 body = { "query":{"match_all":{}}, "aggs":{ # 聚合查詢 "sum_age":{ # 和的key "sum":{ # 和 "field":"age" # 獲取所有age的和 } } } } # 搜索所有數據,並獲取所有age的和 es.search(index="my_index",doc_type="test_type",body=body) 獲取平均值 body = { "query":{"match_all":{}}, "aggs":{ # 聚合查詢 "avg_age":{ # 平均值的key "sum":{ # 平均值"field":"age" # 獲取所有age的平均值} } } } # 搜索所有數據,獲取所有age的平均值 es.search(index="my_index",doc_type="test_type",body=body)