在当今信息爆炸的时代,对于大规模数据的搜索、分析和可视化变得至关重要。Elasticsearch(ES)作为一款开源搜索和分析引擎,为开发者和企业提供了一种强大而灵活的工具,能够轻松处理海量数据,并提供高效的搜索和分析能力
本文围绕 Elasticsearch 来讲解一下如何创建索引、进行数据写入和查询,快速入门ES使用
创建索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| PUT <index> { "settings": { "number_of_shards": "3", "number_of_replicas": "1" }, "mappings": { "properties": { "address": { "type": "text" }, "userName": { "type": "keyword" }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "fullName": { "properties": { "firstName": { "type": "text" }, "lastName": { "type": "text" } } } } } }
|
索引操作
基本CRUD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| POST <index>/_doc/<id> { "id": "1", "name": "zhangsan" }
GET <index>/_doc/<id>
POST <index>/_update/<id> { "doc": { "id": "1", "name": "zhangsan" } }
DELETE <index>/_doc/<id>
|
乐观锁修改数据
在修改请求url后拼接参数?if_seq_no=xx&if_primary_term=yy, 具体的值为之前查询出的数据值
1 2 3 4 5 6 7 8 9 10 11 12
| PUT <index>/_doc/1?if_seq_no=xx&if_primary_term=yy {
}
POST <index>/_update/<id>?if_seq_no=xx&if_primary_term=yy { "doc": { "id": "1", "name": "zhangsan" } }
|
批量写入数据
1 2 3 4 5 6 7 8 9 10 11 12 13
| POST <index>/_bulk { {"index":{"_id":1}} {"id":1, "name":"name1"} {"create":{"_id":2}} {"id":2, "name":"name2"}
{"update":{"_id":3}} {"doc":{"name":"name3"}}
{"delete": {"_id": 4}} }
|
搜索数据
精确查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| POST <index>/_search { "query": { "term": { "<property>": { "value": "<value>" } } } }
POST <index>/_search { "query": { "terms": { "<property>": [ "value1", "value2" ] } } }
GET <index>/_search { "query": { "ids": { "values": [<id1>, <id2>] } } }
GET <index>/_search { "query": { "range": { "<field>": { "gte": "1998-09-01 00:00:00", "lte": "1998-11-01 00:00:00" } } } }
GET <index>/_search { "query": { "exists": { "field": "<field>" } } }
|
全文检索
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| POST <index>/_search { "query": { "match": { "<field>": { "query": "<value1> <value2>" } } } }
POST <index>/_search { "query": { "multi_match": { "query": "<value1> <value2>", "fields": ["<field1>", "<field2>"], "operator": "and" } } }
POST <index>/_search { "query": { "query_string": { "query": "\"content\"=\"XXX\" AND \"content\"=\"YYY\"" } } }
|
复合查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| POST <index>/_search { "query": { "bool": { "must": [ { "match": { "<field>": "<value>" } } ], "filter": [ { "term": { "<field>": "<value>" }, "range": { "<field>": { "gte": 10, "lte": 20 } } } ] } } }
|
只获取需要字段
1 2 3 4 5 6 7 8
| GET <index>/_search { "query": { "match_all": {} }, "_source": ["<field>"] }
|
分页查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| GET <index>/_search { "query": { "match_all": {} }, "size": 2, "from": 0, "track_total_hits": true, "sort": [ { "<field>": { "order": "desc" } } ] }
POST <index>/_search?scroll=1m { "query": { "match_all": {} }, "size": 10, "track_total_hits": true }
POST /_search/scroll { "scroll": "1m", "scroll_id":"<_scroll_id>" }
DELETE /_search/scroll { "scroll_id":"<_scroll_id>" }
GET books/_search { "query": { "match_all": {} }, "size": 2, "from": 0, "track_total_hits": true, "sort": [ { "<field>": { "order": "desc" } } ], "search_after": [311] }
|
对象嵌套结构
构造有嵌套结构的索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| PUT <index> { "mappings": { "properties": { "<field1>": { "type": "nested", "properties": { "<nested field1>": { "type": "integer" } } } } } }
|
嵌套结构查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| POST <index>/_search { "query": { "nested": { "path": "<嵌套对象路径>", "query": { "bool": { "must": [ { "match": { "<field>": "<value>" } } ] } }, "inner_hits": {} } } }
|