在本节中,我们将向ElasticSearch添加一些索引、映射和数据。这些数据将在本教程中解释的示例中使用。
我们先启动好ElasticSearch和Kibana,然后访问http://localhost:5601/app/kibana#/dev_tools,进入Kibana的开发工具界面,让我们一步一步做。
- 😄博主:程序员一灯,一个老菜鸟,哈哈哈哈
- 🚌一个人可以走的很快,一群人可以走的很远🇨🇳
- 🎉点赞➕评论➕收藏 ➕关注== 养成习惯(一键四连)📝
- 🎉欢迎关注💗一起学习👍一起讨论⭐️一起进步📝
- 🙏作者水平有限,欢迎各位大佬指点,相互学习进步!😆
一、索引
二、文档
三、ES文档的结构
四、映射(mapping)
五、DSL
六、创建索引
6.1、响应内容
七、与数据库类比
八、创建索引
8.1、加数据
8.2、请求正文
九、创建映射并添加数据
十一、添加其他索引
11.1、返回响应
11.2、添加其他数据
11.3、返回响应
一、索引Elasticsearch中的索引有多层的意思:
a. 某一类文档的集合就构成了一个索引,类比到数据库就是一个数据库(或者数据库表);
b.它还描述了一个动作,就是将某个文档保存在elasticsearch的过程也叫索引;
c. 倒排索引。
二、文档具体的一条数据,类比到数据库就是一条记录。
三、ES文档的结构mapping 是ES每一个文档的约束信息,例如属性的类型,是否能被索引等。
映射是定义文档及其包含的字段如何存储和索引的过程。
每个文档都是字段的集合,每个字段都有自己的 数据类型。映射数据时,您创建一个映射定义,其中包含与文档相关的字段列表。映射定义还包括元数据字段,例如_source自定义如何处理文档关联元数据的 字段。
数据类型介绍:Elasticsearch品读—第三章:ES数据类型_sym542569199的博客-CSDN博客_es 数据类型
五、DSLElasticsearch 提供了一个基于 JSON 的完整 Query DSL(Domain Specific Language)来定义查询。将查询 DSL 视为查询的 AST(抽象语法树),由两种类型的子句组成:
叶查询子句
叶查询子句在特定字段中查找特定值,例如 match、term或 range查询。这些查询可以单独使用。
复合查询子句
复合查询子句包装其他叶查询或复合查询,用于以逻辑方式组合多个查询(例如 boolordis_max查询),或改变它们的行为(例如 constant_score查询)。
查询子句的行为取决于它们是用于 查询上下文还是过滤器上下文。
六、创建索引在Kibana中执行如下命令
PUT schools
6.1、响应内容命令执行后,如果没有任何问题,会返回如下内容。
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "schools"
}
如果没有提示error,就意味着成功创建了索引 ,如图所示
我们通过大家比较熟悉的 DBMS 与 ES 的基本概念进行类比,加深大家的理解。
PUT school
8.1、加数据Elasticsearch将存储我们添加到索引中的文档,如以下代码所示。给文档提供了一些ID,这些ID用于识别文档。
8.2、请求正文POST school/_doc/10
{
"name":"Saint Paul School", "description":"ICSE Afiliation",
"street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075",
"location":[28.5733056, 77.0122136], "fees":5000,
"tags":["Good Faculty", "Great Sports"], "rating":"4.5"
}
在这里,我们添加了另一个类似的文档。
POST school/_doc/16
{
"name":"Crescent School", "description":"State Board Affiliation",
"street":"Tonk Road",
"city":"Jaipur", "state":"RJ", "zip":"176114","location":[26.8535922,75.7923988],
"fees":2500, "tags":["Well equipped labs"], "rating":"4.5"
}
九、创建映射并添加数据
ElasticSearch将根据请求体中提供的数据自动创建映射,我们将使用其批量功能在该索引中添加多个JSON对象
POST /schools/_bulk
{
"index": {
"_index": "schools",
"_id": "1"
}
}
{
"name": "Central School",
"description": "CBSE Affiliation",
"street": "Nagan",
"city": "paprola",
"state": "HP",
"zip": "176115",
"location": [
31.8955385,
76.8380405
],
"fees": 2000,
"tags": [
"Senior Secondary",
"beautiful campus"
],
"rating": "3.5"
}
{
"index": {
"_index": "schools",
"_id": "2"
}
}
{
"name": "Saint Paul School",
"description": "ICSE Afiliation",
"street": "Dawarka",
"city": "Delhi",
"state": "Delhi",
"zip": "110075",
"location": [
28.5733056,
77.0122136
],
"fees": 5000,
"tags": [
"Good Faculty",
"Great Sports"
],
"rating": "4.5"
}
{
"index": {
"_index": "schools",
"_id": "3"
}
}
{
"name": "Crescent School",
"description": "State Board Affiliation",
"street": "Tonk Road",
"city": "Jaipur",
"state": "RJ",
"zip": "176114",
"location": [
26.8535922,
75.7923988
],
"fees": 2500,
"tags": [
"Well equipped labs"
],
"rating": "4.5"
}
需要注意的是:复制的json可能会存在格式问题,可以使用kinna的格式化:
执行结果:
PUT schools_gov
11.1、返回响应{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "schools_gov"
}
11.2、添加其他数据POST schools_gov/_bulk
{
"index":{
"_index":"schools_gov", "_id":"1"
}
}
{
"name":"Model School", "description":"CBSE Affiliation", "street":"silk city",
"city":"Hyderabad", "state":"AP", "zip":"500030", "location":[17.3903703, 78.4752129],
"fees":200, "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
}
{
"index":{
"_index":"schools_gov", "_id":"2"
}
}
{
"name":"Government School", "description":"State Board Affiliation",
"street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
"location": [18.599752, 73.6821995], "fees":500, "tags":["Great Sports"], "rating":"4"
}
同样的需要格式化数据格式!!!
11.3、返回响应{
"took" : 122,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "schools_gov",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "schools_gov",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
]
}