ElasticSearch(Settings,Mappings)

转载:https://blog.csdn.net/hr787753/article/details/78415968
ElasticSearch(Settings,Mappings)
2017年11月01日 16:56:50
阅读数:438
ElasticSearch(Settings,Mappings)
1.Setting 是 针对于索引库而言
可以设置索引库的分片数量 和 副本数量

url 的方式 设置和修改

“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 1
1
2
3
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”
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
创建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());
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
我创建 一个索引库这样来:

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”
}
}
}
}
}
’ //别忘了这个

猜你喜欢

转载自blog.csdn.net/witsmakemen/article/details/80860970