ElasticSearch(Settings,Mappings) 叁歲伎倆 2022-06-06 06:37 142阅读 0赞 ## ElasticSearch(Settings,Mappings) ## **1.Setting** 是 针对于索引库而言 可以设置索引库的**分片数量** 和 **副本数量** url 的方式 设置和修改 "settings": { "number_of_shards": 5, "number_of_replicas": 1 api 前面在创建索引库的时候已经讲过了,就不重复了 **2.Mappings**相当于数据库中对字段的类型约束 以及 **某些字段查询时指定分词器** 具体解释请看官网 包含数据类型 (text,keyword,date,long,interger……………) 别的不多说了 看下我创建的mapping "mappings": { "user": { //映射类型 "properties": { //定义字段 "title": { "type": "text",//字段类型 "analyzer": "ik_max_word", "search_analyzer": "ik_max_word"//查询分词 }, "name": { "type": "text" }, "age": { "type": "long" } } }, "blog": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } } } **创建Mapping的javaApi** // 以XContentBuilder形式 XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .startObject("properties") .startObject("name") .field("type", "text") .endObject() .startObject("title") .field("type", "text") .field("analyzer", "ik_max_word") .field("search_analyzer", "ik_max_word") .endObject() .endObject() .endObject(); PutMappingResponse response = client.admin().indices().preparePutMapping() .setIndices("dragon").setType("ccc") .setSource(builder).get(); System.out.println(response.isAcknowledged()); //以json形式: 要注意要指定type setType josn文件: { "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "name": { "type": "text" }, "age": { "type": "long" } }} String json ="{ \"properties\": {\n" + " \"title\": {\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_max_word\",\n" + " \"search_analyzer\": \"ik_max_word\"\n" + " },\n" + " \"name\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"age\": {\n" + " \"type\": \"long\"\n" + " }\n" + " }}"; byte[] bytes = json.getBytes(); PutMappingResponse response = client.admin().indices() .preparePutMapping("dragon").setType("aaa") .setSource(json, XContentType.JSON).get(); System.out.println(response.isAcknowledged()); } 我创建 一个索引库这样来: curl -XPUT 'localhost:9200/索引库名' -d ' { "settings": { "number_of_shards": 5, "number_of_replicas": 1 }, "mappings": { "user": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "name": { "type": "text" }, "age": { "type": "long" } } }, "blog": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } } } } ' //别忘了这个
还没有评论,来说两句吧...