38.初识搜索引擎_手动建立和修改mapping以及定制string类型数据是否分词

1、定制string类型数据是否分词属性index的值

analyzed:设置该field可以进行分词。
not_analyzed:设置该field不能进行分词,只能精确查询(exact value)
no:不能作为查询field。

2、手动建立mapping

只能创建index时手动建立mapping,或者新增field mapping,但是不能修改field mapping

(1)创建索引,并手动设置mapping以及是否分词

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

type:field的数据类型;
analyzer:分词器类型(默认分词类型为standard,不用写);
index:是否分词;

(2)验证是否可以修改field mapping

PUT /website
{
  "mappings": {
    "article":{
      "properties": {
        "author_id":{
          "type": "text"
        }
      }
    }
  }
}
---------------------------------结果---------------------------------
{
  "error": {
    "root_cause": [
      {
        "type": "index_already_exists_exception",
        "reason": "index [website/9zbQfHPnSraYDTK-ERp9-Q] already exists",
        "index_uuid": "9zbQfHPnSraYDTK-ERp9-Q",
        "index": "website"
      }
    ],
    "type": "index_already_exists_exception",
    "reason": "index [website/9zbQfHPnSraYDTK-ERp9-Q] already exists",
    "index_uuid": "9zbQfHPnSraYDTK-ERp9-Q",
    "index": "website"
  },
  "status": 400
}

(3)新增field 并手动设置mapping

PUT /website/_mapping/article
{
  "properties": {
    "insert_field":{
      "type": "text",
      "index": "not_analyzed"
    }
  }
}
--------------------------结果--------------------------
{
  "acknowledged": true
}

3、测试mapping

  • 可以分词的field测试
GET website/_analyze
{
  "field": "content",
  "text": "a dogs"
}
--------------------------结果--------------------------
{
  "tokens": [
    {
      "token": "a",
      "start_offset": 0,
      "end_offset": 1,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "dogs",
      "start_offset": 2,
      "end_offset": 6,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}
  • 不可分词的field测试
GET website/_analyze
{
  "field": "new_field",
  "text": "a dogs"
}
--------------------------结果--------------------------
{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[S2Ptarq][127.0.0.1:9300][indices:admin/analyze[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Can't process field [new_field], Analysis requests are only supported on tokenized fields"
  },
  "status": 400
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值