Elasticsearch搜索引擎学习笔记(三)

索引的一些操作

集群健康

GET     /_cluster/health

创建索引

PUT     /index_test
{
    "settings": {
        "index": {
            "number_of_shards": "2",
            "number_of_replicas": "0"
        }
    }
}

查看索引

GET     _cat/indices?v

删除索引

DELETE      /index_test
 
索引的mappings映射

0. 索引分词概念

index:默认true,设置为false的话,那么这个字段就不会被索引

1. 创建索引的同时创建mappings

PUT     /index_str
{
    "mappings": {
        "properties": {
            "realname": {
            	"type": "text",
            	"index": true
            },
            "username": {
            	"type": "keyword",
            	"index": false
            }
        }
    }
}

2.查看分词效果

GET         /index_mapping/_analyze
{
	"field": "realname",
	"text": "imooc is good"
}

3. 尝试修改

POST        /index_str/_mapping
{
    "properties": {
        "name": {
        	   "type": "long"
        }
    }
}

4. 为已存在的索引创建或创建mappings

POST        /index_str/_mapping
{
    "properties": {
        "id": {
        	"type": "long"
        },
        "age": {
        	"type": "integer"
        },
        "nickname": {
            "type": "keyword"
        },
        "money1": {
            "type": "float"
        },
        "money2": {
            "type": "double"
        },
        "sex": {
            "type": "byte"
        },
        "score": {
            "type": "short"
        },
        "is_teenager": {
            "type": "boolean"
        },
        "birthday": {
            "type": "date"
        },
        "relationship": {
            "type": "object"
        }
    }
}
  • 注:某个属性一旦被建立,就不能修改了,但是可以新增额外属性

主要数据类型

  • text, keyword, string
  • long, integer, short, byte
  • double, float
  • boolean
  • date
  • object
  • 数组不能混,类型一致

字符串

  • text:文字类需要被分词被倒排索引的内容,比如商品名称商品详情商品介绍,使用text。
  • keyword:不会被分词,不会被倒排索引,直接匹配搜索,比如订单状态用户qq微信号手机号等,这些精确匹配,无需分词。
 
文档的基本操作

添加文档数据

POST /my_doc/_doc/1 -> {索引名}/_doc/{索引ID}(是指索引在es中的id,而不是这条记录的id,比如记录的id从数据库来是1001,并不是这个。如果不写,则自动生成一个字符串。建议和数据id保持一致> )

{
    "id": 1001,
    "name": "imooc-1",
    "desc": "imooc is very good, 慕课网非常牛!",
    "create_date": "2019-12-24"
}

{
    "id": 1002,
    "name": "imooc-2",
    "desc": "imooc is fashion, 慕课网非常时尚!",
    "create_date": "2019-12-25"
}

{
    "id": 1003,
    "name": "imooc-3",
    "desc": "imooc is niubility, 慕课网很好很强大!",
    "create_date": "2019-12-26"
}

{
    "id": 1004,
    "name": "imooc-4",
    "desc": "imooc is good~!",
    "create_date": "2019-12-27"
}

{
    "id": 1005,
    "name": "imooc-5",
    "desc": "慕课网 is 强大!",
    "create_date": "2019-12-28"
}

{
    "id": 1006,
    "name": "imooc-6",
    "desc": "慕课是一个强大网站!",
    "create_date": "2019-12-29"
}

{
    "id": 1007,
    "name": "imooc-7",
    "desc": "慕课网是很牛网站!",
    "create_date": "2019-12-30"
}

{
    "id": 1008,
    "name": "imooc-8",
    "desc": "慕课网是很好看!",
    "create_date": "2019-12-31"
}

{
    "id": 1009,
    "name": "imooc-9",
    "desc": "在慕课网学习很久!",
    "create_date": "2020-01-01"
}
  • 注:如果索引没有手动建立mappings,那么当插入文档数据的时候,会根据文档类型自动设置属性类型。这个就是es的动态映射,帮我们在index索引库中去建立数据结构的相关配置信息。
  • “fields”: {“type”: “keyword”}
    对一个字段设置多种索引模式,使用text类型做全文检索,也可使用keyword类型做聚合和排序
  • “ignore_above” : 256
    设置字段索引和存储的长度最大值,超过则被忽略

 

修改文档数据

  局部修改

  POST /my_doc/_doc/1/_update

  {

      "doc": {

          "name": "慕课"

      }

  }

  全量修改

  PUT /my_doc/_doc/1

  {

       "id": 1001,

      "name": "imooc-1",

      "desc": "imooc is very good, 慕课网非常牛!",

      "create_date": "2019-12-24"

  }

 

删除文档数据

  DELETE /my_doc/_doc/1

 

查询文档数据

  常规查询

  GET /index_demo/_doc/1
  GET /index_demo/_doc/_search

 

  元数据

  _index:文档数据所属那个索引,理解为数据库的某张表即可。
  _type:文档数据属于哪个类型,新版本使用_doc。
  _id:文档数据的唯一标识,类似数据库中某张表的主键。可以自动生成或者手动指定。
  _score:查询相关度,是否契合用户匹配,分数越高用户的搜索体验越高。
  _version:版本号。
  _source:文档数据,json格式。

 

  定制结果集

  GET /index_demo/_doc/1?_source=id,name
  GET /index_demo/_doc/_search?_source=id,name

 

  判断文档是否存在

  HEAD /index_demo/_doc/1

 

                             整理自慕课网《java架构师体系课》
 
 

 

posted @ 2020-01-17 15:19  坏猫先生  阅读(280)  评论(0编辑  收藏  举报