四十四、Elasticsearch初识搜索引擎-mapping的建立和定制string类型是否分词

1、index定义字段的分析类型以及检索方式

(1)no ---- 无法通过检索查询到该字段;
(2)not_analyzed ---- 将整个字段存储为关键词,不进行分词,常用于汉字短语,邮箱等复杂的字符串
(3)analyzed ---- 将通过默认的standard分词器进行分析。
只有string(text)类型默认是analyzed的,其余皆是not_analyzed

2、mapping的创建与新增

(1)创建mapping

PUT /website
{
  "mappings": {
    "article" : {
      "properties": {
        "author_id" : {
          "type": "long"
        },
        "title" : {
          "type": "text",
          "analyzer": "english"
        },
        "content" : {
          "type": "text"
        },
        "post_date" : {
          "type": "date",
          "index": "not_analyzed"
        }
      }
    }
  }
}

type:指定数据类型
analyzer:指定分词器
index:指定是否进行分词

(2)尝试修改mapping

PUT /website 
{
  "mappings": {
    "article" : {
      "properties": {
        "author_id" : {
          "type": "text"
        }
      }
    }
  }
}

返回结果

{
  "error": {
    "root_cause": [
      {
        "type": "index_already_exists_exception",
        "reason": "index [website/x9u4ADX1SLGHnFNL5u84Zw] already exists",
        "index_uuid": "x9u4ADX1SLGHnFNL5u84Zw",
        "index": "website"
      }
    ],
    "type": "index_already_exists_exception",
    "reason": "index [website/x9u4ADX1SLGHnFNL5u84Zw] already exists",
    "index_uuid": "x9u4ADX1SLGHnFNL5u84Zw",
    "index": "website"
  },
  "status": 400
}

ES规定mapping一旦建立不可修改,但是可以新增字段mapping,如下

PUT /website/_mapping/article
{
  "properties": {
    "new_field" : {
      "type": "string",
      "index": "not_analyzed"
    }
  }
}

3、测试mapping

(1)测试content,看是否进行了默认的analyzed分词

GET /website/_analyze
{
  "field": "content",
  "text": "My-dogs"
}

返回结果

{
  "tokens": [
    {
      "token": "my",
      "start_offset": 0,
      "end_offset": 2,
      "type": "",
      "position": 0
    },
    {
      "token": "dogs",
      "start_offset": 3,
      "end_offset": 7,
      "type": "",
      "position": 1
    }
  ]
}

发现大小写转换,自动去除了-,进行了分词操作。

(2)测试date类型是否进行了分词

GET /website/_analyze
{
  "field": "post_date",
  "text": "2011-11-11"
}

返回结果

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[rrFOnCB][192.168.0.74:9300][indices:admin/analyze[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Can't process field [post_date], Analysis requests are only supported on tokenized fields"
  },
  "status": 400
}

发现报错了。报错原因是因为我们创建mapping的时候给post_date指定了index:not_analyzed

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


qrcode_for_gh_577b64e73701_258.jpg

你可能感兴趣的:(四十四、Elasticsearch初识搜索引擎-mapping的建立和定制string类型是否分词)