您当前的位置: 首页 > 

Dongguo丶

暂无认证

  • 2浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

30用一个例子解释mapping到底是什么

Dongguo丶 发布时间:2021-11-07 19:42:13 ,浏览量:2

首先插入几条数据,让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
}

尝试各种搜索

GET /website/article/_search?q=2017		            
GET /website/article/_search?q=2017-01-01        
GET /website/article/_search?q=post_date:2017-01-01   
GET /website/article/_search?q=post_date:2017         	
1)
GET /website/article/_search?q=2017

响应结果 3条结果

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.28004453,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "2",
        "_score": 0.28004453,
        "_source": {
          "post_date": "2017-01-02",
          "title": "my second article",
          "content": "this is my second article in this website",
          "author_id": 11400
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "1",
        "_score": 0.28004453,
        "_source": {
          "post_date": "2017-01-01",
          "title": "my first article",
          "content": "this is my first article in this website",
          "author_id": 11400
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "3",
        "_score": 0.28004453,
        "_source": {
          "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-01-01  

响应结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1.0566096,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "1",
        "_score": 1.0566096,
        "_source": {
          "post_date": "2017-01-01",
          "title": "my first article",
          "content": "this is my first article in this website",
          "author_id": 11400
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "2",
        "_score": 0.84013355,
        "_source": {
          "post_date": "2017-01-02",
          "title": "my second article",
          "content": "this is my second article in this website",
          "author_id": 11400
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "3",
        "_score": 0.84013355,
        "_source": {
          "post_date": "2017-01-03",
          "title": "my third article",
          "content": "this is my third article in this website",
          "author_id": 11400
        }
      }
    ]
  }
}
3)
GET /website/article/_search?q=post_date:2017-01-01 

响应结果

{
  "took": 84,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "1",
        "_score": 1,
        "_source": {
          "post_date": "2017-01-01",
          "title": "my first article",
          "content": "this is my first article in this website",
          "author_id": 11400
        }
      }
    ]
  }
}
4)
GET /website/article/_search?q=post_date:2017 

响应结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "1",
        "_score": 1,
        "_source": {
          "post_date": "2017-01-01",
          "title": "my first article",
          "content": "this is my first article in this website",
          "author_id": 11400
        }
      }
    ]
  }
}

我们会奇怪在2)搜索2017-01-01为什么会出现3条数据、在4)搜索2017为什么只出现1条数据

这是为什么呢?

查看es自动建立的mapping,带出什么是mapping的知识点

自动或手动为index中的type建立的一种数据结构和相关配置,简称为mapping dynamic mapping:自动为我们建立index,创建type,以及type对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置 dynamic mapping这个后边会了解的,我们当然也可以手动建立mapping,在创建数据之前,先创建index和type,以及type对应的mapping

查看自动创建的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
              }
            }
          }
        }
      }
    }
  }
}

自动创建的mapping中设置了fileds的类型

author_id:long

content:text

post_date:date

title:text

因为es自动建立mapping的时候,设置了不同的field不同的data type。不同的data type的分词、搜索等行为是不一样的。

比如

GET /website/article/_search?q=2017-01-01        
GET /website/article/_search?q=post_date:2017-01-01 

q=2017-01-01 应该使用的是text的分词、搜索,

q=post_date:2017-01-01 使用的是date的分词、搜索

所以出现了_all field和post_date field的搜索表现完全不一样。

关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

0.0407s