QQ截图20220420083356.jpg

CRUD

查询

  1. get(id查询):192.168.136.133:9200/student/_doc/100
    1. 查看索引库:get student
    2. 查询数据:get student_search
  2. post(查询所有):192.168.136.133:9200/student/_doc/100/_search

    创建

  3. post(随机id):192.168.136.133:9200/student/_doc

  4. put(指定id):192.168.136.133:9200/student/_doc/100 当id存在时,会替换原id下的数据,相当于修改

    修改

  5. post:192.168.136.133:9200/student/300/_update

    删除

    delete:192.168.136.133:9200/student/_doc/100

类型

字段类型:long、integer、float、double、byte、keywordtext

创建索引,指定文档类型

  1. put student
  2. {
  3. "mappings":{
  4. "properties":{
  5. "name":{
  6. "type":"text"
  7. },
  8. "birthday":{
  9. "type":"date"
  10. },
  11. "age":{
  12. "type":"integer"
  13. }
  14. }
  15. }
  16. }

查询名称

默认单字分词

  1. get student/_search
  2. {
  3. "query":{
  4. "match":{
  5. "name": "丽"
  6. }
  7. }
  8. }

不分词

  1. get student/_search
  2. {
  3. "query":{
  4. "term": {
  5. "name":"小华"
  6. }
  7. }
  8. }

分页

  1. # from 从第一条数据开始
  2. # size 返回2条数据
  3. get student/_search
  4. {
  5. "from":0,
  6. "size":2
  7. }

分词器

安装分词器

  1. 进入容器:docker exec-it elasticsearch /bin/bash
  2. 创建文件夹:mkdir /usr/share/elasticsearch/plugins/ik
  3. 把压缩包复制到ik下:docker cp /opt/elasticsearch-analysis-ik-7.16.3.zip elasticsearch:/usr/share/elasticsearch/plugins/ik/
  4. 进入ik目录解压
    1. 进入容器:docker exec-it elasticsearch /bin/bash
    2. 进入ik:cd /usr/share/elasticsearch/plugins/ik
    3. 解压:unzip elasticsearch-analysis-ik-7.16.3.zip
  5. 删除压缩包:rm -rf elasticsearch-analysis-ik-7.16.3.zip
  6. 重启容器:docker restart elasticsearch

    指定分词类型

    1. put student
    2. {
    3. "mappings":{
    4. "properties":{
    5. "name":{
    6. "type":"text",
    7. "analyzer":"ik_max_word"
    8. },
    9. }
    10. }
    11. }

    常用分词方式

  7. 单字分词:standard

  8. 最大分词:ik_max_word
  9. 最小分词:ik_smart

    查看分词方式

    1. get _analyze
    2. {
    3. "analyzer":"ik_max_word",
    4. "text":"我是中国人"
    5. }

    分词搜索

    按设定分词方式搜索

    1. get person/_search
    2. {
    3. "query":{
    4. "match":{
    5. "name":"国人"
    6. }
    7. }
    8. }

    条件整体搜索

    1. get person/_search
    2. {
    3. "query":{
    4. "term":{
    5. "name":"国人"
    6. }
    7. }
    8. }

    模糊搜索

    1. get person/_search
    2. {
    3. "query":{
    4. "wildcard":{
    5. "name":"中*"
    6. }
    7. }
    8. }