-
Notifications
You must be signed in to change notification settings - Fork 0
ElasticSearch第四篇 Mapping
yangyp8110 edited this page Jan 17, 2018
·
2 revisions
它类似于静态语言中的数据类型声明,比如声明一个字段为String, 以后这个变量都只能存储String类型的数据。同样的, 一个number类型的 mapping 字段只能存储number类型的数据。
eg : "_ttl": {"enabled": false}
示 ttl关闭,ttl默认就是关闭。
"id": {
"index": "not_analyzed",
"type": "string"
}
指定字段 id不分词,并且类型为 string。
- 首先创建一个索引
[root@yyp ~]# curl -XPUT http://192.168.74.129:9200/productindex
{"acknowledged":true,"shards_acknowledged":true}[root@yyp ~]#
- 没有设置mapping,查看下mapping:
[root@yyp ~]# curl -XGET http://192.168.74.129:9200/productindex/_mapping?pretty
返回结果:
{
"productindex" : {
"mappings" : { }
}
}
从查询的结果可以看到mapping为空 *给productindex这个索引加一个type,type name为product,并设置mapping:
curl -XPOST http://192.168.74.129:9200/productindex/product/_mapping?pretty -d '
{
"product": {
"properties": {
"title": {
"type": "string",
"store": "yes"
},
"description": {
"type": "string",
"index": "not_analyzed"
},
"price": {
"type": "double"
},
"onSale": {
"type": "boolean"
},
"type": {
"type": "integer"
},
"createDate": {
"type": "date"
}
}
}
}
'
返回结果:
{
"acknowledged" : true
}
给productindex加了一个type,并写入了product的mapping信息,查看是否增加成功:
[root@yyp ~]# curl -XGET http://192.168.74.129:9200/productindex/_mapping?pretty
返回结果:
{
"productindex" : {
"mappings" : {
"product" : {
"properties" : {
"createDate" : {
"type" : "date"
},
"description" : {
"type" : "keyword"
},
"onSale" : {
"type" : "boolean"
},
"price" : {
"type" : "double"
},
"title" : {
"type" : "text",
"store" : true
},
"type" : {
"type" : "integer"
}
}
}
}
}
}
- 如果想给product新增一个字段,那么需要修改mapping
[root@yyp ~]# curl -XPOST http://192.168.74.129:9200/productindex/product/_mapping?pretty -d '{
"product": {
"properties": {
"amount":{
"type":"integer"
}
}
}
}'
返回:
{
"acknowledged" : true
}
- 如果要修改一个字段的类型呢,比如onSale字段的类型为boolean,现在想要修改为string类型
[root@yyp ~]# curl -XPOST http://192.168.74.129:9200/productindex/product/_mapping?pretty -d '{
"product": {
"properties": {
"onSale":{
"type":"string"
}
}
}
}'
返回:
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [text]"
}
],
"type" : "illegal_argument_exception",
"reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [text]"
},
"status" : 400
}
为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作,不允许修改类型在我看来是符合lucene机制的。
通过
Elasticsearch-head
(http://192.168.74.129:9100/ )查出存在Elasticsearch
内的内容:
按照官网配置,报错如下:
java.lang.ClassNotFoundException: org.elasticsearch.plugins.ActionPlugin
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_101]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_101]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_101]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_101]
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_101]
感谢来自StackOverFlow的解决方案。新增了
elasticsearch
的依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.0.1</version>
</dependency>
输出查询结果:
2017-02-26 21:09:21.040 INFO 8508 --- [nio-8080-exec-1] c.es.search.controller.SearchController : res:{"_index":"index","_type":"fulltext","_id":"3","_version":1,"found":true,"_source":
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
},getSourceAsString:
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
,index:index
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.es.search.demo</groupId>
<artifactId>es.search.demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目示例地址:es.search.demo