您的位置:首页 > 移动开发

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

2018-01-03 14:12 691 查看
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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch
相关文章推荐