一、Mapping介绍

Elasticsearch中,Mapping是什么?

mappingElasticsearch中的作用就是约束。

1.数据类型声明

它类似于静态语言中的数据类型声明,比如声明一个字段为String, 以后这个变量都只能存储String类型的数据。同样的, 一个number类型的mapping字段只能存储number类型的数据。

2.Mapping它定义了 Type 的属性。

"_ttl": {"enabled": false}

表示 ttl关闭,其实ttl默认就是关闭。

3.指定分词器。

"id": {
    "index": "not_analyzed",
    "type": "string"
}

指定字段 id不分词,并且类型为 string

二、创建Mapping

1.下面介绍一下HTTP的创建方式。我一般用Java 创建方式。

PUT http://123.123.123.123:9200/index/type/
{
  "settings": {
     //设置10个分片,理解为类似数据库中的表分区中一个个分区的概念,不知道是否妥当
     "number_of_shards": 10
  },
  "mappings": {
    "trades": {
      "_id": {
        "path": "id"
      },
      "properties": {
        "id": {
         "type": "integer",
        //id:自增数字
        //要求:查询
         "store" : true
        },
        "name": { //名称
         "type": "string"
        },
        "brand": { //品牌: PG,P&G,宝洁集团,宝洁股份,联想集团,联想电脑等
          "type": "string"
        },
        "orderNo": { //订单号 :如ATTS000928732
          "type": "string",
          "index":  "not_analyzed"
        },
        "description": {
              //描述: 2015款玫瑰香型强生婴儿沐浴露,550ml,包邮
              "type": "string",      
               "sort": true
        },
        "date": {
          "type": "date"
        },
        "city": {
          "type": "string"
        },
        "qty": {// index分词无效
            "type": "float"
        },
        "price": {
              //价格: float index无效
             "type": "float"
        }
      }
    }
  }
}

2.Java方式创建。

构建Mapping

package com.sojson.core.elasticsearch.mapping;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.io.IOException;

import org.elasticsearch.common.xcontent.XContentBuilder;

