Skip to content

ES中常见的搜索语句

搜索入门

搜索全部

  • 最简单的搜索,使用match_all来表示,例如搜索全部;
GET /bank/_search
{
  "query": { "match_all": {} }
}

image.png

分页搜索

  • 分页搜索,from表示偏移量,从0开始,size表示每页显示的数量;
GET /bank/_search
{
  "query": { "match_all": {} },
  "from": 0,
  "size": 10
}

image.png

搜索排序

  • 搜索排序,使用sort表示,例如按balance字段降序排列;
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
}

image.png

搜索返回指定字段

  • 搜索并返回指定字段内容,使用_source表示,例如只返回account_numberbalance两个字段内容:
GET /bank/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}

image.png

条件搜索

精确匹配(Term/Terms)

term:精确匹配字段值(不分词),如查找name"python"的文档

{ "query": { "term": { "name": "python" } } }

terms:匹配多个值,如name"python""android"

{ "query": { "terms": { "name": ["python", "android"] } } }

范围查询 Range

查询数字或日期范围,如age在18到30之间

{ 
  "query": { 
    "range": { 
      "age": { "gte": 18, "lte": 30 } 
    } 
  } 
}

全文搜索

Match查询​

对文本字段进行分词搜索

  • 条件搜索,使用match表示匹配条件,例如搜索出account_number20的文档:
GET /bank/_search
{
  "query": {
    "match": {
      "account_number": 20
    }
  }
}

image.png

  • 文本类型字段的条件搜索,例如搜索address字段中包含mill的文档,对比上一条搜索可以发现,对于数值类型match操作使用的是精确匹配,对于文本类型使用的是模糊匹配;
GET /bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "_source": [
    "address",
    "account_number"
  ]
}

image.png

多字段匹配(Multi-match)

在多个字段中搜索同一关键词,如nameaddr包含"深圳"

{ 
  "query": { 
    "multi_match": { 
      "query": "深圳", 
      "fields": ["name", "addr"] 
    } 
  } 
}

短语匹配搜索

  • 短语匹配搜索,使用match_phrase表示,例如搜索address字段中同时包含milllane的文档:
GET /bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill lane"
    }
  }
}

image.png

模糊匹配(Fuzzy/Wildcard)

fuzzy:容忍拼写错误,如"iphon"匹配"iphone"

{ "query": { "fuzzy": { "name": { "value": "iphon", "fuzziness": "AUTO" } } } }

wildcard:通配符匹配,如name"app"开头

{ "query": { "wildcard": { "name": "app*" } } }

组合搜索/复合查询(Bool)

通过逻辑组合多个条件:

  • must:所有条件必须满足(AND逻辑)。

  • should:至少满足一个条件(OR逻辑)。

  • must_not:排除满足条件的文档。

  • filter:类似must但不影响评分,性能更高。

  • 组合搜索,使用bool来进行组合,must表示同时满足,例如搜索address字段中同时包含milllane的文档;

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

image.png

  • 组合搜索,should表示满足其中任意一个,搜索address字段中包含mill或者lane的文档;
GET /bank/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

image.png

  • 组合搜索,must_not表示同时不满足,例如搜索address字段中不包含mill且不包含lane的文档;
GET /bank/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

image.png

  • 组合搜索,组合mustmust_not,例如搜索age字段等于40state字段不包含ID的文档;
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}

image.png

过滤搜索

  • 搜索过滤,使用filter来表示,例如过滤出balance字段在20000~30000的文档;
GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

image.png

搜索聚合

  • 对搜索结果进行聚合,使用aggs来表示,类似于MySql中的group by,例如对state字段进行聚合,统计出相同state的文档数量;
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}

image.png

  • 嵌套聚合,例如对state字段进行聚合,统计出相同state的文档数量,再统计出balance的平均值;
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}

image.png

  • 对聚合搜索的结果进行排序,例如按balance的平均值降序排列;
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword",
        "order": {
          "average_balance": "desc"
        }
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}

image.png

  • 按字段值的范围进行分段聚合,例如分段范围为age字段的[20,30] [30,40] [40,50],之后按gender统计文档个数和balance的平均值;
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

image.png


参考