Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

首先,安装好Elasticsearch和Kibana,具体操作细节可以参考专栏文章《Elasticsearch v7.x从入门到精通》

一、添加样例数据

启动Kibana后,访问Kibana首页,就可以添加一些样例数据到Elasticsearch索引中了。

Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

选择添加【样例电子商务订单】的数据:

Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

如对基于Elasticsearch v7.x 构建订单中心实战项目有兴趣,可以访问Elasticsearch技术专栏《Elasticsearch 7.x订单中心实战》

Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

该技术专栏基于最新的 Elasticsearch v7.x 版本进行订单中心实际项目演示,帮助你对Elasticsearch 新特性有更加全面和直观的了解。

二、查看仪表板(Dashboard)

添加完成样例数据之后,再点击查看数据按钮,然后选择仪表板菜单项。

Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

下面是[电子商务]收入仪表板的样例数据展示效果。

Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

三、查看索引和文档记录

通过Elasticsearch head插件,可以方便发现新增的索引 kibana_sample_data_ecommerce。

Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

进一步通过Elasticsearch head 插件查看索引中的文档记录。

Elasticsearch/Kibana v7.x 中导入样例数据-电子商务订单数据

四、查看kibana_sample_data_ecommerce 类型定义

在Kibana的Dev tools 工具中,通过GET kibana_sample_data_ecommerce 命令,获取索引的mapping定义。

{

"kibana_sample_data_ecommerce" : {

"aliases" : { },

"mappings" : {

"properties" : {

"category" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword"

}

}

},

"currency" : {

"type" : "keyword"

},

"customer_birth_date" : {

"type" : "date"

},

"customer_first_name" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword",

"ignore_above" : 256

}

}

},

"customer_full_name" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword",

"ignore_above" : 256

}

}

},

"customer_gender" : {

"type" : "keyword"

},

"customer_id" : {

"type" : "keyword"

},

"customer_last_name" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword",

"ignore_above" : 256

}

}

},

"customer_phone" : {

"type" : "keyword"

},

"day_of_week" : {

"type" : "keyword"

},

"day_of_week_i" : {

"type" : "integer"

},

"email" : {

"type" : "keyword"

},

"geoip" : {

"properties" : {

"city_name" : {

"type" : "keyword"

},

"continent_name" : {

"type" : "keyword"

},

"country_iso_code" : {

"type" : "keyword"

},

"location" : {

"type" : "geo_point"

},

"region_name" : {

"type" : "keyword"

}

}

},

"manufacturer" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword"

}

}

},

"order_date" : {

"type" : "date"

},

"order_id" : {

"type" : "keyword"

},

"products" : {

"properties" : {

"_id" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword",

"ignore_above" : 256

}

}

},

"base_price" : {

"type" : "half_float"

},

"base_unit_price" : {

"type" : "half_float"

},

"category" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword"

}

}

},

"created_on" : {

"type" : "date"

},

"discount_amount" : {

"type" : "half_float"

},

"discount_percentage" : {

"type" : "half_float"

},

"manufacturer" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword"

}

}

},

"min_price" : {

"type" : "half_float"

},

"price" : {

"type" : "half_float"

},

"product_id" : {

"type" : "long"

},

"product_name" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword"

}

},

"analyzer" : "english"

},

"quantity" : {

"type" : "integer"

},

"sku" : {

"type" : "keyword"

},

"tax_amount" : {

"type" : "half_float"

},

"taxful_price" : {

"type" : "half_float"

},

"taxless_price" : {

"type" : "half_float"

},

"unit_discount_amount" : {

"type" : "half_float"

}

}

},

"sku" : {

"type" : "keyword"

},

"taxful_total_price" : {

"type" : "half_float"

},

"taxless_total_price" : {

"type" : "half_float"

},

"total_quantity" : {

"type" : "integer"

},

"total_unique_products" : {

"type" : "integer"

},

"type" : {

"type" : "keyword"

},

"user" : {

"type" : "keyword"

}

}

},

"settings" : {

"index" : {

"number_of_shards" : "1",

"auto_expand_replicas" : "0-1",

"provided_name" : "kibana_sample_data_ecommerce",

"creation_date" : "1570804408643",

"number_of_replicas" : "0",

"uuid" : "RWmbl1QLQX-JnDtTnr9ZbQ",

"version" : {

"created" : "7030099"

}

}

}

}

}

上面mapping定义中,可以找到学习和借鉴的地方。

比如,其中customer_first_name 字段的定义,使用 text 字段来实现全文本查询,同时使用一个未分词的 keyword 字段,且启用doc_values,来处理聚合操作。

"customer_first_name" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword",

"ignore_above" : 256

}

}

}

customer_first_name 用于全文检索;customer_first_name.keyword 用于聚合、排序等等操作。

另外,其中products 字段的定义,是在索引映射中创建了一个嵌套字段 products。

"products" : {

"properties" : {

"_id" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword",

"ignore_above" : 256

}

}

},

"base_price" : {

"type" : "half_float"

},

"base_unit_price" : {

"type" : "half_float"

},

"category" : {

"type" : "text",

"fields" : {

"keyword" : {

"type" : "keyword"

}

}

},

......

}

}

再看看嵌套字段products的赋值,可以看出products嵌套字段也是数组类型。为嵌套字段赋予多个值,那么ElasticSearch自动把字段值转换为数组类型。

嵌套字段是指对象类型的一个特殊版本,ElasticSearch引擎在内部把嵌套字段索引成单个文档。如果在嵌套字段中定义对象数组,那么对象数组中的每个元素(文档)都被索引成单个文档,每个文档都能被独立地查询。

"products" : [

{

"base_price" : 11.99,

"discount_percentage" : 0,

"quantity" : 1,

"manufacturer" : "Elitelligence",

"tax_amount" : 0,

"product_id" : 6283,

"category" : "Men's Clothing",

......

},

{

"base_price" : 24.99,

"discount_percentage" : 0,

"quantity" : 1,

"manufacturer" : "Oceanavigations",

"tax_amount" : 0,

"product_id" : 19400,

"category" : "Men's Clothing",

......

]