Elasticsearch 8.x 遇到的问题
ES 问题:
转发:https://www.cnblogs.com/codename-h/p/15928886.html
-----------------------------------------------------------------------------------------
ES 主要分index索引 和 文档 两部分
当保存文档数据时,ES会将数据文字进行分词拆解操作,并将拆解后的数据保存在 倒排索引 当中,这样,即使使用value中的部分数据也能查询出数据,这种检索方式成为 全文检索!
-----------------------------------------------------------------------------------------
put -> localhost:9200/index01 创建索引
post / put -> localhost:9200/index01/_doc 向index索引中添加数据(数据id会随机生成)
post / put -> localhost:9200/index01/_doc/1001 向index索引中添加数据(指定id,id相同不论post/put几次,就只创建一条记录)
get -> localhost:9200 查看ES信息
get -> localhost:9200/index01/_doc/1001 查看索引数据(指定id)
get -> localhost:9200/index01/_search 查看索引全部数据
put 具有幂等性,而post不具有幂等性
put -> localhost:9200/index01/_doc/1001 全量更新数据
post -> localhost:9200/index01/_update/1001 局部更新数据 {doc:{key:value}}
delete -> put -> localhost:9200/index01/_doc/1001 删除指定数据
分页查询(form页数,size每页条数) form = (页码-1 )* size
get -> localhost:9200/index01/_search {"query":{"match_all":{}},"form":1,"size":2}
get -> localhost:9200/index01/_search {"query":{"match_all":{}},"form":1,"size":2,"sort":{"price":{"order":"desc"}}} sort排序
get -> localhost:9200/index01/_search {"query":{"match_all":{}},"_key":['key']} _key指定显示key
多条件查询
get -> localhost:9200/index01/_search
{
"query" : {
"bool" : { // --> 条件
"must" : [{ // must 相当于 and , should 相当于 or
"match" : {
"name" : "xiaoming"
},
"match" : {
"age" : "16"
},
}],
"filter" : { // --> 过滤
"range" : { // --> 范围
"age" : {
"gt" : 20 // --> 大于20
}
}
}
}
}
}
-----------------------------------------------------------------------------------------
全文检索
get -> localhost:9200/index01/_search
{
"query" : {
"match" : { // --> 全文检索匹配
"name" : "小华" // --> 这里ES会把 小华 进行分词操作,把小华按照“小”和“华”来查询
},
}
}
{
"query" : {
"match_phrase" : { // --> 完全匹配
"name" : "小华" // --> 小华作为一个整体来查询
},
“highlight” : { // --> 高亮显示
"fields" : {
"name" : {] //--> 会把对应的name高亮显示,默认在value前后添加<em>
}
}
}
}
-----------------------------------------------------------------------------------------
聚合索引
get -> localhost:9200/index01/_search
{
"aggs" : { //--> 聚合操作
"age_group" : { //--> 名称,随意起名
"terms" : { //--> 分组
"field" : "age" //--> 分组字段
}
},
"size" : 0 //--> 先显示统计结果,不显示元数据
}
}
{
"aggs" : { //--> 聚合操作
"age_avg" : { //--> 名称,随意起名
"avg" : { //--> 平均值
"field" : "age" //--> 平均值字段
}
},
"size" : 0 //--> 先显示统计结果,不显示元数据
}
}
-----------------------------------------------------------------------------------------
在ES中,有些value可以分词查询,但有些value则必须完全匹配!那么是否分词,我们如何来界定和控制呢?
在mysql中,一个字段的类型,长度都属于表结构信息,在ES中也有类似的概念,我们称之为 映射!
put -> localhost:9200/index01/_mapping
{
"properties" : {
"name" : {
"type" : "text", // --> 可以被分词
"index" : true // --> 可以索引查询
},
"sex" : {
"type" : "keyword", // --> 关键字,不可以分词
"index" : true // --> 可以索引查询
},
"telohone" : {
"type" : "keyword", // --> 不可以分词
"index" : false // --> 不可以索引,不支持当作条件来查询
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)