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
}
网友评论