开源自建,GPT 切换空间麻烦?一件帮你搞定

核心内容摘要

告别传统春联!用这款AI工具,3步生成皇城大门同款书法对联
Skills精选

all-MiniLM-L6-v2实战案例:Python调用Ollama Embedding API实现文本聚类

基础概念什么是 DSLDSL (Domain Specific Language) 是 Elasticsearch 的查询语言基于 JSON 格式用于执行各种数据操作。

核心概念{ index: 索引名, // 类似数据库 type: 类型名, //

x 已废弃统一为 _doc id: 文档ID, // 类似主键 document: 文档内容 // 类似记录 }索引操作创建索引PUT /products { settings: { number_of_shards: 3, number_of_replicas: 1 }, mappings: { properties: { name: { type: text, analyzer: ik_max_word }, price: { type: double }, category: { type: keyword }, description: { type: text, analyzer: ik_max_word }, stock: { type: integer }, created_at: { type: date, format: yyyy-MM-dd HH:mm:ss }, is_active: { type: boolean } } } }查看索引GET /products GET /products/_mapping GET /_cat/indices?v删除索引DELETE /products文档操作创建文档POST /products/_doc { name: iPhone 15 Pro, price: 7999, category: 手机, description: 苹果最新款智能手机, stock: 100, created_at:

10:00:00, is_active: true }创建指定 ID 文档PUT /products/_doc/1 { name: MacBook Pro, price: 12999, category: 电脑, description: 苹果专业笔记本电脑, stock: 50, created_at:

11:00:00, is_active: true }批量创建文档POST /_bulk { index: { _index: products, _id: 2 } } { name: iPad Air, price: 4399, category: 平板, description: 轻薄平板电脑, stock: 80, created_at:

12:00:00, is_active: true } { index: { _index: products, _id: 3 } } { name: AirPods Pro, price: 1899, category: 耳机, description: 无线降噪耳机, stock: 200, created_at:

13:00:00, is_active: true } { index: { _index: products, _id: 4 } } { name: Apple Watch, price: 2999, category: 手表, description: 智能手表, stock: 150, created_at:

14:00:00, is_active: true }查询文档GET /products/_doc/1更新文档POST /products/_update/1 { doc: { price: 11999, stock: 45 } }删除文档DELETE /products/_doc/1基础查询查询所有文档GET /products/_search { query: { match_all: {} } }分页查询GET /products/_search { query: { match_all: {} }, from: 0, size: 10 }指定返回字段GET /products/_search { query: { match_all: {} }, _source: [name, price, category] }排序GET /products/_search { query: { match_all: {} }, sort: [ { price: { order: desc } } ] }查询类型详解

Match Query全文搜索GET /products/_search { query: { match: { name: 苹果 手机 } } }

Term Query精确匹配GET /products/_search { query: { term: { category: 手机 } } }

Terms Query多值精确匹配GET /products/_search { query: { terms: { category: [手机, 电脑] } } }

Range Query范围查询GET /products/_search { query: { range: { price: { gte: 1000, lte: 10000 } } } }范围操作符gt: 大于gte: 大于等于lt: 小于lte: 小于等于

Prefix Query前缀匹配GET /products/_search { query: { prefix: { name: iPh } } }

Wildcard Query通配符匹配GET /products/_search { query: { wildcard: { name: *Pro* } } }

Fuzzy Query模糊查询GET /products/_search { query: { fuzzy: { name: { value: iPone, fuzziness: 2 } } } }

Bool Query布尔查询GET /products/_search { query: { bool: { must: [ { match: { name: 苹果 } } ], must_not: [ { term: { category: 耳机 } } ], should: [ { range: { price: { lte: 5000 } } } ], filter: [ { term: { is_active: true } } ] } } }Bool 子句说明must: 必须匹配影响得分must_not: 必须不匹配should: 应该匹配影响得分filter: 必须匹配不影响得分复合查询

Multi Match Query多字段搜索GET /products/_search { query: { multi_match: { query: 苹果, fields: [name, description] } } }

Query String Query查询字符串GET /products/_search { query: { query_string: { fields: [name, description], query: (苹果 AND 手机) OR (iPad) } } }

Nested Bool Query嵌套布尔查询GET /products/_search { query: { bool: { must: [ { bool: { should: [ { match: { name: 苹果 } }, { match: { description: 苹果 } } ] } }, { range: { price: { gte: 1000 } } } ] } } }聚合查询

Terms Aggregation词项聚合GET /products/_search { size: 0, aggs: { category_count: { terms: { field: category, size: 10 } } } }

Stats Aggregation统计聚合GET /products/_search { size: 0, aggs: { price_stats: { stats: { field: price } } } }返回count、min、max、avg、sum

Range Aggregation范围聚合GET /products/_search { size: 0, aggs: { price_ranges: { range: { field: price, ranges: [ { to: 2000 }, { from: 2000, to: 5000 }, { from: 5000 } ] } } } }

Date Histogram Aggregation日期直方图GET /products/_search { size: 0, aggs: { products_over_time: { date_histogram: { field: created_at, calendar_interval: day, format: yyyy-MM-dd } } } }

