Skip to content

查询DSL与搜索

查询类型总览

┌─────────────────────────────────────────┐
│              Query DSL                   │
├─────────────────────────────────────────┤
│                                          │
│  1. 全文搜索                             │
│     - match / match_phrase / query_string │
│                                          │
│  2. 精确查询                            │
│     - term / terms / range              │
│                                          │
│  3. 复合查询                            │
│     - bool / constant_score            │
│                                          │
│  4. 嵌套查询                            │
│     - nested / has_child / has_parent   │
│                                          │
│  5. 函数式查询                          │
│     - function_score / script         │
└─────────────────────────────────────────┘

1. 全文搜索

match查询

json
// 最常用的全文搜索
{
  "query": {
    "match": {
      "title": "iPhone 15"
    }
  }
}

// match_phrase:短语匹配
{
  "query": {
    "match_phrase": {
      "title": "iPhone 15"
    }
  }
}

// match_phrase_prefix:前缀匹配
{
  "query": {
    "match_phrase_prefix": {
      "title": "iPhone"
    }
  }
}

query_string

json
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "iPhone AND 15"
    }
  }
}

multi_match

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

2. 精确查询

term查询

json
// 精确匹配,不分词
{
  "query": {
    "term": {
      "status": "active"
    }
  }
}

// terms:多个精确值
{
  "query": {
    "terms": {
      "status": ["active", "pending"]
    }
  }
}

range查询

json
{
  "query": {
    "range": {
      "price": {
        "gte": 1000,
        "lte": 2000
      }
    }
  }
}

// 日期范围
{
  "query": {
    "range": {
      "create_date": {
        "gte": "2024-01-01",
        "lte": "now"
      }
    }
  }
}

exists / ids

json
// 字段存在
{
  "query": {
    "exists": {
      "field": "price"
    }
  }
}

// 指定ID查询
{
  "query": {
    "ids": {
      "values": ["1", "2", "3"]
    }
  }
}

3. 复合查询

bool查询

json
{
  "query": {
    "bool": {
      "must": [
        {"match": {"title": "iPhone"}}
      ],
      "should": [
        {"match": {"description": "pro"}}
      ],
      "must_not": [
        {"term": {"status": "deleted"}}
      ],
      "filter": [
        {"term": {"category": "phone"}}
      ]
    }
  }
}

bool条件说明

条件说明计分
must必须满足
should应该满足
must_not必须不满足
filter必须满足❌(不考虑评分)

constant_score

json
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {"status": "active"}
      },
      "boost": 1.0
    }
  }
}

4. 嵌套查询

nested查询

json
// 索引结构
{
  "mappings": {
    "properties": {
      "comments": {
        "type": "nested",
        "properties": {
          "user": {"type": "keyword"},
          "content": {"type": "text"}
        }
      }
    }
  }
}

// nested查询
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "match": {"comments.content": "good"}
      }
    }
  }
}

5. 查询子句

from / size

json
{
  "query": {"match_all": {}},
  "from": 0,
  "size": 10
}

sort

json
{
  "query": {"match_all": {}},
  "sort": [
    {"price": {"order": "asc"}},
    {"_score": {"order": "desc"}}
  ]
}

highlight

json
{
  "query": {"match": {"title": "iPhone"}},
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": ["<em>"],
        "post_tags": ["</em>"]
      }
    }
  }
}

面试考点

Q: match vs term?

查询分词用途
match全文搜索
term精确值
range-范围

Q: should vs must?

  • must:必须满足,影响评分
  • should:满足更好,不满足也可以

Q: 为什么filter快?

  • 不计算评分
  • 可以缓存结果

Q: 高亮不生效?

  • 检查字段是否index
  • 检查分词器配置

最后更新: