elastcisearch7.6基本使用

本文详细介绍了Elasticsearch的基本操作,包括启动、访问、索引创建、字段类型区别、查询方式、更新索引、分词分析及高亮显示等。此外,还讲解了IK分词器的使用,尤其是ik_max_word和ik_smart模式的差异。通过实例展示了如何在Elasticsearch中高效管理和检索数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。也就是说对文本分词的时候要拆分的足够详细,而查询的时候粒度不用太细。

同时也可以自定义分词,以及热更新分词。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值