Bucket Aggregation Metric Aggregation桶聚合 指标聚合GET /products/_search { size: 0, aggs: { by_category: { terms: { field: category }, aggs: { average_price: { avg: { field: price } }, max_price: { max: { field: price } } } } } }

Filter Aggregation过滤聚合GET /products/_search { size: 0, aggs: { active_products: { filter: { term: { is_active: true } }, aggs: { avg_price: { avg: { field: price } } } } } }高级功能

Highlight高亮显示GET /products/_search { query: { match: { name: 苹果 } }, highlight: { fields: { name: {}, description: {} } } }

Script Fields脚本字段GET /products/_search { query: { match_all: {} }, script_fields: { discount_price: { script: { source: doc[price].value *

9 } } } }

Source Filtering源过滤GET /products/_search { query: { match_all: {} }, _source: { includes: [name, price], excludes: [description] } }

Explain解释查询GET /products/_search { query: { match: { name: 苹果 } }, explain: true }

Profile性能分析GET /products/_search { profile: true, query: { match: { name: 苹果 } } }实战示例示例 1: 电商商品搜索GET /products/_search { query: { bool: { must: [ { multi_match: { query: 苹果, fields: [name^2, description], type: best_fields } } ], filter: [ { term: { is_active: true } }, { range: { price: { gte: 1000, lte: 15000 } } } ] } }, aggs: { categories: { terms: { field: category, size: 10 } }, price_ranges: { range: { field: price, ranges: [ { to: 2000, key: 低价 }, { from: 2000, to: 5000, key: 中价 }, { from: 5000, key: 高价 } ] } } }, sort: [ { _score: { order: desc } } ], from: 0, size: 20, highlight: { fields: { name: {}, description: {} }, pre_tags: [em], post_tags: [/em] } }示例 2: 日志分析GET /logs/_search { query: { bool: { must: [ { range: { timestamp: { gte: now-24h, lte: now } } } ], filter: [ { term: { level: ERROR } } ] } }, aggs: { errors_by_service: { terms: { field: service_name, size: 20 }, aggs: { error_types: { terms: { field: error_type, size: 10 } }, timeline: { date_histogram: { field: timestamp, calendar_interval: 1h } } } } }, sort: [ { timestamp: { order: desc } } ], size: 100 }示例 3: 用户行为分析GET /user_actions/_search { query: { bool: { must: [ { range: { timestamp: { gte: now-7d/d, lte: now/d } } } ] } }, aggs: { daily_stats: { date_histogram: { field: timestamp, calendar_interval: day }, aggs: { unique_users: { cardinality: { field: user_id } }, action_types: { terms: { field: action_type } } } }, top_users: { terms: { field: user_id, size: 10, order: { action_count: desc } }, aggs: { action_count: { value_count: { field: _id } } } } } }示例 4: 复杂的多条件查询GET /products/_search { query: { bool: { must: [ { bool: { should: [ { match: { name: 手机 } }, { match: { description: 手机 } } ], minimum_should_match: 1 } } ], must_not: [ { term: { category: 配件 } } ], should: [ { range: { price: { lte: 3000 } } }, { term: { stock: { value: 0 } } } ], filter: [ { term: { is_active: true } }, { range: { created_at: { gte: now-30d } } } ] } }, aggs: { category_stats: { terms: { field: category }, aggs: { price_stats: { stats: { field: price } }, stock_stats: { stats: { field: stock } } } } }, sort: [ { price: { order: asc } }, { _score: { order: desc } } ], from: 0, size: 20 }常用操作符和参数查询参数参数说明from起始位置size返回数量timeout超时时间track_total_hits跟踪总命中数request_cache请求缓存排序参数参数说明order排序方向asc/descmode排序模式min/max/sum/avg/medianmissing缺失值处理_last/_first聚合参数参数说明size返回桶的数量order排序方式min_doc_count最小文档数性能优化建议

使用 filter 而非 query// 不推荐 GET /products/_search { query: { term: { is_active: true } } } // 推荐 GET /products/_search { query: { bool: { filter: [ { term: { is_active: true } } ] } } }

限制返回字段GET /products/_search { _source: [name, price], query: { match_all: {} } }

使用 scroll 处理大量数据GET /products/_search { scroll: 1m, size: 1000, query: { match_all: {} } }

避免深度分页// 使用 search_after 代替 from/size GET /products/_search { size: 100, query: { match_all: {} }, sort: [ { _id: asc } ], search_after: [last_document_id] }

总结Elasticsearch DSL 的核心要点查询类型match、term、range、bool 等复合查询bool 查询的 must、must_not、should、filter聚合分析terms、stats、range、date_histogram 等高级功能highlight、script、explain、profile性能优化使用 filter、限制字段、避免深度分页通过本教程你应该能够创建和管理索引执行各种类型的查询进行数据聚合分析优化查询性能继续实践和探索你会发现 Elasticsearch DSL 的强大之处

ysl水蜜桃86满十八岁可以用的吗女生官方版-ysl水蜜桃86满十八岁可以用的吗女生官方版应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123