网站目录设计,常州网站推广公司哪家好,网站频道策划,wordpress上传图片插件首先讲下 ES的倒排序索引 创建倒排索引是对正向索引的一种特殊处理#xff0c; 将每一个文档的数据利用算法分词#xff0c;得到一个个词条 创建表#xff0c;每行数据包括词条、词条所在文档id、位置等信息 因为词条唯一性#xff0c;可以给词条创建索引#xff0c;例如…首先讲下 ES的倒排序索引 创建倒排索引是对正向索引的一种特殊处理 将每一个文档的数据利用算法分词得到一个个词条 创建表每行数据包括词条、词条所在文档id、位置等信息 因为词条唯一性可以给词条创建索引例如hash表结构索引 倒排索引的搜索流程 倒排索引的搜索流程如下以搜索华为手机为例
用户输入条件华为手机进行搜索。对用户输入内容分词得到词条华为、手机。拿着词条在倒排索引中查找可以得到包含词条的文档id1、2、3。拿着文档id到正向索引中查找具体文档 正向索引和倒排索引 正向索引
优点
可以给多个字段创建索引
根据索引字段搜索、排序速度非常快
缺点
根据非索引字段或者索引字段中的部分词条查找时只能全表扫描
倒排索引
优点
根据词条搜索、模糊搜索时速度非常快
缺点
只能给词条创建索引而不是字段
无法根据字段做排序 如何在ES上创建索引
首先打开kibanba 进入到控制台然后开始创建索引
之前说过es中索引对应的是mysql的数据库所以我们应该先创建索引
创建索引语法put shooping { acknowledged : true, shards_acknowledged : true, index : shopping } 会返回这几个字段分别代表
acknowledged代表是创建索引创建成功
shards_acjnowledged: 代表所有分片创建成功
index代表索引的名字
然后再次put的会出现这种情况 put是具有幂等性的 { error : { root_cause : [ { type : resource_already_exists_exception, reason : index [shopping/32XXUAigSX-96lF9wV49Aw] already exists, index_uuid : 32XXUAigSX-96lF9wV49Aw, index : shopping } ], type : resource_already_exists_exception, reason : index [shopping/32XXUAigSX-96lF9wV49Aw] already exists, index_uuid : 32XXUAigSX-96lF9wV49Aw, index : shopping }, status : 400 } error表示错误
root_cause表示错误的列表
type表示错误类型
reason错误的原因
index_uuid:唯一标识
index索引名字
如果想用post请求会发送什么post是不具有幂等性的 Incorrect HTTP method for uri [/shopping?prettytrue] and method [POST], allowed: [HEAD, PUT, GET, DELETE],
方法不能使用post直接报错
查询索引语法get shopping { shopping : { aliases : { }, mappings : { }, settings : { index : { creation_date : 1710766914710, number_of_shards : 1, number_of_replicas : 1, uuid : 32XXUAigSX-96lF9wV49Aw, version : { created : 7080099 }, provided_name : shopping } } } }
shopping索引名字
aliases索引别名
mappings意识就是文档结构和字段类型
settings设置索引的信息
index索引级的设置
creation_date创建时间
number_of_shards索引分片的数量
number_of_replicas每个分片的副本数量
uuiduuid
version版本号
provided_name提供的索引名字
查询全部索引语法get _cat/indices?v 表头 含义 health 当前服务器健康状态 green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) status 索引打开、关闭状态 index 索引名 uuid 索引统一编号 pri 主分片数量 rep 副本数量 docs.count 可用文档数量 docs.deleted 文档删除状态逻辑删除 store.size 主分片和副分片整体占空间大小 pri.store.size 主分片占空间大小
删除索引语法delete shopping { acknowledged : true } acknowledged是否成功
创建es数据语法post /shopping/_doc { error : { root_cause : [ { type : parse_exception, reason : request body is required } ], type : parse_exception, reason : request body is required }, status : 400 } 文档对应的是数据库中数据
这个原因就是没有body数据并且我们传递数据是json
试着这种修改一下 { _index : shopping, _type : _doc, _id : iQPEUY4Bonlp-IezIGqE, _version : 1, result : created, _shards : { total : 2, successful : 1, failed : 0 }, _seq_no : 0, _primary_term : 1 }
index索引
type文档
id唯一标识
result结果
shards分配
total分片总数
successful成功数量
failed失败数量
seq_no文档序列号序列号越大标识越新
_primary_term文档所属的主要分片的任期号
当在发送一模一样的请求 id一直在变化如果我们想查询某个数据id太麻烦了如果想自定义呢
创建es数据指定id语法post /shopping/_doc/1001 如果我们指定了id这时候就可以用put因为他是幂等性 主键查询和全局查询语法
主键查询get /shopping/_doc/1001 { _index : shopping, //索引 _type : _doc, //类型 _id : 1001, //id _version : 2, //版本号 _seq_no : 5, //序列号 _primary_term : 1, found : true, //成功 _source : { //信息 name : huyin, age : 21, sex : 男 } }
查询没有的数据 { _index : shopping, _type : _doc, _id : 1002, found : false }
查询全部数据语法get /shopping/_search { took : 0, // 耗时 timed_out : false, // 超时 _shards : { total : 1, successful : 1, skipped : 0, failed : 0 }, hits : { total : { value : 5, relation : eq }, max_score : 1.0, hits : [ //命中 { _index : shopping, _type : _doc, _id : iQPEUY4Bonlp-IezIGqE, _score : 1.0, _source : { name : huyin, age : 21, sex : 男 } }, { _index : shopping, _type : _doc, _id : AQPJUY4Bonlp-IezE2v3, _score : 1.0, _source : { name : huyin, age : 21, sex : 男 }, ] } }
全局修改和局部修改语法
全局修改put /shopping/_doc/1001
put具有幂等性所以可以用 { _index : shopping, _type : _doc, _id : 1001, _version : 3, // 版本号是一直变化的 result : updated, 结果updated 修改 _shards : { total : 2, successful : 1, failed : 0 }, _seq_no : 6, _primary_term : 1 }
局部修改post /shopping/_update/1001 get请求一个get /shopping/_doc/1001
条件查询语法get /shopping/_search 传入body
{query:{ query 查询的意思 match: { match 匹配的意思 name: huyin name 查询那个字段 huyin 查询那个值 } }} 全文查询的语法get /shopping/_search
{query:{ query 查询的意思 match_all: {} match_all 全部查询 }} 分页查询的语法get /shopping/_search
{query:{ match_all: {} }, from:0, from当前页的起始位置 size:2 每页条数 } 筛选出固定的字段get /shopping/_search
{query:{ match_all: {} }, from:0, size:2, _source:[ source 数组里面是可以筛选出固定的字段 name ] } 对数据进行排序post /shoppingtest/_search
{query:{ match_all: {} }, from:0, size:2, sort:{ sort 排序 ------字段--------order排序-----desc倒序/asc(正序) age:{ order:desc } } } 多条件查询的语法get /shoppingtest/_search
{query:{ query 查询 bool:{ bool 条件 must:[ must 必须 {match: {name: huyin}, match 匹配 {match: {age: 20} } ] } } } 如果想要只满足一个的意思匹配数据get /shoppingtest/_search
{query:{ bool:{ should:[ should应该的意思 {match: {name: huyin} }, {match: {name: nihao} } ] } } } 如果想要范围匹配的语法get /shoppingtest/_search
{query:{ bool:{ should:[ {match: {name: huyin} }, {match: {name: nihao} } ], filter: [ filter 过滤 {range: { range范围 age: { age字段 lt: 25, lt小于 gte: 20 gte大于等于 } } } ] } } } 全文检索和完全匹配和高亮查询的操作
全文检索 Es底层会对文本数据保存时进行拆解操作然后保存到倒排索引这种检索方式称为全文检索
完全匹配match_phrase 高亮匹配 highlight:{ 高亮 fields: { 字段属性 name:{} 字段 } } 聚合函数的语法
分组操作
{ aggs:{ 聚合操作 age_group:{ 别名 terms: { 分组 field: age 字段 } } } }
计算平均值
{ aggs:{ age_avg:{ avg: { avg 平均值 field: age } } }, size:0 } 映射关系
首先创建一个索引put /user 创建一个映射关系:put /user/_mapping
{properties:{ 属性 name:{ type:text, type类型 text文本 index:true }, age:{ type:keyword, type类型 keyword代表不能被分词 index:true }, tel:{ type:keyword, index:false index不能被索引 } }} 获取索引信息get /user 创建数据post /user/_doc 查询数据 name 查询数据 age 因为映射的type是keyword不支持分词 查询数据tel index是false 不支持索引查询 es的kibanba的语法结束其实还有很多