Elasticsearch笔记(4)

这是我参与8月更文挑战的第14天,活动详情查看:8月更文挑战

ES操作(中)

索引的mappings映射

索引分词概念

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

创建索引的同时创建mappings
#PUT     /{索引名}(自定义)
#PUT     http://192.168.123.64:9200/index_jacquesh
{
    "mappings": {
        "properties": {
            "username": {
                "type": "text",
                "index": true
            },
            "realname": {
                "type": "keyword",
                "index": false
            }
        }
    }
}
#返回
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index_mapping"
}
#创建索引成功
​
复制代码
mappings与setting一起使用
# PUT http://192.168.123.64:9200/{索引名}
{
    "mappings": {
        "properties": {
            "username": {
                "type": "text",
                "index": true
            },
            "realname": {
                "type": "keyword",
                "index": false
            }
        }
    },
     "settings": {
        "index": {
            "number_of_shards": "2", 
            "number_of_replicas": "0"
    }
}
}
##返回
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index_mapping_and_setting"
}
##创建成功
​
复制代码
查看分词效果
#GET         /{索引名}/_analyze
#GET         http://192.168.123.64:9200/index_mapping
{
    "field": "username",
    "text": "my name is jacquesh"
}
##返回
{
    "tokens": [
        {
            "token": "my",
            "start_offset": 0,
            "end_offset": 2,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "name",
            "start_offset": 3,
            "end_offset": 7,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "is",
            "start_offset": 8,
            "end_offset": 10,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "jacquesh",
            "start_offset": 11,
            "end_offset": 19,
            "type": "<ALPHANUM>",
            "position": 3
        }
    ]
}
##可以看出是经过分词器处理的
复制代码
修改索引映射
#POST http://192.168.123.64:9200/{索引名}/_mapping
{
    "properties": {
        "username": {
               "type": "long"
        }
    }
}
##返回(报错)
{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "mapper [username] cannot be changed from type [text] to [long]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "mapper [username] cannot be changed from type [text] to [long]"
    },
    "status": 400
}
###注意->某个属性一旦被建立,就不能修改了,但是可以新增额外属性
如下:
{
    "properties": {
        "sex": {
               "type": "integer"
        }
    }
}
#返回
{
    "acknowledged": true
}
###新增加成功
​
​
复制代码
主要数据类型
  • text, keyword, string
  • long, integer, short, byte
  • double, float
  • boolean
  • date
  • object
  • 数组不能混,类型一致
字符串
  • text:文字类需要被分词被倒排索引的内容,比如商品名称商品详情商品介绍,使用text
  • keyword:不会被分词,不会被倒排索引,直接匹配搜索,比如订单状态用户qq微信号手机号等,这些精确匹配,无需分词

文档的基本操作

添加文档数据

# POST /{索引名}/_doc/{索引ID}
# 
(是指索引在es中的id,而不是这条记录的id,比如记录的id从数据库来是1001,并不是这个。如果不写,则自动生成一个字符串。<建议和数据id保持一致>)
## POST  http://192.168.123.64:9200/index_jacquesh/_doc/1001
{
    "id": 1001,
    "name": "jacuqesh",
    "desc": "jacuqesh is very good,jacuqesh非常牛!",
    "create_date": "2020-09-04"
}
##返回
{
    "_index": "index_jacquesh",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 2
}
##创建成功
接下来以此再新增加2条数据
## POST  http://192.168.123.64:9200/index_jacquesh/_doc/1002
{
    "id": 1002,
    "name": "jack",
    "desc": "jack is very nice,jack非常好!",
    "create_date": "2020-09-05"
}
## POST  http://192.168.123.64:9200/index_jacquesh/_doc/1003
{
    "id": 1003,
    "name": "huang",
    "desc": "huang 是广东吴彦祖!",
    "create_date": "2020-09-15"
}
​
​
复制代码

查看index信息:可以看出mappings自动创建了

image-20200904170457521.png

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

删除文档

# DELETE   /{索引名}/_doc/{索引ID}
# DELETE   http://192.168.123.64:9200/index_jacquesh/_doc/1001
​
##返回
{
    "_index": "index_jacquesh",
    "_type": "_doc",
    "_id": "1001",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 2
}
##删除成功
复制代码
  • 注:文档删除不是立即删除,文档还是保存在磁盘上,索引增长越来越多,才会把那些曾经标识过删除的,进行清理,从磁盘上移出去。

    可以看出_id=1001的已经被删除

    image-20200904171448189

修改文档

  • 局部:

    # POST  /{索引名}/_doc/{索引ID}/_update
    # POST  http://192.168.123.64:9200/index_jacquesh/_doc/1002/_update
    {
        "doc": {
            "name": "huangkunhuang"
        }
    }
    ##返回
    {
        "_index": "index_jacquesh",
        "_type": "_doc",
        "_id": "1002",
        "_version": 2,
        "result": "updated",
        "_shards": {
            "total": 1,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 2
    }
    ##修改成功
    复制代码
  • 全量替换:

    # PUT /{索引名}/_doc/{索引ID}
    {
         "id": 1002,
        "name": "黄某人",
        "desc": "黄某人实在是代码写的太好了!",
        "create_date": "2020-12-24"
    }
    ## 返回
    {
        "_index": "index_jacquesh",
        "_type": "_doc",
        "_id": "1002",
        "_version": 3,
        "result": "updated",
        "_shards": {
            "total": 1,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 2
    }
    ##更新成功
    ​
    复制代码
  • 注:每次修改后,version会更改

查询文档

常规查询

//第一种 
# GET  /{索引名}/_doc/{索引ID}
# http://192.168.123.64:9200/index_jacquesh/_doc/1002
​
返回
{
    "_index": "index_jacquesh",
    "_type": "_doc",
    "_id": "1002",
    "_version": 3,
    "_seq_no": 2,
    "_primary_term": 2,
    "found": true,
    "_source": {
        "id": 1002,
        "name": "黄某人",
        "desc": "黄某人实在是代码写的太好了!",
        "create_date": "2020-12-24"
    }
}
//第二种 
# GET   /{索引名}/_doc/_search
# GET    http://192.168.123.64:9200/index_jacquesh/_doc/_search
{
    "query" : {
    "match" :{
        "_id":1002
     }
   }
}
返回
{
    "took": 136,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "index_jacquesh",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.0,
                "_source": {
                    "id": 1002,
                    "name": "黄某人",
                    "desc": "黄某人实在是代码写的太好了!",
                    "create_date": "2020-12-24"
                }
            }
        ]
    }
}
​
复制代码

\