zoukankan      html  css  js  c++  java
  • 索引与类型

    创建ES库

    • 查看所有索引库
    curl 127.0.0.1:9200/_cat/indices
    
    • 创建索引(数据库)
    PUT /my_index
    {
        "settings": { ... any settings ... }
    }
    
    • 创建文章索引库
    // 文章索引
    curl -X PUT 127.0.0.1:9200/articles -H 'Content-Type: application/json' -d'
    {
       "settings" : {
            "index": {
                "number_of_shards" : 3,  # 主分片数
                "number_of_replicas" : 1  # 从数据库数量
               }
       }
    }
    '
    # 查看索引库
    curl 127.0.0.1:9200/_cat/indices
    
    • 删除索引库
    curl -X DELETE 127.0.0.1:9200/articles
    

    类型和映射

    • 概念
      • 类型, 相当于数据库的表
      • 设置类型映射, 相当于描述表结构(字段名称, 字段类型)并建表
    • 字段的类型
      • 字符串: text (在elaticsearch 2.x版本中,为string类型)
      • 整数 : byte, short, integer, long
      • 浮点数: float, double
      • 布尔型: boolean
      • 日期: date

    • 头条项目的文章类型映射
      • _mapping 设置类型映射的接口
      • /article 类型, 对应一张表
      • properties 指定字段名称和类型
        • 以查询为目的建立字段
        • 标题/内容 为用户提供查询使用的字段
        • 文章id/作者id/文章状态/发布时间 主要给后台管理查询使用
        • _all字段默认会包含所有字段的关键词, 比如查询关键词时, 不设置查询条件, 既查询标题也查询内容, 则可以使用__all字段查询
        • include_in_all则是设置该字段的关键词是否加入到_all字段的关键词中
          • user_id, article_id不加入_all, 这样用户查询时, 可以直接查询__all字段
          • 后台查询,可以根据需求进行查询
        • analyzer 分析器设置, 只对字符串类型(text)有效
        • boost 设置相关性排序的权重 整数形式, 尽量控制在10以内
    curl -X PUT 127.0.0.1:9200/articles/_mapping/article -H 'Content-Type: application/json' -d'
    {
         "_all": {
              "analyzer": "ik_max_word"
          },
          "properties": {
              "article_id": {
                  "type": "long",
                  "include_in_all": "false"
              },
              "user_id": {
                  "type": "long",
                  "include_in_all": "false"
              },
              "title": {
                  "type": "text",
                  "analyzer": "ik_max_word",
                  "include_in_all": "true",
                  "boost": 2
              },
              "content": {
                  "type": "text",
                  "analyzer": "ik_max_word",
                  "include_in_all": "true"
              },
              "status": {
                  "type": "integer",
                  "include_in_all": "false"
              },
              "create_time": {
                  "type": "date",
                  "include_in_all": "false"
              }
          }
    }
    '
    
    • 查看映射
    curl 127.0.0.1:9200/articles?pretty  # 查询整个索引库结构
    curl 127.0.0.1:9200/articles/_mapping/article?pretty  # 查询article表的结构
    # 不设置-X默认为GET
    

    修改索引库的类型映射

    • 可以增加字段
    • 不能修改已有字段的类型(索引的建立和类型有关)
      • 只能建立新的库, 重新进行类型映射
      • 好处是不需要将数据再导入到新的索引库, 只需要重新索引数据
    # 创建新的索引库 5.x版本分别设置配置和类型映射
    curl -X PUT 127.0.0.1:9200/articles_v2 -H 'Content-Type: application/json' -d'
    {
       "settings" : {
          "index": {
              "number_of_shards" : 3,
              "number_of_replicas" : 1
           }
       }
    }
    '
    
    curl -X PUT 127.0.0.1:9200/articles_v2/_mapping/article -H 'Content-Type: application/json' -d'
    {
         "_all": {
              "analyzer": "ik_max_word"
          },
          "properties": {
              "article_id": {
                  "type": "long",
                  "include_in_all": "false"
              },
              "user_id": {
                   "type": "long",
                  "include_in_all": "false"
              },
              "title": {
                  "type": "text",
                  "analyzer": "ik_max_word",
                  "include_in_all": "true",
                  "boost": 2
              },
              "content": {
                  "type": "text",
                  "analyzer": "ik_max_word",
                  "include_in_all": "true"
              },
              "status": {
                  "type": "byte",
                  "include_in_all": "false"
              },
              "create_time": {
                  "type": "date",
                  "include_in_all": "false"
              }
          }
    }
    
    # 重新索引数据
    curl -X POST 127.0.0.1:9200/_reindex -H 'Content-Type:application/json' -d '
    {
      "source": {
        "index": "articles"
      },
      "dest": {
        "index": "articles_v2"
      }
    }
    '
    
    • 起别名

      • 如果修改索引库, 代码中的库名称也要对应修改, 为了避免代码的改动, 可以给新的索引库起别名, 让其使用原库的名称
      • 注意先删除原库, 避免出现名称冲突
    curl -X DELETE 127.0.0.1:9200/articles  # 先删除原索引库
    curl -X PUT 127.0.0.1:9200/articles_v2/_alias/articles  # 给索引库起别名, 设置为原索引库的名称
    
    • 查询索引别名
    # 查看别名指向哪个索引
    curl 127.0.0.1:9200/*/_alias/articles
    
    # 查看哪些别名指向这个索引
    curl 127.0.0.1:9200/articles_v2/_alias/*
    
  • 相关阅读:
    docker知识复习
    记住left join最简单的方式(转)
    ingress rewrite 配置文件的结构 + server_name和location这两条指令的配置来匹配要 虚拟主机
    PAM unable to dlopen(/lib/security/pam_limits.so): /lib/security/pam_limits.so: wrong ELF class: ELFCLASS32
    iRedMail退信问题的解决(转)
    curl NSS error -8179 (SEC_ERROR_UNKNOWN_ISSUER)
    看进程的启动时间长度 + vmstat + jstack 应用
    Nginx日志中的金矿
    php-fpm参数优化
    PHP扩展--opcache安装及配置
  • 原文地址:https://www.cnblogs.com/oklizz/p/11443274.html
Copyright © 2011-2022 走看看