多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 一、创建一个索引 *** ~~~ PUT 索引名/~类型名~/文档id { 请求体 } ~~~ ![](http://cloud.ryloo.icu/1c002de2e7db1885865c92cb7fa9cbe3) 以上我们创建了一个新的索引 。这个索引采用的是默认的配置,es 根据存入的文档,自动分析出来文档中字段的类型以及存储方式,这种就是**动态映射**。新的字段通过动态映射的方式被添加到类型映射。现在我们需要对这个建立索引的过程做更多的控制:我们想要确保这个索引有数量适中的主分片,并且在我们索引任何数据*之前*,分析器和映射已经被建立好。 为了达到这个目的,手动创建索引,在请求体里面传入设置或类型映射(这其实就是**静态映射**),如下所示: ~~~ PUT /my_index { "settings": { ... any settings ... }, "mappings": { "type_one": { ... any mappings ... }, "type_two": { ... any mappings ... }, ... } } ~~~ ### 二、索引设置和mapping类型和映射 *** <blockquote class="success">setting配置</blockquote> 开发过程中我们设计到同句同段的检索,所以我们需要创建了一个名称为sentence_paragrah_mapping的char filter,在**传入设置**,它的目的有两个: * 替换`p`,`h1`,`h2`或`\n`标签为统一的分段符:**paragraph**; * 替换中英文`!`,`?`,`。`标点符号为统一的分页符:**sentence**。 ~~~ "settings": { "number_of_replicas": 0, "number_of_shards": 1, "analysis": { "analyzer": { "sentence_paragrah_analyzer": { "type": "custom", "char_filter": [ "sentence_paragrah_mapping" ], "tokenizer": "ik_max_word" } }, "char_filter": { "sentence_paragrah_mapping": { "type": "mapping", "mappings": [ """\n => \u0020sentence\u0020paragraph\u0020 """, """! => \u0020sentence\u0020 """, """? => \u0020sentence\u0020 """, """。 => \u0020sentence\u0020 """, """? => \u0020sentence\u0020 """, """! => \u0020sentence\u0020 """ ] } } } } ~~~ <blockquote class="success">类型映射</blockquote> 除了数据库字段要和es做一一的[类型映射](https://www.cnblogs.com/duanxz/p/5081178.html)外,我们需要全文检索匹配文档的多个字段或全字段,这时我们就要[`copy_to`](https://www.cnblogs.com/sanduzxcvbnm/p/12085057.html)指向一个字段,并配置[analyzer索引分词器和search_analyzer搜索分词器](https://blog.csdn.net/liangwenmail/article/details/112058772)。 * 字符串类型:text、keyword * 数值类型:ling,integer,short,byte,double,float,half float,scaled float * 日期类型:date * te布尔值类型:boolean * 二进制类型:binary ~~~ "mappings": { "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "text", "fields": { "keyword": { "type": "keyword" } } }, "mainContent": { "type": "text", "fields": { "keyword": { "type": "keyword" } }, "analyzer": "sentence_paragrah_analyzer", "search_analyzer": "ik_smart" }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword" } }, "analyzer": "sentence_paragrah_analyzer", "search_analyzer": "ik_smart", "copy_to": "mainContent" } } ~~~ ### 三、获取索引信息 *** ~~~ GET 索引名/~类型名~/文档id ~~~ ![](http://cloud.ryloo.icu/f6f9d6a7b00189895d487d7695579b56) ![](http://cloud.ryloo.icu/8d7036c99e2047f56d7aacc213060458) ### 四、修改索引 *** ~~~ POST 索引名称/_update/文档id ~~~ ![](http://cloud.ryloo.icu/967209d1f101a97cbbc45ea84dd6d0d3) ### 五、删除索引 *** ~~~ DELETE /索引名称/类型名称/文档名称 ~~~ ![](http://cloud.ryloo.icu/7b3ddc2eab50d9bf20406587e538efea) ![](http://cloud.ryloo.icu/0f73ba5673ad01e1b39eaa695503315b)