Elasticsearch基础 es索引及查询
es与关系数据库的对比,方便理解es关系型数据库indexdatabasetypetabledocumentrow (行数据)fieldcolumn (列数据)传统关系型数据库中,我们是把数据存入一个database里的某个table中,es可以理解为一个文档类型的数据库,每一条数据,就相当于一个文档,所以index用以检索文档,type就相当于table,确定文档位置,就相当于确定这条数据在哪个
·
es与关系数据库的对比,方便理解
es | 关系型数据库 |
---|---|
index | database |
type | table |
document | row (行数据) |
field | column (列数据) |
传统关系型数据库中,我们是把数据存入一个database里的某个table中,es可以理解为一个文档类型的数据库,每一条数据,就相当于一个文档,所以index用以检索文档,type就相当于table,确定文档位置,就相当于确定这条数据在哪个table中。document就相当于一行数据,field相当于一列数据。
es查询方式
PUT操作
put请求相当于insert与update操作,可以插入或更新数据。
例:
下面展示一些 内联代码片
。
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
此时插入了一条index为megacorp,type为employee,id为1的一条数据。
当这条数据存在时,会更新,相当于update操作。
GET操作
根据id查询
GET /megacorp/employee/1
得到的结果
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
}
查询全部数据
GET /megacorp/employee/_search
得到的结果
{
"took": 6,
"timed_out": false,
"_shards": { ... },
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "2",
"_score": 1,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}
根据参数过滤
GET /megacorp/employee/_search?q=last_name:Smith
得到的结果
{
...
"hits": {
"total": 2,
"max_score": 0.30685282,
"hits": [
{
...
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
},
{
...
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}
使用DSL语句查询(更灵活的查询)
在json结构体中写查询语句query
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
下篇文章中,重点写写DSL查询语句的使用。
更多推荐
已为社区贡献1条内容
所有评论(0)