public class ZhidaoMapping {

public static XContentBuilder getMapping(){
        XContentBuilder mapping = null;
        try {
            mapping = jsonBuilder()
            .startObject()
            //开启倒计时功能
            .startObject("_ttl")
                .field("enabled",false)
                .endObject()
                    .startObject("properties")
                    .startObject("title")
                        .field("type","string")
                    .endObject()
                    .startObject("question")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("answer")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("category")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("author")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("date")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("answer_author")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("answer_date")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("description")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("keywords")
                        .field("type","string")
                        .field("index","not_analyzed")
                    .endObject()
                    .startObject("read_count")
                        .field("type","integer")
                        .field("index","not_analyzed")
                    .endObject()
                    //关联数据
                    .startObject("list").field("type","object").endObject()
                .endObject()
            .endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return mapping;
    }
}

创建Mapping

public static void createBangMapping(){
    PutMappingRequest mapping = Requests.putMappingRequest(INDEX).type(TYPE).source(ZhidaoMapping.getMapping());
    ESTools.client.admin().indices().putMapping(mapping).actionGet();
}

创建的时候,需要 index已经创建才行,要不然会报错。

//构建一个Index(索引)CreateIndexRequest request = new CreateIndexRequest(INDEX);
ESTools.client.admin().indices().create(request);

创建完毕在Head插件里查看或者Get请求。

http://123.123.123.123:9200/index/type/_mapping

得到的结果:

{
    "zhidao_index": {
        "mappings": {
            "zhidao_type": {
                "_ttl": {
                    "enabled": false
                },
                "properties": {
                    "answer": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "answerAuthor": {
                        "type": "string"
                    },
                    "answerDate": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"//这里出现了复合类型
                    },
                    "answer_author": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "answer_date": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "author": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "category": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "date": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "description": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "keywords": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "list": {
                        "type": "object"
                    },
                    "question": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "readCount": {
                        "type": "long"
                    },
                    "read_count": {
                        "type": "integer"
                    },
                    "title": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

Head插件查看

其实Mapping,你接触Elasticsearch久一点也就那么回事。我们虽然知道Elasticsearch有根据数据识别创建Mapping,但是最好是创建,并且指定分词与否。这样高效一点。

Elasticsearch教程(五) elasticsearch Mapping的创建的更多相关文章

  1. (转)ElasticSearch教程——汇总篇

    https://blog.csdn.net/gwd1154978352/article/details/82781731 环境搭建篇 ElasticSearch教程——安装 ElasticSearch ...

  2. ElasticSearch(五):Mapping和常见字段类型

    ElasticSearch(五):Mapping和常见字段类型 学习课程链接<Elasticsearch核心技术与实战> 什么是Mapping Mapping类似数据库中的schema的定 ...

  3. Elasticsearch入门教程(五):Elasticsearch查询(一)

    原文:Elasticsearch入门教程(五):Elasticsearch查询(一) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...

  4. Elasticsearch教程(六) elasticsearch Client创建

    Elasticsearch  创建Client有几种方式. 首先在 Elasticsearch  的配置文件 elasticsearch.yml中.定义cluster.name.如下: cluster ...

  5. ElasticSearch实战系列五: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合

    Title:ElasticSearch实战系列四: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合 前言 在上上一篇中介绍了ElasticSearch实战系列三: Elas ...

  6. Elasticsearch 教程--入门

    1.1 初识 Elasticsearch 是一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎,可以说 Lucene 是当今最先进,最高效的全功能开源搜索引擎框架. 但是 L ...

  7. Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解

    Elasticsearch JAVA操作有三种客户端: 1.TransportClient 2.JestClient 3.RestClient 还有种是2.3中有的NodeClient,在5.5.1中 ...

  8. elasticsearch index 之 put mapping

    elasticsearch index 之 put mapping   mapping机制使得elasticsearch索引数据变的更加灵活,近乎于no schema.mapping可以在建立索引时设 ...

  9. Elasticsearch教程(二)java集成Elasticsearch

    1.添加maven <!--tika抽取文件内容 --> <dependency> <groupId>org.apache.tika</groupId> ...

随机推荐

  1. python通过SSH登陆linux并操作

    使用python通过SSH登陆linux并操作 用的昨天刚接触到的库,在windows下通过paramiko来登录linux系统并执行了几个命令,基本算是初试成功,后面会接着学习的. 代码: > ...

  2. 百度之星初赛(A)——T6

    度度熊的01世界 Problem Description 度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成. 现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者 ...

  3. SSH整合一

    Spring整合Hibernate 1.引入我们所需的jar包 2.在applicationContext.xml中植入我们的数据源和sessionFactory工厂,将生成权交由Spring容器进行 ...

  4. [bzoj1026][SCOI2009]windy数——数位dp

    题目 求[a,b]中的windy数个数. windy数指的是任意相邻两个数位上的数至少相差2的数,比如135是,134不是. 题解 感觉这个题比刚才做的那个简单多了...这个才真的应该是数位dp入门题 ...

  5. JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer

    String path = request.getContextPath();  String basePath = request.getScheme()+"://"+reque ...

  6. 【计算机网络】简单网络管理协议 SNMP

    计算机网络  6.7节学习笔记 SNMP: 管理网络上的对象时,必然会给该对象添加一些软件或硬件,但这种添加必须对原有对象的影响尽量小. SNMP中的管理程序和代理程序按客户-服务器方式工作.管理程序 ...

  7. quailty's Contest #1 道路修建 EXT(启发式合并)

    题目链接  道路修建 EXT 考虑并查集的启发式合并,合并的时候小的子树的根成为大的子树的根的儿子. 可以证明这样整棵树的深度不会超过$logn$. 两个根合并的时候,产生的新的边的边权为当前的时间. ...

  8. 洛谷——P2656 采蘑菇

    P2656 采蘑菇 题目描述 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖和ZYR经过某条小径一次, ...

  9. IO 最快的read 和 write

    import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; impo ...

  10. [xsy2724]Tree

    题意:给一棵树,找出$k$个点$A_{1\cdots k}$以最小化$\begin{align*}\sum\limits_{i=1}^{k-1}dis_{A_i,A_{i+1}}\end{align* ...