安装elasticsearch
1.Mac安装elasticsearch
brew install elasticsearch
image.png
2.启动和停止
brew services start elasticsearch
brew services stop elasticsearch
{
"name" : "WuChangMacBookPro",
"cluster_name" : "elasticsearch_brew",
"cluster_uuid" : "_njBreX6QwiDpWj4FtOZRw",
"version" : {
"number" : "7.10.2-SNAPSHOT",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "unknown",
"build_date" : "2021-01-16T01:41:27.115673Z",
"build_snapshot" : true,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
4.es 添加document
POST localhost:9200/user_log/_doc
{
"id": 1,
"name": "xxx",
"sex": "man",
"age": 10
}
5.es 批量插入数据.
POST localhost:9200/user_log/_bulk
{
"id": 1,
"name": "xxx",
"sex": "man",
"age": 10
}
{
"id": 2,
"name": "xxx",
"sex": "man",
"age": 20
}
6.查询数据,按照age排序
POST localhost:9200/user_log/_search
{
"query": {
"match_all": { }
},
"sort": [
{
"age": "desc"
}
]
}
7.查询指定字段,查询name,age
POST localhost:9200/user_log/_search
{
"query": {
"match_all": {}
},
"fields": ["name", "age"],
"_source": false,
"sort": [{
"age": "desc"
}]
}
8.条件查询,range范围查询。
POST localhost:9200/user_log/_search
{
"query": {
"range": {
"age": {
"gte": "5",
"lte": "10"
}
}
},
"_source": true,
"sort": [{
"age": "desc"
}]
}
9.组合查询,年龄大于等于5,小于40,name等于xxx
POST localhost:9200/user_log/_search
{
"query": {
"bool": {
"filter": [{
"range": {
"age": {
"gte": "5",
"lt": "40"
}
}
},
{
"terms": {
"name": [
"xxx"
]
}
}
]
}
},
"_source": true,
"sort": [{
"age": "desc"
}]
}
10.聚合数据,使用聚合将数据汇总为度量、统计或其他分析。
POST locahost:9200/user_log/_search
{
"query": {
"bool": {
"filter": [{
"range": {
"age": {
"gte": "5",
"lt": "40"
}
}
}]
}
},
"aggs": {
"average_response_size": {
"avg": {
"field": "http.response.body.bytes"
}
}
},
"fields": [
"name",
"http.response.body.bytes"
],
"_source": false,
"sort": [{
"age": "desc"
}]
}
11.删除索引product_log
DELETE localhost:9200/product_log
12.查询所有索引
GET localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open user_log 0pN86mPiRbma1w2CoXfZBA 1 1 3 0 9.8kb 9.8kb
yellow open logs-my_app-default n4vSydRFT1usn_WekYC_zQ 1 1 1 0 4.6kb 4.6kb
13.根据条件查询,name为xxx
POST localhost:9200/user_log/_search
{
"query": {
"match": {
"name": "xxx"
}
}
}
14.多条件组合查询;
GET user_log/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "xxx"
}
},
{
"match": {
"sex": "man"
}
}
]
}
}
}
网友评论