本文主要介绍了ES文档的基本操作
ES是面向文档(document oriented)的,这意味着它可以存储整个对象或文档(document)。然而它不仅仅是存储,还会索引(index)每个文档的内容使之可以被搜索。在ES中,你可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤。
ES使用Javascript对象符号(JavaScript Object Notation),也就是JSON,作为文档序列化格式。JSON现在已经被大多语言所支持,而且已经成为NoSQL领域的标准格式。
_index:索引库,类似于关系型数据库里的“数据库”—它是我们存储和索引关联数据的地方。
_type:类型,类似于关系型数据库中表。可以是大写或小写,不能包含下划线或逗号。我们将使用 employee 做为类型名。
_id:与 _index 和 _type 组合时,就可以在ELasticsearch中唯一标识(类似于主键)一个文档。当创建一个文档,你可以自定义 _id ,也可以让Elasticsearch帮你自动生成。
另外还包括:
_uid:文档唯一标识(_type#_id)
_source:文档原始数据
_all:所有字段的连接字符串
1、插入
es可以指定id存储,也可以不指定id自动生成。自动生成的id是 URL-safe、基于Base64编码且长度为20个字符的GUID字符串。这些GUID字符串由可修改的FlakeID模式生成,这种模式允许多个节点并行生成唯一ID且互相之间的冲突概率几乎为零。
PUT /league/_doc/1
{
"name":"夏侯惇",
"age":26,
"role":"上单",
"tags":["战士","肉"]
}

2、查询数据
2.1 简单查询
GET /league/_doc/1
2.2 返回文档部分数据
GET /league/_doc/1?_source=name,age2.3 只要 _source字段
GET /league/_doc/1?filter_path=_source3、更新数据
3.1 PUT操作全量修改
PUT /league/_doc/1
{
"name":"盖伦",
"age":36,
"role":"上单",
"tags":["战士"]
}3.2 POST+_update局部更新修改
POST /league/_update/1
{
"doc": {
"tags": [
"战士",
"船夫",
"肉"
]
}
}4、搜索
4.1 简单搜索
GET /league/_search?q=name:寒
//q表示query
//字段是name
//匹配的值是寒4.2 复杂搜索
ES比较复杂的是查询操作,包括排序、分页、高亮、模糊查询、精准查询等
- 查询名称包含盖伦的数据
GET /league/_search
{
"query": {
"match": {
"name": "盖伦"
}
}
}
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.4523083,
"hits": [
{
"_index": "league",
"_type": "_doc",
"_id": "1",
"_score": 1.4523083,
"_source": {
"role": "上单",
"name": "盖伦"
}
},
{
"_index": "league",
"_type": "_doc",
"_id": "4",
"_score": 1.2199391,
"_source": {
"role": "上单",
"name": "盖伦1"
}
}
]
}
}hit:包含了索引和文档的信息、查询的结果总数、查询出来的具体的文档、分数(通过分数可以判断哪个更符合)
- 查询结果返回固定字段(类似于mongo中的倒影查询)
GET /league/_search
{
"query": {
"match": {
"name": "盖伦"
}
},
"_source": ["name","role"]
}- 排序
- 根据关键字符合盖伦,age排序,asc:升序,desc:降序
GET /league/_search
{
"query": {
"match": {
"name": "盖伦"
}
},
"sort": [{"age":{"order":"desc"}}]
}
{
"took": 29,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "league",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "盖伦",
"age": 36,
"role": "上单",
"tags": [
"战士"
]
},
"sort": [
36
]
},
{
"_index": "league",
"_type": "_doc",
"_id": "4",
"_score": null,
"_source": {
"name": "盖伦1",
"age": 35,
"role": "上单",
"tags": [
"战士",
"船夫"
]
},
"sort": [
35
]
}
]
}
}
- 分页查询
from:从第几条数据开始,size:返回多少条数据
GET /league/_search
{
"query": {
"match": {
"name": "盖伦"
}
},
"sort": [{"age":{"order":"desc"}}],
"from":0,
"size":1
}- 通过bool进行多条件的匹配查询
must(相当于MySQL中的and),所有条件都要符合,must_not(相当于MySQL中的!=) ,should(相当于MySQL中的or),所有条件或的查询
GET /league/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "盖伦"
}
},
{
"match": {
"age": 22
}
}
]
}
}
}- 通过filter进行过滤查询
GET /league/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "盖伦"
}
}
],
"filter": {
"range": {
"age": {
"gte": 35
}
}
}
}
}
}- 数组匹配查询
数组里的多个匹配条件通过空格隔开即可,只要满足其中一个条件即可被查出
GET /league/_search
{
"query": {
"match": {
"tags": "战士 法师"
}
}
}- 精确查询
term查询是直接通过倒排索引指定的词条进程精确的查找!
关于分词
a. term:查询精确的
b. match:会使用分词器解析(先分析文档,再通过分析的文档进行查询)
两个类型 text keyword
创建testdb索引并插入两条数据,name为text类型,desc为keyword类型。text类型会被当成分词器普通解析,如果是keyword类型则不会解析。
PUT testdb
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"desc":{
"type": "keyword"
}
}
}
}
PUT testdb/_doc/1
{
"name":"刻舟求剑",
"desc":"刻舟求剑desc1"
}
PUT testdb/_doc/2
{
"name":"刻舟求剑",
"desc":"刻舟求剑desc2"
}
GET testdb/_search
{
"query": {
"term": {
"name": "刻"
}
}
}
GET testdb/_search
{
"query": {
"term": {
"desc": "刻舟求剑desc1"
}
}
}


发表评论 取消回复