本文使用的elasticsearch是5.2.1,官方的multi-fields例子参见 https://www.elastic.co/guide/en/elasticsearch/reference/5.3/multi-fields.html
创建一个index及type和mapping 及setting 和analyzer和search_analyzer,注意此时已经是multi-fields了,如下:
PUT /test/
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
},
"my_analyzer2": {
"type": "pattern",
"pattern": "\\W+|_",
"lowercase": true
}
},
"tokenizer": {
"my_tokenizer": {
"type": "pattern",
"pattern": "\\w+|\"(.+?)\"",
"group":0
}
}
}
},
"mappings": {
"tech": {
"properties": {
"body": {
"type": "text",
"analyzer":"standard",
"search_analyzer": "standard",
"fields": {
"syh": {
"type": "text",
"analyzer":"my_analyzer",
"search_analyzer": "my_analyzer"
},
"xhx": {
"type": "text",
"analyzer":"my_analyzer2",
"search_analyzer": "my_analyzer2"
}
}
}
}
}
}
}
修改刚才创建的type的mapping为multi-fields
PUT /test/tech/_mapping
{
"tech": {
"properties": {
"body": {
"type": "text",
"analyzer":"standard",
"search_analyzer": "standard",
"fields": {
"syh": {
"type": "text",
"analyzer":"my_analyzer",
"search_analyzer": "my_analyzer"
},
"xhx": {
"type": "text",
"analyzer":"my_analyzer2",
"search_analyzer": "my_analyzer2"
}
}
}
}
}
}
修改刚才创建的索引的setting,本例是修改analyzer,注意修改setting必须先关闭索引,然后再打开索引
POST /test/_close
PUT /test/_settings
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
},
"my_analyzer2": {
"type": "pattern",
"pattern": "\\W+|_",
"lowercase": true
}
},
"tokenizer": {
"my_tokenizer": {
"type": "pattern",
"pattern": "\\w+|\"(.+?)\"",
"group":0
}
}
}
}
}
POST /test/_open
针对刚才索引test的类型tech的不同的字段做analyzer,同样的文档其分词效果是不一样的。
查看body.syh字段的带双引号分词效果
POST /test/_analyze
{
"field": "body.syh",
"text": "description \"this device in beijing office \" \"what are you going to do\" email_address"
}
查看body.xhx字段的带下划线的分词效果
POST /test/_analyze
{
"field": "body.xhx",
"text": "description \"this device in beijing office \" \"what are you going to do\" email_address"
}
查看body字段的standard的分词效果
POST /test/_analyze
{
"field": "body",
"text": "description \"this device in beijing office \" \"what are you going to do\" email_address"
}
使用bulk插入一条数据
POST _bulk/?refresh=true
{ "index" : { "_index" : "test", "_type" : "tech" } }
{ "body": "description \"this device in beijing office\" \"what are you going to do\" email_address"}
查询body字段的standard搜索
POST /test/_search?pretty
{
"query": {
"term": {
"body": "description"
}
}
}
查询body.xhx字段的my_analyzer2搜索
POST /test/_search?pretty
{
"query": {
"term": {
"body.syh": "\"this device in beijing office\""
}
}
}
POST /test/_search?pretty
{
"query": {
"term": {
"body.xhx": "address"
}
}
}