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,这样就不会进行分词
查看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
}
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核心知识--36._手动建立和修改mapping以及定制string类型数据是否分词或者是否被检索
- ElasticSearch32:初识搜索引擎_mapping的核心数据类型以及dynamic mapping
- ElasticSearch34:初识搜索引擎_mapping复杂数据类型以及object类型数据底层结构揭秘
- elasticsearch核心知识--37.搜索引擎_mapping复杂数据类型以及object类型数据底层结构
- ElasticSearch30:初识搜索引擎_query string的分词以及mapping引入案例遗留问题的大揭秘
- 自动拆箱&自动装箱以及String 和基本数据类型封装类生成的对象是否相等
- ElasticSearch29:初识搜索引擎_分词器的内部组成到底是什么以及内置分词器的介绍
- SG公务车:用车人职务级别改成可以手动输入(续:不删表数据,修改字段类型)
- elasticsearch核心知识--32.elasticsearch中的mapping是什么和_mapping的核心数据类型以及dynamic mapping
- 本文从如何建立mysql索引以及介绍mysql的索引类型,再讲mysql索引的利与弊,以及建立索引时需要注意的地方 首先:先假设有一张表,表的数据有10W条数据,其中有一条数据是nickname='
- oracle中修改已有字段数据类型为clob的方法以及ssh框架中如何保存clob字段
- SQL SERVER的数据类型以及怎样建立备注型字段?
- JPA学习笔记---JPA实体Bean的建立---链接上一个博文:对实体Bean中属性进行操作:保存日期类型,设置字段的长度,名字,是否为空,可以声明枚举字段;可以存放二进制数据,可以存放
- java里String需要注意的地方以及基本数据类型注意点(不定期修改更新)
- mysql中数字类型、日期和时间类型以及字符串(字符)类型及大数据在hibernate中的配置
- 【MySQL】(3)约束以及修改数据表
- Java判断对象或者原始数据类型是否是数组
- Spring JDBC-使用Spring JDBC获取本地连接对象以及操作BLOB/CLOB类型数据
- Java判断对象或者原始数据类型是否是数组
- Oracle中的LOB数据类型以及ibatis中处理该类型的typeHandler