美文网首页ElasticSearch入门玩转大数据大数据 爬虫Python AI Sql
三十七、Elasticsearch初识搜索引擎-mapping是

三十七、Elasticsearch初识搜索引擎-mapping是

作者: 编程界的小学生 | 来源:发表于2017-07-09 13:28 被阅读117次

    用一个例子说明mapping是什么?

    (1)插入几条数据,让ES自动为我们建立一个索引

    PUT /website/article/1
    {
      "post_date": "2017-01-01",
      "title": "my first article",
      "content": "this is my first article in this website",
      "author_id": 11400
    }
    
    PUT /website/article/2
    {
      "post_date": "2017-01-02",
      "title": "my second article",
      "content": "this is my second article in this website",
      "author_id": 11400
    }
    
    PUT /website/article/3
    {
      "post_date": "2017-01-03",
      "title": "my third article",
      "content": "this is my third article in this website",
      "author_id": 11400
    }
    

    (2)尝试各种搜索
    GET /website/article/_search?q=2017
    3条结果全部输出

    GET /website/article/_search?q=2017-01-01
    3条结果全部输出

    GET /website/article/_search?q=post_date:2017-01-01
    只输出了2017-01-01这条数据

    GET /website/article/_search?q=post_date:2017
    只输出了2017-01-01这条数据

    (3)为什么会这样?
    是因为mapping搞的鬼,因为ES自动创建mapping的时候,设置了不同的field不同的data type。不同的data type的分词,搜索行为是不一样的。具体细节后期说,这里只是简单介绍下。
    当我们创建document的时候,ES会自动或手动为index中的type建立一种数据结构和相关配置,简称为mapping。
    mapping中包含了每个field对应的数据类型,以及如何分词等设置

    (4)如何查看为我们生成的mapping是什么样的?
    GET /website/_mapping/article/
    结果:

    {
      "website": {
        "mappings": {
          "article": {
            "properties": {
              "author_id": {
                "type": "long"
              },
              "content": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "post_date": {
                "type": "date"
              },
              "title": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          }
        }
      }
    }
    

    返回结果解释说明
    website:index名称
    article:type名称
    properties:type下所有的字段
    type:字段对应值所属类型

    若有兴趣,欢迎来加入群,【Java初学者学习交流群】:458430385,此群有Java开发人员、UI设计人员和前端工程师。有问必答,共同探讨学习,一起进步!
    欢迎关注我的微信公众号【Java码农社区】,会定时推送各种干货:


    qrcode_for_gh_577b64e73701_258.jpg

    相关文章

      网友评论

        本文标题:三十七、Elasticsearch初识搜索引擎-mapping是

        本文链接:https://www.haomeiwen.com/subject/bvvshxtx.html