Elasticsearch 基础入门

查询集群文档数量

查询集群文档数量

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/_count?pretty'

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://localhost:9200/employee/_bulk?pretty' -d'
{ "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://localhost:9200/employee/_doc/1?pretty'

简单搜索

查询所有数据,默认返回 10 条

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty'

搜索 last_name 为 Smith 的文档

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty&q=last_name:Smith'

精确匹配

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
"query": {
"constant_score": {
"filter": {
"term": {"last_name.keyword": "Smith"}
}
}
}
}
'

全文搜索

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
"query": {
"match": {
"last_name": "Smith"
}
}
}
'

复杂查询

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
"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://localhost:9200/employee/_search?pretty' -d'
{
"query": {
"match_phrase": {
"about": "rock climbing"
}
}
}
'

高亮搜索

curl -XGET -k -u elastic:passwd -H 'Content-Type: application/json' 'https://localhost:9200/employee/_search?pretty' -d'
{
"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"}
}
}
}
}
}'
posted @   廖子博  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示