- Published on
ElasticSearch快速入门
- Authors
- Name
- wellsleep (Liu Zheng)
开发环境
阿里云 ElasticSearch 服务(7.10 版本)
创建一个索引
// PUT 索引名
PUT /zliu-test-index
{
"mappings": {
"properties": {
"description": { "type": "text" }, // 这里填写每个字段的属性,text 属性会自动分词
"hs6code": { "type": "keyword" }, // keyword 属性会把所有内容当作整体匹配
"sourceCountry": { "type": "text" },
"destinationCountry": { "type": "text" },
"suitableDescription": { "type": "text"},
"suitableHS6code": { "type": "text" },
"exportCode": { "type": "text" },
"importCode": { "type": "text" }
}
}
}
RESPONSE:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "zliu-test-index"
}
查看索引字段定义
// 索引名/_mapping
GET dhl/_mapping
RESPONSE:
{
"dhl" : {
"mappings" : {
"properties" : {
"add_time" : {
"type" : "date",
"store" : true,
"format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"description" : {
"type" : "text", // text 类型默认支持分词
"store" : true
},
"event" : {
"type" : "keyword", // keyword 类型为全词匹配
"store" : true
},
"ric" : {
"type" : "keyword",
"store" : true
},
"standard_code" : {
"type" : "keyword",
"store" : true
},
"status" : {
"type" : "keyword",
"store" : true
},
"tracking_info" : {
"type" : "keyword",
"store" : true
},
"update_time" : {
"type" : "date",
"store" : true,
"format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
删除索引
DELETE zliu-test-index
RESPONSE:
{
"acknowledged" : true
}
插入doc
// POST 索引名/_doc,如果使用 POST 索引名/_doc/文档id 则 文档id 不可重复
POST zliu-test-index/_doc
{
"description": "Earring(Iron,Zinc)",
"hs6code": "100000",
"sourceCountry": "CN",
"destinationCountry": "DE",
"suitableDescription": "vitamin supplements",
"suitableHS6code": "210690",
"exportCode": "2106903010",
"importCode": "2106909260"
}
RESPONSE:
{
"_index" : "zliu-test-index",
"_type" : "_doc",
"_id" : "aGZ8f30Bst2-Rx77RrDV",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
查看doc
// 索引名/_doc/文档名
GET dhl/_doc/CmZhdX0Bst2-Rx77fQvD
RESPONSE:
{
"_index" : "dhl",
"_type" : "_doc",
"_id" : "CmZhdX0Bst2-Rx77fQvD",
"_version" : 1,
"_seq_no" : 729,
"_primary_term" : 1,
"found" : true,
"_source" : {
"tracking_info" : "00340434321030951347",
"update_time" : "2021-11-26 01:52:44",
"event" : "LDTMV",
"ric" : "MVMTV",
"standard_code" : "AA",
"description" : "Loaded to movement / tour vehicle (Movement / tour vehicle)",
"add_time" : "2021-12-01 17:44:21",
"status" : 2
}
}
删除doc
// DELETE 索引名/_doc/文档id
DELETE zliu-test-index/_doc/aGZ8f30Bst2-Rx77RrDV
RESPONSE:
{
"_index" : "zliu-test-index",
"_type" : "_doc",
"_id" : "aGZ8f30Bst2-Rx77RrDV",
"_version" : 2,
"result" : "deleted", // 状态
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
布尔查询(计算匹配得分,略慢)
GET _search
{
"query": {
"bool": { //布尔搜素
"must": [ //以下条件为逻辑与,如果使用逻辑或,关键词为"should"
{
"match": {
"description": "Loaded"
}
},
{
"match": {
"tracking_info": "00340434321030951347"
}
},
{
"range": {
"update_time": {
"gt":"2021-11-01 00:00:00"
}
}
}
]
}
}
}
RESPONSE:
{
"took" : 715,
"timed_out" : false,
"_shards" : {
"total" : 24,
"successful" : 24,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 12.623645, // 匹配得分
"hits" : [
{
"_index" : "dhl",
"_type" : "_doc",
"_id" : "CmZhdX0Bst2-Rx77fQvD",
"_score" : 12.623645,
"_source" : {
"tracking_info" : "00340434321030951347",
"update_time" : "2021-11-26 01:52:44",
"event" : "LDTMV",
"ric" : "MVMTV",
"standard_code" : "AA",
"description" : "Loaded to movement / tour vehicle (Movement / tour vehicle)",
"add_time" : "2021-12-01 17:44:21",
"status" : 2
}
}
]
}
}
布尔查询(不计算匹配得分,较快,自动缓存结果)
GET _search
{
"query": {
"bool": {
"filter": [ // 关键词 filter 即不计算得分
{
"match": {
"description": "Loaded"
}
},
{
"match": {
"tracking_info": "00340434321030951347"
}
},
{
"range": {
"update_time": {
"gt":"2021-11-01 00:00:00"
}
}
}
]
}
}
}
RESPONSE:
{
"took" : 481,
"timed_out" : false,
"_shards" : {
"total" : 24,
"successful" : 24,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "dhl",
"_type" : "_doc",
"_id" : "CmZhdX0Bst2-Rx77fQvD",
"_score" : 0.0,
"_source" : {
"tracking_info" : "00340434321030951347",
"update_time" : "2021-11-26 01:52:44",
"event" : "LDTMV",
"ric" : "MVMTV",
"standard_code" : "AA",
"description" : "Loaded to movement / tour vehicle (Movement / tour vehicle)",
"add_time" : "2021-12-01 17:44:21",
"status" : 2
}
}
]
}
}