查询集群文档数量
查询集群文档数量
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
curl -i
显示响应头信息
| curl -XGET -i -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/_count?pretty' |
示例数据
员工数据
- 结构化查询
- 全文搜索、短语搜索
- 高亮显示
- 数据分析
| curl -XPUT -i -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
| { "index" : { "_id" : "1" } } |
| { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } |
| { "index" : { "_id" : "2" } } |
| { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } |
| { "index" : { "_id" : "3" } } |
| { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] } |
| ' |
检索数据
- POST 创建文档
- PUT 替换文档
- GET 查询文档
- DELETE 删除文档
- HEAD 检查文档是否存在
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
简单搜索
查询所有数据,默认返回 10 条
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
搜索 last_name 为 Smith 的文档
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
精确匹配
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
| { |
| "query": { |
| "constant_score": { |
| "filter": { |
| "term": {"last_name.keyword": "Smith"} |
| } |
| } |
| } |
| } |
| ' |
全文搜索
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
| { |
| "query": { |
| "match": { |
| "last_name": "Smith" |
| } |
| } |
| } |
| ' |
复杂查询
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
| { |
| "query": { |
| "bool": { |
| "must": { |
| "match": {"last_name": "smith"} |
| }, |
| "filter": { |
| "range": {"age": {"gt": 30}} |
| } |
| } |
| } |
| } |
| ' |
短语搜索
结果必须包含 "rock climbing"
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
| { |
| "query": { |
| "match_phrase": { |
| "about": "rock climbing" |
| } |
| } |
| } |
| ' |
高亮搜索
| curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https: |
| { |
| "query": { |
| "match_phrase": { |
| "about": "rock climbing" |
| } |
| }, |
| "highlight": { |
| "fields": { |
| "about": {} |
| } |
| } |
| } |
| ' |
聚合
根据 interests 分组统计,all_interests 是别名
https://stackoverflow.com/questions/59298209/how-to-fix-elasticsearch-fielddata-is-disabled-on-text-fields-by-default-for-k
| curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \ |
| '{ |
| "aggs": { |
| "all_interests": { |
| "terms": { |
| "field": "interests.keyword" |
| } |
| } |
| } |
| }' |
先进行搜索,在对结果根据 interests 分组统计
| curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \ |
| '{ |
| "query": { |
| "match": { |
| "last_name": "Smith" |
| } |
| }, |
| "aggs": { |
| "all_interests": { |
| "terms": { |
| "field": "interests.keyword" |
| } |
| } |
| } |
| }' |
根据 interests 分组,再计算每组的平均年龄
| curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \ |
| '{ |
| "aggs": { |
| "all_interests": { |
| "terms": { |
| "field": "interests.keyword" |
| }, |
| "aggs": { |
| "avg_age": { |
| "avg": {"field": "age"} |
| } |
| } |
| } |
| |
| } |
| }' |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性