1,对那些有唱歌兴趣的用户按年龄进行分组,并求出每个组的年龄平均值,并按平均年龄的降序进行排序
GET /megacorp/employee
{ "size" : 0, "query" : { "match":{ "interests" :"changge" } }, "aggs" : { "age_of_group":{ "terms":{ "field":"age", "order":{ "age_of_avg":"desc" } }, "aggs":{ "age_of_avg":{ "avg":{ "field":"age" } } } } } }
2,term 查询和terms查询
term query 会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword,numeric,date的查询
GET /megacorp/employee
{ "query" : { "term":{ "interests" :"changge" } } }
3,terms 查询某个字段含有多个关键字的文档
GET /megacorp/employee
{ "query" : { "terms":{ "interests" :["changge","hejiu","sport"] } } }
注意,分页查询是从0开始的,用的是from和size
match 查询:先进行分词,再查询
GET /megacorp/employee
{
"query" : { "match":{ "name" :"changge" } } }
返回分数为1,则为精确查询
multi_match可以匹配多个字段
GET /megacorp/employee
{ "query" : { "multi_match":{ "query" :"changge", "field" :["changge","sport"] } } }
短语匹配查询,需要完全匹配这个短语
GET /megacorp/employee { "query" : { "match_phrase":{ "interests" :["changge,sport"] } } }
排序,sort节点和query节点平级
GET /megacorp/employee { "query" : { "match_all":{} }, "sort" : [ { "age":{ "order":"desc" } } ] }
范围查询
GET /megacorp/employee { "query" : { "range":{ "birthday":{ "from":"1990-10-10", "to":"2020-01-12" } } } } 是否包含边界值,包含下界不包含上届 { "query" : { "range":{ "age":{ "from":20, "to":26, "include_lower":true, "include_upper":false } } } }
wildcard 查询,是通配符查询,*表示0个或多个,? 表示1个字符
GET /megacorp/employee { "query" : { "wildcard":{ "name":"zhao*" } } } { "query" : { "wildcard":{ "name":"li?a" } } }
fuzzy 实现模糊查询,change 也能被找出来
GET /megacorp/employee { "query" : { "fuzzy":{ "interests":"chonge" } } }
高亮显示
GET /megacorp/employee { "query" : { "match":{ "interests":"change" } }, "highlight" : { "fields":{ "interests":{} } } }