赞
踩
Elastic Search
服务器:192.168.0.100
用户:docker启停:
docker start/stop/restart elasticsearchKibana控制台:
http://192.168.0.100:5601/app/kibana#/dev_tools/console
Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。
概念 | 英文 | 描述 |
---|---|---|
索引库 | indices | indices是index的复数,类似于Databases |
文档 | Document | 存入索引库原始的数据。比如每一条记录,就是一个文档。类似于Row字段(Field)文档中的属性,类似于Column。 |
映射配置 | mappings | 字段的数据类型、属性、是否索引、是否存储等特性。 |
索引名只能是小写,number_of_shards表示分片数,number_of_replicas表示副本数。
PUT index_name
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "index_name"
}
HEAD index_name
结果
200 - OK
GET index_name
结果
{ "index_name" : { "aliases" : { }, "mappings" : { }, "settings" : { "index" : { "creation_date" : "1585209049905", "number_of_shards" : "3", "number_of_replicas" : "2", "uuid" : "mYpHG1HkSuy2gWKXoowHug", "version" : { "created" : "7060099" }, "provided_name" : "index_name" } } } }
DELETE index_name
结果
{
"acknowledged" : true
}
属性名 | 属性 | 描述 |
---|---|---|
类型 | type | text、long、short、date、integer、object等 |
是否索引 | index | 默认true |
是否存储 | store | 默认false |
分词器 | analyzer | standard、whitespace 空格为分隔符、simple、stop、keyword等,参考https://www.cnblogs.com/xiaobaozi-95/p/9328948.html |
PUT index_name/_mapping { "properties": { "name": { "type": "text", "analyzer": "whitespace", "store": false }, "weight": { "type": "integer", "index": "true" }, "code": { "type": "keyword" } } }
结果
{
"acknowledged" : true
}
PUT index_name/_mapping
{
"properties": {
"code": {
"type": "keyword"
},
"sex": {
"type": "text"
}
}
}
结果
{
"acknowledged" : true
}
GET index_name/_mapping
结果
{ "index_name" : { "mappings" : { "properties" : { "code" : { "type" : "keyword" }, "name" : { "type" : "text", "analyzer" : "whitespace" }, "sex" : { "type" : "text" }, "weight" : { "type" : "integer" } } } } }
POST index_name/_doc/id1
{
"name" : "name1",
"weight" : 1,
"code": "code1"
}
结果
{
"_index" : "index_name",
"_type" : "_doc",
"_id" : "id1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 3,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
POST index_name/_doc
{
"name" : "name2",
"weight" : 2,
"code": "code2"
}
结果
{
"_index" : "index_name",
"_type" : "_doc",
"_id" : "JZ_VFXEBZtdZ_6fzGLrJ",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 3,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
有相同id则会更新
POST index_name/_doc/id1
{
"name" : "name3",
"weight" : 3,
"code": "code3"
}
结果
{
"_index" : "index_name",
"_type" : "_doc",
"_id" : "id1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 3,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
GET index_name/_doc/id1
结果
{
"_index" : "index_name",
"_type" : "_doc",
"_id" : "id1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "name3",
"weight" : 3,
"code" : "code3"
}
}
DELETE index_name/_doc/id1
结果
{
"_index" : "index_name",
"_type" : "_doc",
"_id" : "id1",
"_version" : 3,
"result" : "deleted",
"_shards" : {
"total" : 3,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
DELETE index_name/_doc/JZ_VFXEBZtdZ_6fzGLrJ
POST index_name/_doc/id1
{
"name" : "name1",
"weight" : 1,
"code": "code1"
}
POST index_name/_doc/id2
{
"name" : "name2",
"weight" : 2,
"code": "code2"
}
POST index_name/_doc/id3
{
"name" : "name1 name2",
"weight" : 3,
"code": "code1 code2"
}
GET index_name/_count
结果
{
"count" : 3,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
}
}
GET index_name/_search
或者
GET index_name/_search
{
"query":{
"match_all": {}
}
}
结果
{ "took" : 959, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 1.0, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id2", "_score" : 1.0, "_source" : { "name" : "name2", "weight" : 2, "code" : "code2" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 1.0, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] } }
GET index_name/_search
{
"query": {
"match": {
"name": "name1 name2"
}
}
}
结果
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.2039728, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id2", "_score" : 1.2039728, "_source" : { "name" : "name2", "weight" : 2, "code" : "code2" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 0.6931471, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 0.5753642, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] } }
GET index_name/_search
{
"query": {
"match": {
"name":{
"query": "name1 name2",
"operator": "and"
}
}
}
}
结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.5753642, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 0.5753642, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] } }
GET index_name/_search
{
"query": {
"match": {
"name":{
"query": "name1 name2",
"minimum_should_match": "50%"
}
}
}
}
结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.2039728, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id2", "_score" : 1.2039728, "_source" : { "name" : "name2", "weight" : 2, "code" : "code2" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 0.6931471, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 0.5753642, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] } }
GET index_name/_search
{
"query": {
"terms": {
"code": [
"code1",
"code2"
]
}
}
}
结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 1.0, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id2", "_score" : 1.0, "_source" : { "name" : "name2", "weight" : 2, "code" : "code2" } } ] } }
GET index_name/_search
{
"query":{
"range": {
"weight": {
"gte": 0.9,
"lte": 1.5
}
}
}
}
结果
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 1.0, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } } ] } }
查询name或者code中包含name1值的记录
GET index_name/_search
{
"query": {
"multi_match": {
"query": "name1",
"fields": [
"name",
"code"
]
}
}
}
结果
{ "took" : 18, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.6931471, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 0.6931471, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 0.2876821, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] } }
备注:bool里面的都是一些 条件,must必须瞒足,should只要满足minimum_should_match个条件是ture。
GET index_name/_search { "query":{ "bool": { "must": [{ "match": { "name": "name1" } }], "should": [ { "match": { "code": "code1" } }, { "match": { "weight": "2" } } ], "minimum_should_match": 1 } } }
结果
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.3862942, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 1.3862942, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } } ] } }
GET index_name/_search
{
"query": {
"match": {
"name":{
"query": "name1 name2",
"operator": "and"
}
}
},
"_source": "name"
}
结果
{ "took" : 6, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.5753642, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 0.5753642, "_source" : { "name" : "name1 name2" } } ] } }
GET index_name/_search
{
"query": {
"match": {
"name":{
"query": "name1 name2",
"operator": "and"
}
}
},
"_source": {
"excludes": "weight"
}
}
结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.5753642, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 0.5753642, "_source" : { "code" : "code1 code2", "name" : "name1 name2" } } ] } }
GET index_name/_search { "query": { "match": { "name":{ "query": "name1 name2", "operator": "and" } } }, "_source": { "includes": [ "name", "code" ] } }
结果
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.5753642, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 0.5753642, "_source" : { "code" : "code1 code2", "name" : "name1 name2" } } ] } }
备注:分页问题,效率会很低,尽量避免深分页。
分页:如果要查询出每页100条,第100页数据数据(9900-10000),如果是去5个节点查询,那么会在每个节点查询出第9900-10000条数据,然后做汇总,最后排序取出9900-10000条,这样做非常占资源。
GET index_name/_search
{
"from":1,
"size":2
}
结果
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id2", "_score" : 1.0, "_source" : { "name" : "name2", "weight" : 2, "code" : "code2" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 1.0, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] } }
POST index_name/_search?pretty
{
"sort":{
"weight":{
"order":"desc"
}
},
"size": 1
}
结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : null, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" }, "sort" : [ 3 ] } ] } }
GET index_name/_search
{
"aggs": {
"total_count": {
"value_count": {
"field": "weight"
}
},
"pjz":{
"avg": {
"field": "weight"
}
}
}
}
结果
{ "took" : 78, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 1.0, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id2", "_score" : 1.0, "_source" : { "name" : "name2", "weight" : 2, "code" : "code2" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 1.0, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] }, "aggregations" : { "pjz" : { "value" : 2.0 }, "total_count" : { "value" : 3 } } }
GET index_name/_search
{
"aggs": {
"fz": {
"terms": {
"field": "weight"
}
}
}
}
结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "index_name", "_type" : "_doc", "_id" : "id1", "_score" : 1.0, "_source" : { "name" : "name1", "weight" : 1, "code" : "code1" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id2", "_score" : 1.0, "_source" : { "name" : "name2", "weight" : 2, "code" : "code2" } }, { "_index" : "index_name", "_type" : "_doc", "_id" : "id3", "_score" : 1.0, "_source" : { "name" : "name1 name2", "weight" : 3, "code" : "code1 code2" } } ] }, "aggregations" : { "fz" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : 1, "doc_count" : 1 }, { "key" : 2, "doc_count" : 1 }, { "key" : 3, "doc_count" : 1 } ] } } }
scoll游标查询,指定scroll=时间,指定保存的分钟数,第一次发起请求放回的不是数据,而是_scroll_id,后面通过_scroll_id去请求数据,非常适合大批量查询。
备注:游标查询是在es里面缓存了结果,然后一次一次的去取,所以发起第一次请求的时候只有size,没有from,后面的请求只有scroll_id和scroll时间。
保持游标查询窗口1分钟,关键字为_doc
GET index_name/_search?scroll=1m
{
"sort" : ["_doc"],
"size":2
}
GET _search/scroll
{
"scroll": "1m",
"scroll_id" : "XXX"
}
GET /_cat/indices
结果【GET /_cat/indices?help】
health | status | name | uuid | pri | rep | docs.count | docs.deleted | store.size | pri.store.size |
---|---|---|---|---|---|---|---|---|---|
yellow | open | index_name | lWIDNTFgRVW7duDWd_XqRg | 3 | 2 | 4 | 0 | 24.6kb | 24.6kb |
yellow | open | index_name1 | yy62UUwjTo-cVVaKB7N_AA | 1 | 1 | 166 | 0 | 204.3kb | 204.3kb |
yellow | open | index_name2 | 7cHS493OT_qXeBS6QbKVow | 1 | 1 | 29 | 0 | 115.2kb | 115.2kb |
yellow | open | index_name3 | pzz2E75SRBqyVtDH14ZFRA | 1 | 1 | 31 | 0 | 92.7kb | 92.7kb |
yellow | open | index_name4 | dWmzRZEdT9uo4vqj3NkVag | 1 | 1 | 147 | 0 | 257.8kb | 257.8kb |
yellow | open | index_name5 | LrOutVUcQoiqDrMYlLNluQ | 1 | 1 | 1236 | 0 | 289.9kb | 289.9kb |
yellow | open | index_name6 | tPu4hjQlRJu1dV5ufrIcYg | 1 | 1 | 591 | 0 | 379.7kb | 379.7kb |
GET /_cat/health?v
结果
epoch | timestamp | cluster | status | node.total | node.data | shards | pri | relo | init | unassign | pending_tasks | max_task_wait_time | active_shards_percent |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1585207426 | 07:23:46 | docker-cluster | yellow | 1 | 1 | 34 | 34 | 0 | 0 | 30 | 0 | - | 53.1% |
GET index_name/_settings
结果
{ "index_name" : { "settings" : { "index" : { "creation_date" : "1585209049905", "number_of_shards" : "3", "number_of_replicas" : "2", "uuid" : "mYpHG1HkSuy2gWKXoowHug", "version" : { "created" : "7060099" }, "provided_name" : "index_name" } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。