学习Elasticsearch的笔记,后续还会继续更新,有不正确的地方欢迎指点~~
和mysql结构的区别
badabase -> index(索引)
table -> type(类型)
row -> Document(文档)
column -> Field(字段)
Schema -> Mapping(映射)
Index -> Everything Indexed by default(所有字段都能被索引)
SQL -> Query DSL(专用查询语言)
语法
查看所有索引 ip:端口/_cat/indices?v
get
查所有 "query" : {"match_all" : {}}
查具体 "query" : {
"bool" : { //布尔值查询
"must" : [ /**must必须满足 should或(任意一个满足) must_not必须不满足 */
{
/** 查具体的 不使用分词 match 换成match_phrase*/
"match" : {"indexName" : "value"}
},
{
"match" : {"indexName" : "value"}
}
]
}
}
查多个字段 "multi_match" : { /**这个是"query"的内部条件*/
"query" : "value",
"fields" : ["indexName1","indexName2"]
}
指定字段 "query_string" : {/**这个是"query"的内部条件*/
"query": "要搜索的内容",
"analyzer": "分词器的名称",
"fields": ["indexName1","indexName2"]
}
指定条数 "size" : 1
起始位置 "from" : 0 /**和size搭配实现分页*/
指定返回字段 "_source" : ["name1", "name2"]
查关键字 "term" : {} /**这个是"query"的内部条件*/
范围查询 "range" "gte" "lte" /**这个是"query"的内部条件*/
// gte lte 加了e就是>= or <=,不加e就只是 > or <
"range" : {"indexName" : {"gte" : 5,"lte" : 10}}
排序 "sort" : [
{"indexName1" : {"order" : "desc"}},
{"indexName2" : {"order" : "asc"}}
]
高亮 "higlight" : {"fileds" : {"indexName" : ""}}
//自定义高亮标签
"higlight" : {
"pre_tags" : ["<span style='color:red'>"],
"post_tags" : ["</span>"],
"fileds" : {"indexName" : ""}
}
聚合 "aggs" : { // 聚合操作
"groupName" : { // 分组的名称,自定义的
"terms" : { // 分组
"filed" : "indexName" // 要分组的字段
},
"avg" : { // 平均值p
"filed" : "indexName" // 要计算的字段
}
}
}
前缀查询 "prefix" : {"content": {"indexName":"value"}}// 这是"query"内部条件
通配符查询 "wildcard" : {"content": {"indexName":"value"}}// 这是"query"内部条件
多id查询 "ids" : {"values" : ["id1","id2"]}// 这是"query"内部条件
/**
关键字长度为2,不允许存在模糊0
关键字长度为3-5,允许一次模糊 0 1
关键字长度大于5 允许最大2模糊
*/
模糊查询 "fuzzy" : {"indexName" : "value"}
put
创建索引 ip:端口/indexName
全量更新
优点 保证幂等性
post
不保证幂等性
局部更新
delete
路径上的标识
_doc 代表是文档数据的意思,后面跟插入时的主键id,主键查询
_search 给搜索增加条件
GET [索引名/类型名/]_search?timeout=10ms
GET [索引名/类型名/]_search { 具体条件 }
_mapping 查看索引内字段的类型
GET /索引名/_mapping/类型名
_update 更新数据时在路径后跟上_update
POST [索引名/类型名/行数据的名字]_update {具体内容}
_bulk 批量操作数据
过滤器
// 以下过滤器都是在"filter" : {}里面
term "term" : {
"indexName" : {
"content" : "过滤的值"
}
}
terms "terms" : {
"indexName" : [
"过滤的值",
"过滤的值"
]
}
range "range" : {"indexName" : {"gte" : value,"lte" : value}}
exists "exists" : {"field" : "indexName"}
ids "ids" : {"values" : ["",""]}
分词器
es的内置分词器:
Standard Analyzer:默认分词器,按词切分,小写处理
Simple Analyzer:按照非字母切分(符号被过滤),小写处理
Stop Analyzer:停用词过滤(the,a,is),小写处理
Whitespace Analyzer:按照空格切分,不转小写
Keyword Analyzer:直接将输入当做输出,不分词
Patter Analyzer:正则表达式,默认 \W+(非字符分割)
Language:提供了30多种常见语言的分词器
使用IK分词器
IK分词器有两种分词模式:ik_max_word和ik_smart模式
ik_max_word模式会对文本进行最细粒度的拆分, 比如将华为手机分为华为、手、手机
ik_smart模式是粗粒度的,将华为手机分为华为、手机。
# 默认标准分词器
GET _analyze
{
"analyzer": "standard",
"text": ["我爱北京天安门!","it is so beautiful?"]
}
# ik分词器,粗粒度
GET _analyze
{
"analyzer": "ik_smart",
"text": ["我爱北京天安门!","it is so beautiful?"]
}
# ik分词器,细粒度
GET _analyze
{
"analyzer": "ik_max_word",
"text": ["我爱北京天安门!","it is so beautiful?"]
}
本人也在一步步的学习中,如发现有不对的地方,欢迎指正。
另本人会在私人公众号不定时推送一些知识文章,欢迎各位订阅哦。