美文网首页
34、初识搜索引擎_用一个例子告诉你mapping到底是什么

34、初识搜索引擎_用一个例子告诉你mapping到底是什么

作者: 拉提娜的爸爸 | 来源:发表于2020-01-06 11:40 被阅读0次

    1、首先插入几条数据

    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     1条结果
    GET /website/article/_search?q=post_date:2017           1条结果
    

    3、我们会发现查询出来的结果和我们想象中的结果不一样,这是为什么?

    我们在创建document的时候,es会自动为我们建立一种数据结构和相关配置,简称为mapping
    dynamic mapping自动为我们建立index,创建type,以及type对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置。
    查看es为我们自动创建的type以及对应的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
                  }
                }
              }
            }
          }
        }
      }
    }
    

    结果显示了es自动为我们创建的各个field的数据类型。
    那么搜索结果和我们想象中的结果不一样原因就知道了是因为es自动建立mapping的时候,设置了不同的field不同的data type。不同的data type的分词、搜索等行为是不一样的。所以出现了_all field和post_date field的搜索表现完全不一样。

    相关文章

      网友评论

          本文标题:34、初识搜索引擎_用一个例子告诉你mapping到底是什么

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