索引

集群健康

  1. GET /_cluster/health

创建索引

  1. PUT /index_test
  2. {
  3. "settings": {
  4. "index": {
  5. "number_of_shards": "2",
  6. "number_of_replicas": "0"
  7. }
  8. }
  9. }

查看索引

  1. GET _cat/indices?v

删除索引

  1. DELETE /index_test

mappings 映射

index:默认 true,如果设置为 false,这个字段就不会被索引
analyzer: 指定分词器,中文分词需要安装 ik 分词器

创建索引同时创建 mappings

  1. PUT /index_str
  2. {
  3. "mappings": {
  4. "properties": {
  5. "realname": {
  6. "type": "text",
  7. "index": true
  8. },
  9. "username": {
  10. "type": "keyword",
  11. "index": false
  12. },
  13. "intro": {
  14. "type": "text",
  15. "analyzer": "ik_max_word"
  16. }
  17. }
  18. }
  19. }

查看分词效果

这里索引名为 index_mapping

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

结构一旦创建,不支持修改

需要修改就直接删除

添加

不需要 mappings ,直接 properties

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
  • long, integer, short, byte
  • double, float
  • boolean
  • date
  • object
  • 数组类型一致

    字符串类型说明

  • text: 文字类需要被分词,被倒排索引的内容,比如商品名称,详情,介绍

  • keyword: 不会被分词,不会被倒排索引,直接匹配搜索。比如订单状态,手机号,这类精确匹配,无需分词。

    文档

    添加

    ```json POST /my_doc/_doc/1 -> {索引名}/_doc/{索引ID}

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

{ “id”: 1001, “name”: “text1”, “desc”: “ass 阿达是哒是哒 SD”, “create_date”: “2019-12-24” }

<a name="jacIv"></a>
### 根据文档类型自动设置属性类型
如果索引没有建立 mappings,那么插入文档数据的时候,会根据文档类型自动设置属性类型。<br />这就是 es 的动态映射。

字符串 name 默认生成  以下 mapping
```json
"name": {
    "type": "text",
    "fields": {
        "keyword": {
            "ignore_above": 256,
            "type": "keyword"
        }
    }
}

对一个字段设置多种索引模式,使用 text 类型做全文检索,也可以使用 keyword 类型做聚合和排序

“ignore_above”: 256
设置字段索引和存储的长度最大值,超过此长度会被忽略

删除

DELETE /{索引名}/_doc/{索引ID}

删除文档不会立即将文档从磁盘中删除,只是将文档标记为已删除状态。随着你不断的索引更多的数据,Elasticsearch 将会在后台清理标记为已删除的文档。

修改文档

局部修改
doc 表示修改文档内容

POST /{索引名}/_doc/{索引ID}/_update
{
    "doc": {
        "name": "你好"
    }
}

全局替换

PUT /{索引名}/_doc/{索引ID}
{
    "id": 1001,
    "name": "text1",
    "desc": "ass 阿达是哒是哒 SD哒哒哒哒哒",
    "create_date": "2019-12-25"
}

每次修改后,version 会更改

文档查询

ID 查询

//根据 索引 ID 查询
GET /{索引名}/_doc/{索引ID}

响应

{
    "_index": "my_doc",
    "_type": "_doc",
    "_id": "1002",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "id": 1002,
        "name": "text1",
        "desc": "ass 阿达是哒是哒 SD",
        "create_date": "2019-12-24"
    }
}

全局查询

GET /{索引名}/_search?q=desc:阿&q=name:t

响应

{
    "took": 334,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.52583575,
        "hits": [
            {
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.52583575,
                "_source": {
                    "id": 1002,
                    "name": "text1",
                    "desc": "ass 阿达是哒是哒 SD",
                    "create_date": "2019-12-24"
                }
            }
        ]
    }
}

“_seq_no”: 1, 文档版本号
“_primary_term”: 1, 文档所在位置

指定响应字段查询

GET /{索引名}/_doc/{索引ID}?_source=id,name

这样 _source 只会返回 id 与 name

判断文档是否存在

HEAD /{索引名}/_doc/{索引ID}

文档乐观锁控制

请求操作时,url 参数增加 ?if_seq_no={数值}&if_primary_term={数值}


POST    /{索引名}/_doc/{索引ID}/_update?if_seq_no={数值}&if_primary_term={数值}
{
    "doc": {
        "name": "你好"
    }
}