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

1.如何建立索引
string类型的字段,是否进行分词设置:
  • 需要进行分词 analyzed
  • 不进行分词 not_analyzed
  • 不能进行搜索 no


2.新增或修改mapping

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

1)新建mapping

例子:

PUT /website
{
  "mappings": {
    "article":{
      "properties": {
        "content":{
          "type": "text",
          "analyzer":"english"
        },
        "post_date":{
          "type":"date"
        },
        "title":{
          "type":"text"
        },
        "publisher_id":{
          "type": "text",
          "index":"not_analyzed"
        }
      }
    }
  }
}
执行结果


{
  "acknowledged": true,
  "shards_acknowledged": true
}


2)如果对该index的type中修改已有字段的mapping,那么就会报错

例子:

PUT /website
{
  "mappings": {
    "article":{
      "properties": {
        "publisher_id":{
          "type": "text",
          "index":"not_analyzed"
        }
      }
    }
  }
}

执行结果

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

3)新增一个type中的字段

例子:

PUT /website/_mapping/article
{
  "properties": {
    "tags":{
      "type":"text"
    }
  }
}

执行结果:
{
  "acknowledged": true
}


2)修改mapping,修改的时候只能新增新的field,不能修改原来的field
如果我们想要修改已有的field的mapping属性,那么就会报错




3.测试mapping
1)测试分词的类型

GET /website/_analyze
{
    "field":"title",
    "text":"a dog"
}

因为使用的是standard analyzer
执行结果:

{
  "tokens": [
    {
      "token": "a",
      "start_offset": 0,
      "end_offset": 1,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "dog",
      "start_offset": 2,
      "end_offset": 5,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}
2)测试不进行分词的类型
新增一个字段,定义为string类型,并设置成not_analyzed,这样就不会进行分词
PUT /website/_mapping/article
{
  "properties": {
    "new_field":{
      "type":"string",
      "index":"not_analyzed"
    }
  }
}

查看website/article的mapping,可以看到new_field的type是keyword,表示不进行分词操作。

{
  "website": {
    "mappings": {
      "article": {
        "properties": {
          "content": {
            "type": "text",
            "analyzer": "english"
          },
          "new_field": {
            "type": "keyword"
          },
          "post_date": {
            "type": "date"
          },
          "publisher_id": {
            "type": "text"
          },
          "tags": {
            "type": "text"
          },
          "title": {
            "type": "text"
          }
        }
      }
    }
  }
}

测试该字段的分词:
GET /website/_analyze
{
  "field": "new_field",
  "text": "a dog"
}
执行结果:因为new_field不能进行分词,所以不支持analyse操作(analysis请求只支持可分词字段)

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[5JcZFTo][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、付费专栏及课程。

余额充值