ES查询语法总结 - 教程

一、全文检索查询

  1. Match Query
    搜索分词后的文本字段(如 text 类型):

    GET /products/_search
    {
      "query": {
        "match": {
          "description": "wireless headphones"
        }
      }
    }
    • 自动对 "wireless headphones" 分词(如拆为 wirelessheadphones),匹配任一词汇的文档。
  2. Multi-match Query
    跨多个字段搜索:

    {
      "query": {
        "multi_match": {
          "query": "apple",
          "fields": ["title", "brand", "description"]
        }
      }
    }

二、精确查询

  1. Term Query
    精确匹配未分词的 keyword 字段:

    {
      "query": {
        "term": {
          "status": {
            "value": "published"  // 精确匹配字段值
          }
        }
      }
    }
  2. Terms Query
    匹配字段中包含任一指定值的文档:

    {
      "query": {
        "terms": {
          "tags": ["electronics", "sale"]  // 匹配含 electronics 或 sale 的文档
        }
      }
    }
  3. Range Query
    数值或日期范围过滤:

    {
      "query": {
        "range": {
          "price": {
            "gte": 100,
            "lte": 1000
          }
        }
      }
    }
  4. Exists Query
    筛选存在某字段的文档:

    {
      "query": {
        "exists": {
          "field": "author"  // 返回包含 author 字段的文档
        }
      }
    }

三、复合查询

  1. Bool Query 组合逻辑条件(AND/OR/NOT):

    {
      "query": {
        "bool": {
          "must": [  // 必须满足
            { "match": { "title": "phone" } },
            { "range": { "price": { "lte": 500 } } }
          ],
          "must_not": [  // 必须不满足
            { "term": { "brand": "A" } }
          ],
          "should": [  // 满足任意一个
            { "term": { "color": "black" } },
            { "term": { "color": "silver" } }
          ],
          "minimum_should_match": 1,  // 至少满足 1 个 should 条件
          "filter": [  // 过滤,不参与评分
            { "term": { "in_stock": true } }
          ]
        }
      }
    }

四、特殊查询

  1. Match_all Query
    匹配所有文档:

    {
      "query": {
        "match_all": {}
      }
    }
  2. Wildcard Query
    通配符匹配(* 匹配任意字符,? 匹配单个字符):

    {
      "query": {
        "wildcard": {
          "sku": "pro-*"  // 匹配 pro-123, pro-abc 等
        }
      }
    }
  3. Prefix Query
    前缀匹配:

    {
      "query": {
        "prefix": {
          "city": "new"  // 匹配 new york, new delhi 等
        }
      }
    }
  4. Fuzzy Query
    容错匹配(允许拼写错误):

    {
      "query": {
        "fuzzy": {
          "text": {
            "value": "quick",
            "fuzziness": "AUTO"  // 自动允许 1-2 个字符的差异
          }
        }
      }
    }

五、其他功能示例

  1. 分页与排序

    {
      "query": { "match_all": {} },
      "from": 10,  // 跳过前 10 条
      "size": 5,   // 返回 5 条结果
      "sort": [
        { "price": { "order": "desc" } }  // 按价格降序排序
      ]
    }
  2. 高亮匹配内容

    {
      "query": {
        "match": { "content": "Elasticsearch" }
      },
      "highlight": {
        "fields": {
          "content": {}  // 高亮 content 字段中的匹配词
        }
      }
    }

六、关键注意事项

  • 默认返回 10 条数据:在未显式设置 size 参数时,Elasticsearch 的 search 查询默认仅返回匹配结果的前 10 条记录。
  • 字段类型term 适用于 keyword 类型,match 适用于 text 类型。
  • 性能:优先用 filter 替代 must 进行精确过滤(如状态、时间范围),减少评分计算。
posted @ 2026-02-02 08:47  gccbuaa  阅读(0)  评论(0)    收藏  举报