文章目录
es集群相关概念:
https://www.zhihu.com/question/26446020
启动、访问
https://juejin.im/post/6844904168835006477#heading-2
下载好统一的软件版本。
然后启动。。。。
keyword和text类型的区别
字段为text的会被分词,而keyword不会被分词器进行分词。
term和match的区别
term会根据倒排索引进行精准匹配,而match会将条件进行分词后再进行精准匹配。
备注:match并不是模糊匹配,仅仅是被分词!!!!!,match和term相比也仅仅多了一个分词效果而已!!!!!
创建索引
// 推荐用这个,规范
PUT /test2
{
"mappings": {
"properties": {
"content":{"type": "text","analyzer": "ik_smart"},
"host_name":{"type": "text"},
"name":{"type": "keyword"}
}
}
}
PUT /test1/_doc/2
{
"name":"k1",
"age":22
}
获取索引基本信息
GET test1
查询索引
GET test1/_search
# 获取到总数
get /test1/_search
{
"track_total_hits": true
}
get /test2/_search
{
"query":{
"match": {
"content": "新电1"
}
}
}
-- 通配符的使用,模糊查询,name是keyword类型才行。
get /test2/_search
{
"query":{
"wildcard": {
"name": "*德华2*"
}
}
}
更新/新增索引字段、值
POST test1/_update_by_query
{
"query":{
"match":{
"_id":2
}
},
"script":{
"source": "ctx._source.age2 = 14"
}
}
查看分词效果
GET _analyze
{
"analyzer": "ik_smart",
"text": "于场内交易市"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text": "钢结构工程施工"
}
// 查看某个索引的字段的分词效果
GET test2/_analyze
{
"field": "host_name",
"text": "刘德华的电影"
}
高亮显示
get /examda_news_es2/_search
{
"query" :{
"match": {
"content":{
"query": "于场内交易市",
"analyzer": "ik_smart"
}
}
},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
创建一个动态模板
PUT _template/template_2
{
"index_patterns": [ "examda_news_es2" ],
"settings": {
"number_of_shards": 1
},
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"content": {
"type": "text", "analyzer": "ik_max_word","search_analyzer": "ik_smart"
}
}
}
}
删除索引
delete test2
ik分词器
https://github.com/medcl/elasticsearch-analysis-ik
elastcisearch默认有自己的分词器,但是对中文并不友好,会把中文分割成一个一个词进行分词。
而ik分词器对中文分词十分友好,ik提供两种模式的分词,分别是ik_max_word 和 ik_smart。
ik_max_word 和ik_smart区别
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合,适合 Term Query;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”,适合 Phrase 查询
es中一般情况下,analyzer用ik_max_word,而search_analyzer用ik_smart。也就是说对文本分词的时候要拆分的足够详细,而查询的时候粒度不用太细。
同时也可以自定义分词,以及热更新分词。