# 一、索引管理 - es所有的命令都是通过http完成的 - es的和传统的数据库对于规则如下: ``` db es 数据库 索引 表 类型type 行 文档document 列 字段field 表结构schema 映射mapping 索引 全文索引 sql http接口 ``` - Text:会分词,然后进行索引,支持模糊、精确查询,不支持聚合 - keyword: 不分词,直接索引,支持模糊、精确查询,支持聚合 ## 1.索引的增加 - put blog,索引必须全部小写,es7过后取消type概念,默认为_doc ```json { "settings": { "number_of_shards": 1 }, "mappings": { "properties": { "id": { "type": "long" }, "content": { "type": "text" }, "postTime": { "type": "date" }, "title": { "type": "text" } } } } ``` ## 2.删除 - delete blog - 全部删除 ``` curl -X DELETE http://localhost:9200/_all ``` ## 3.改 - put blog/_settings ```json { "blocks.read": true } ``` ``` blocks.read,禁止对当前索引进行读操作 blocks.read_only,只允许读,不允许写 blocks.write,禁止写操作 ``` - put blog/_mappings ```json { "properties": { "id": { "type": "text" }, "content": { "type": "text" }, "postTime": { "type": "date" }, "title": { "type": "text" }, "aaa": { "type": "text" } } } ``` ## 4.查 - 查询全部的索引 ``` curl -X GET http://localhost:9200/_cat/indices ``` - get blog/_settings - get blog/_mappings ## 5.索引打开和关闭 - post blog/_close,已经关闭的索引不能进行任何读写操作 - post blog/_open - post _all/_close - post _all/_open,全部打开 # 二、文档管理 ## 1.增加文档 - put blog/_doc/1,index/type/id ```json { "id": 1, "title": "Java讲义", "postTime": "2019-06-09", "content": "全功能的一门编程语言" } ``` - post blog/_doc,这种方式id会随机生成 ```json { "id": 3, "title": "Java讲义3", "postTime": "2019-06-09", "content": "全功能的一门编程语言3" } ``` ## 2.修改文档 - put blog/_doc/1 ```json { "title": "Java讲义bbb", "postTime": "2019-06-09", "content": "全功能的一门编程语言bbb" } ``` ## 3.删除文档 - delete blog/_doc/1 ## 4.查询文档 - get blog/_doc/1 ## 5.路由机制 - shard = hash(routing) % shard_num ``` routing默认为文档id,也可以指定routing,这样可以避免查询的时候广播查询,如下 get blog/article/1?routing=jay,routing值固定的话,可以让查询集中在一个片上 ``` # 三、映射 - get blog/_mapping,查看映射信息,默认是动态映射 - put blog,创建索引时就添加静态映射,添加更精准,更详细的配置信息 ``` boost为权重权重,默认为1 format匹配的格式 keyword不会被分词,ignore_above和keyword一起使用,指定分词和索引的最大长度,超过的部分不会被索引 index是否进行到排序索引,false不进行索引,也就不可搜索,默认开启,动态mapping解析出来为数字类型、布尔类型的字段除外 doc_values默认开启,会增加一个列存储索引,对于不需要排序和聚合的字段可以关闭 similarity默认使用BM25评分模型 { "settings": { "number_of_shards": 1 }, "mappings": { "properties": { "id": { "type": "long" }, "title": { "type": "keyword", "ignore_above": 20 }, "content": { "type": "text", "boost": 2, "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "postTime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss", "doc_values": true, "index": false } } } } ``` - post _bulk,批量插入,一个document必须在一行,而且还要换行,下面的结构不要改 ``` {"index":{ "_index": "blog", "_type": "_doc", "_id": "1" }} {"id": 1, "title": "Java讲义", "postTime": "2017-06-09 12:30:00", "content": "全功能的一门编程语言,既可以做前端,也可以做后端"} {"index":{ "_index": "blog", "_type": "_doc", "_id": "2" }} {"id": 2, "title": "精通C#", "postTime": "2018-06-09 12:30:00", "content": "大部分用来在windows上做前端"} {"index":{ "_index": "blog", "_type": "_doc", "_id": "3" }} {"id": 3, "title": "Go实战", "postTime": "2019-06-09 12:30:00", "content": "Google全力主推的一门语言"} ``` # 四、复杂查询 - get/post,blog/_search或者/_search查询全部 ```json { "query": { "match_all": {} } } ``` - get,/_search,分页查询,_source指定需要返回的字段,version默认不返回,min_score过滤最低的分数,highlight高亮显示,不指定sort默认用评分排序 ```json { "from": 0, "size": 100, "query": { "term": { "content": "前端" } }, "_source": [ "title", "content" ], "version": true, "min_score": 0.6, "highlight": { "fields": { "content": { "pre_tags": [ "" ], "post_tags": [ "