代码语言:javascript复制
1.普通查询(全文搜索)
查询name=Smith的文档数据
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"name" : "smith"
}
}
}
备注:
返回结果当中存在max_score和每个记录当中的_score表示最大相关性分数和该记录的相关性分数
2.组合查询
查询name=smith并且年龄大于30岁
GET /megacorp/employee/_search?pretty
{
"query" : {
"bool": {
"must": {
"match" : {
"name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
3.短语搜索
查询about字段当中包含rock和climbing并且rock climbing相连
GET /megacorp/employee/_search?pretty
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
4.高亮搜索
GET /megacorp/employee/_search?pretty
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
5.聚合查询-分析
按照interests字段进行聚合
GET /megacorp/employee/_search?pretty
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
报错:illegal_argument_exception
原因:Fielddata is disabled on text fields by default
解决办法:
PUT /megacorp/_mapping/employee
{
"employee": {
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}
}
备注:
interests是要聚合的字段名
查询结果当中all_interests是分析结果
6.分级汇总-分析
按照interests字段进行聚合,然后计算age平均年龄
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
备注:
avg_age是聚合结果当中新增字段