elasticsearc之mapping的介绍

简介:

    为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成全文本(Full-text)或精确的字符串值,Elasticsearch需要知道每个字段里面都包含了什么类型。这些类型和字段的信息存储(包含)在映射(mapping)中。

   

Elasticsearch支持以下简单字段类型:

类型 表示的数据类型
String string
Whole number byteshortintegerlong
Floating point floatdouble
Boolean boolean
Date date

 查看索引的mapping

[root@ELKServer es]# curl -GET "http://192.168.10.49:9200/site_test/test/_mapping"

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
{
"site_test" :
      {
  "mappings" :
       {
   "test" :
        {
    "properties" :
         {
         "@timestamp" :
            {
               "type" : "date" ,
               "format" : "strict_date_optional_time||epoch_millis"
            },
          "count" :
           {
               "type" : "long"
            },
           "brower" :
           {
           "type" : "string"
           }
         }
       }
     }
   }
}

原创地址:http://irow10.blog.51cto.com/2425361/1851001

上面的mapping中我们可以看到这个索引中有三个变量,分别是date类型,long类型和string类型

我们也可以用其他方式查看mapping。用head插件查看索引的索引信息

其中string类型的字段是默认的,考虑到包含全文本,它们的值在索引前要经过分析器分析,并且在全文搜索此字段前要把查询语句做分析处理。也就是说只要定义为string的字段默认会按照一个规则拆分字段。如:hello100 会拆分成 hello  100两个字段。汉字会拆成一个个的。这样在画图是就尴尬了!

下图是elasticsearch中head插件查询到的数据

wKioL1fSJSLBGdX6AAAnb89QWw0822.png

windows 8.1这个字段就是上面mapping对应的brower。类型是string。

我们在kibana的setting查看下site_test各个字段的信息

wKiom1fSJaqCXB2tAAApI6lelzM567.png

string类型的brower在analyzed(分析)这选项中打勾了!

现在我们用kibana画图试试看看会不会有什么异常

wKiom1fSJjzS1y7TAAAhbIwKbdc335.png怎么windows和8.1分成两个字段画图了,这不是我们想要的结果。那该如何解决呢?

自定义mapping

因为重新更改一个索引的mapping,必须新建索引。

先新建一个site_site索引

1
2
[root@ELKServer es] # curl -XPOST http://192.168.10.49:9200/site_test
{ "acknowledged" : true }

把修改好的mapping放入一个文件类:mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
   "test" :
        {
    "properties" :
         {
          "@timestamp" :
                {
               "type" : "date" ,
               "format" : "strict_date_optional_time||epoch_millis"
                 },
           "count" :
                 {
                "type" : "long"
                 },
            "brower" :
                 {
                "type" : "string"
                "index" "not_analyzed"
                 }
           }
       }
   }


对brower字段额外加个属性not_analyzed(不分词)!

1
2
3
4
[root@ELKServer es] # curl -XPOST "http://192.168.10.49:9200/site_test/test/_mapping?pretty" -d '@mapping'
{
   "acknowledged"  true
}

这样就把索引和mapping都定义好了 然后在导入数据就不会出现分词的情况!

备注:如果有多个_type,定义一个就好,其他的会按照定义好的格式去自己扩建mapping。

当然我们也可以把上面两步做到一步到位。如下:

curl -XPUT 'http://192.168.10.49:9200/site_test' -d '@mapping'

上面这条命令能完成建立索引和mapping。



本文转自 irow10 51CTO博客,原文链接:http://blog.51cto.com/irow10/1851001,如需转载请自行联系原作者

相关文章
|
4月前
|
JSON 自然语言处理 定位技术
Elasticsearch Mapping是啥?
Elasticsearch Mapping是啥?
75 0
|
应用服务中间件 PHP nginx
Elasticsearch-PHP库使用报错:No alive nodes found in your cluster[64] in ../Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php
Hyperf Elasticsearch-PHP库使用报错:No alive nodes found in your cluster[64] in ../Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php
321 0
Elasticsearch-PHP库使用报错:No alive nodes found in your cluster[64] in ../Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php
|
Java Docker 容器
cannot set to file:/usr/share/elasticsearch/plugins/ik/elasticsearch-analysis-ik-7.4.2.jar“
cannot set to file:/usr/share/elasticsearch/plugins/ik/elasticsearch-analysis-ik-7.4.2.jar“
cannot set to file:/usr/share/elasticsearch/plugins/ik/elasticsearch-analysis-ik-7.4.2.jar“
|
存储 搜索推荐 关系型数据库
|
存储 Java API
Elasticsearch Index Aliases详解
Elasticsearch Index Aliases详解
Elasticsearch Index Aliases详解
|
自然语言处理 数据库 索引
elasticsearch创建索引 ,mapping,dynamic_templates
elasticsearch 创建索引时通常需要配置mapping。mapping的含义类似于关系数据库中的表结构。但mapping更加灵活。
579 0
|
存储 API 网络架构
Elasticsearch Mapping & Setting
Mapping & Setting 是 Index 最重要的组成部分,Mapping 定义了 Index中的字段名称以及字段类型等信息, Setting 则定义了 Index 分片数量及副本数量等信息,通过了解 Mapping 和 Setting,可以更加合规的去设计 Index。
196 0
Elasticsearch Mapping & Setting
|
索引 Windows 自然语言处理
|
网络协议 数据安全/隐私保护 网络架构