查询所有数据
1 2 3 4 5 6 7 8 9 | # 搜索所有数据 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # 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
1 2 3 4 5 6 7 8 9 10 11 12 | 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(都不满足)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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) |
切片式查询
1 2 3 4 5 6 7 8 9 | body = { "query" :{ "match_all" :{} } "from" : 2 # 从第二条数据开始 "size" : 4 # 获取4条数据 } # 从第2条数据开始,获取4条数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
范围查询
1 2 3 4 5 6 7 8 9 10 11 12 | body = { "query" :{ "range" :{ "age" :{ "gte" : 18 , # >=18 "lte" : 30 # <=30 } } } } # 查询18<=age<=30的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
前缀查询
1 2 3 4 5 6 7 8 9 | body = { "query" :{ "prefix" :{ "name" : "p" } } } # 查询前缀为"赵"的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
通配符查询
1 2 3 4 5 6 7 8 9 | body = { "query" :{ "wildcard" :{ "name" : "*id" } } } # 查询name以id为后缀的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
排序
1 2 3 4 5 6 7 8 9 10 | body = { "query" :{ "match_all" :{} } "sort" :{ "age" :{ # 根据age字段升序排序 "order" : "asc" # asc升序,desc降序 } } } |
filter_path
响应过滤
1 2 3 4 | # 只需要获取_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
执行查询并获取该查询的匹配数
1 2 | # 获取数据量 es.count(index = "my_index" ,doc_type = "test_type" ) |
度量类聚合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "min_age" :{ # 最小值的key "min" :{ # 最小 "field" : "age" # 查询"age"的最小值 } } } } # 搜索所有数据,并获取age最小的值 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "max_age" :{ # 最大值的key "max" :{ # 最大 "field" : "age" # 查询"age"的最大值 } } } } # 搜索所有数据,并获取age最大的值 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "sum_age" :{ # 和的key "sum" :{ # 和 "field" : "age" # 获取所有age的和 } } } } # 搜索所有数据,并获取所有age的和 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "avg_age" :{ # 平均值的key "sum" :{ # 平均值 "field" : "age" # 获取所有age的平均值 } } } } # 搜索所有数据,获取所有age的平均值 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
更多的搜索用法:
https://elasticsearch-py.readthedocs.io/en/master/api.html