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 @ 2024-10-12 19:33  廖子博  阅读(4)  评论(0编辑  收藏  举报