ES的基本概念
- 索引
- 字段类型
- 文档
我们可以用关系型数据库来做个类比:
而ES则是面向文档的,文档是最小的单位,文档又有几个主要属性:
- 自我包含,一篇文档同时包含字段和对应的值,也就是同时包含 key:value!
- 可以是层次型的,一个文档中包含自文档,复杂的逻辑实体就是这么来的! {就是一个json对象!
fastjson进行自动转换!} - 灵活的结构,文档不依赖预先定义的模式,我们知道关系型数据库中,要提前定义字段才能使用,
在elasticsearch中,对于字段是非常灵活的,有时候,我们可以忽略该字段,或者动态的添加一个
新的字段。
还有一个就是分片操作:就是在创建索引的是时候会将其分成多个分片,然后在集群的时候就会存入到不同的节点里面,这样就可以保证在某个节点崩溃了的时候,保证其他节点数据不会丢失。
倒排索引: 看个表格就懂
ES的基本操作
es的基本的rest的操作:
es的api的操作:
创建一个索引:
PUT /test1/type1/1
{
"name": "wcx",
"age": 18
}
这个返回:
这个时候也可以在head里面查看:
当然也可以在head里面创建索引,自己可以尝试一下,然后其他api:
设置类型:
PUT /test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
更新操作:
POST /test3/_doc/1/_update
{
"doc": {
"name": "zyy"
}
}
获取索引的详细信息:
GET _cat/indices
查找Api:
GET /test4/user/1
GET /test4/user/_search?q=name:张三
上面的可以查询文档id为1的文档信息
下面的就是名字叫张三的
test4是索引名—user是type,相当于数据库表–_search操作
较为复杂的查询:
GET /test4/user/_search
{
"query": {
"match": {
"name": "赵倩"
}
},
"_source": ["name", "age"],
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
query:表示查询
match:表示匹配规则
name:表示需要匹配的字段
_source:表示需要返回的字段
sort:排序(都跟数据库很像)
from:表示分页操作里面的当前页(currentPage)
size:表示分页操作里面的当前页记录条数(pageSize)
可以看下结果:
接下来是bool判断:
GET /test4/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "倩"
}
},
{
"match": {
"age": "18"
}
}
]
}
}
}
返回结果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.7261541,
"hits" : [
{
"_index" : "test4",
"_type" : "user",
"_id" : "3",
"_score" : 1.7261541,
"_source" : {
"name" : "吴倩",
"age" : 18,
"tags" : [
"漂亮",
"喜感",
"唱歌"
]
}
}
]
}
}
可以看到这里的分数也有了,这个就是需要满足上面两个条件才能返回!!(and操作)–》(对应的是 must)
接下来看个should(or)操作:
GET /test4/user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "赵倩"
}
},
{
"match": {
"age": "18"
}
}
]
}
}
}
返回值:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.7261541,
"hits" : [
{
"_index" : "test4",
"_type" : "user",
"_id" : "3",
"_score" : 1.7261541,
"_source" : {
"name" : "吴倩",
"age" : 18,
"tags" : [
"漂亮",
"喜感",
"唱歌"
]
}
},
{
"_index" : "test4",
"_type" : "user",
"_id" : "4",
"_score" : 0.60996956,
"_source" : {
"name" : "吴倩123",
"age" : 2,
"tags" : [
"漂亮",
"喜感",
"跳舞"
]
}
}
]
}
}
多关键字匹配和高亮显示:
GET /test4/user/_search
{
"query": {
"match": {
"tags": "漂 棒"
}
}
}
GET /test4/user/_search
{
"query": {
"match": {
"name": "赵倩"
}
},
"highlight": {
"pre_tags": "<p class='key', style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
ES其他基本查询:
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
好了,这是es的部分Api的讲解,更多的可以参阅官网,但是大部分都是常有的,接下来回将集成到SpringBoot。
感谢大家阅读!!