将多个自定义分析器设置为弹性搜索中的单个字段
Setting multiple custom analyzers to single field in elasticsearch
我在 ElasticSearch 环境中工作,我在本地机器上安装了 elasticsearch 5.4.3 版本。我正在尝试通过定义一些设置以及映射来创建索引。以下是我的设置和映射,
{
"settings":{
"index":{
"analysis":{
"analyzer":{
"index_analyzer":{
"filter":[
"standard",
"lowercase",
"asciifolding"
],
"tokenizer":"standard"
},
"autocomplete":{
"type":"custom",
"tokenizer":"standard",
"filter":[
"lowercase",
"autocomplete_filter"
]
},
"search_analyzer":{
"filter":[
"standard",
"lowercase",
"asciifolding"
],
"tokenizer":"standard"
},
"sortable":{
"filter":"lowercaseFilter",
"tokenizer":"keyword",
"type":"custom"
}
},
"filter":{
"lowercaseFilter":{
"type":"lowercase"
},
"autocomplete_filter":{
"type":"edge_ngram",
"min_gram":1,
"max_gram":20
}
},
"tokenizer":{
"keyword":{
"type":"keyword"
}
}
}
}
}
}
这是我的映射,
{
"geo_data":{
"_all":{
"enabled":true,
"index_analyzer":"index_analyzer",
"search_analyzer":"search_analyzer"
},
"properties":{
"subscriber_level":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
},
"att_id":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
},
"id":{
"include_in_all":false,
"type":"text"
},
"name":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
},
"state_name":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
}
}
}
}
我想要实现的是,我想将所有自定义分析器应用于单个字段。但是上面的分析器字段映射给出了以下异常,
{
"error":{
"root_cause":[
{
"type":"mapper_parsing_exception",
"reason":"analyzer [index_analyzer,search_analyzer,autocomplete_analyzer] not found for field [subscriber_level]"
}
],
"type":"mapper_parsing_exception",
"reason":"analyzer [index_analyzer,search_analyzer,autocomplete_analyzer] not found for field [subscriber_level]"
},
"status":400
}
请任何人都可以帮助我解决这个问题,努力解决它。
您希望使用多个分析器来标记同一个字段。您可以使用 multi-fields 并对多字段内的每种类型应用不同的分析器。
同样在 github issue 之后,_all 字段的配置针对 5.4 进行了更改。
如果您的索引已经存在,
PUT some_index/_mappings/type_name
{
"_all": {
"enabled": true,
"type": "text",
"analyzer": "index_analyzer"
},
"properties": {
"subscriber_level": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"att_id": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"id": {
"include_in_all": false,
"type": "text"
},
"name": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"state_name": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
}
},
"settings": {
"index": {
"analysis": {
"analyzer": {
"index_analyzer": {
"filter": [
"standard",
"lowercase",
"asciifolding"
],
"tokenizer": "standard"
},
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
},
"search_analyzer": {
"filter": [
"standard",
"lowercase",
"asciifolding"
],
"tokenizer": "standard"
},
"sortable": {
"filter": "lowercaseFilter",
"tokenizer": "keyword",
"type": "custom"
}
},
"filter": {
"lowercaseFilter": {
"type": "lowercase"
},
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"tokenizer": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
现在您可以使用任何分析字段进行查询,如下所示
POST some_index/_search
{
"query": {
"term": {
"state_name.index_analyzed": {
"value": "VALUE"
}
}
}
}
谢谢
我在 ElasticSearch 环境中工作,我在本地机器上安装了 elasticsearch 5.4.3 版本。我正在尝试通过定义一些设置以及映射来创建索引。以下是我的设置和映射,
{
"settings":{
"index":{
"analysis":{
"analyzer":{
"index_analyzer":{
"filter":[
"standard",
"lowercase",
"asciifolding"
],
"tokenizer":"standard"
},
"autocomplete":{
"type":"custom",
"tokenizer":"standard",
"filter":[
"lowercase",
"autocomplete_filter"
]
},
"search_analyzer":{
"filter":[
"standard",
"lowercase",
"asciifolding"
],
"tokenizer":"standard"
},
"sortable":{
"filter":"lowercaseFilter",
"tokenizer":"keyword",
"type":"custom"
}
},
"filter":{
"lowercaseFilter":{
"type":"lowercase"
},
"autocomplete_filter":{
"type":"edge_ngram",
"min_gram":1,
"max_gram":20
}
},
"tokenizer":{
"keyword":{
"type":"keyword"
}
}
}
}
}
}
这是我的映射,
{
"geo_data":{
"_all":{
"enabled":true,
"index_analyzer":"index_analyzer",
"search_analyzer":"search_analyzer"
},
"properties":{
"subscriber_level":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
},
"att_id":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
},
"id":{
"include_in_all":false,
"type":"text"
},
"name":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
},
"state_name":{
"analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
"type":"text"
}
}
}
}
我想要实现的是,我想将所有自定义分析器应用于单个字段。但是上面的分析器字段映射给出了以下异常,
{
"error":{
"root_cause":[
{
"type":"mapper_parsing_exception",
"reason":"analyzer [index_analyzer,search_analyzer,autocomplete_analyzer] not found for field [subscriber_level]"
}
],
"type":"mapper_parsing_exception",
"reason":"analyzer [index_analyzer,search_analyzer,autocomplete_analyzer] not found for field [subscriber_level]"
},
"status":400
}
请任何人都可以帮助我解决这个问题,努力解决它。
您希望使用多个分析器来标记同一个字段。您可以使用 multi-fields 并对多字段内的每种类型应用不同的分析器。
同样在 github issue 之后,_all 字段的配置针对 5.4 进行了更改。 如果您的索引已经存在,
PUT some_index/_mappings/type_name
{
"_all": {
"enabled": true,
"type": "text",
"analyzer": "index_analyzer"
},
"properties": {
"subscriber_level": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"att_id": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"id": {
"include_in_all": false,
"type": "text"
},
"name": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"state_name": {
"type": "keyword",
"fields": {
"index_analyzed": {
"type": "text",
"analyzer": "index_analyzer"
},
"search_analyzed": {
"type": "text",
"analyzer": "search_analyzer"
},
"autocomplete_analyzed": {
"type": "text",
"analyzer": "autocomplete"
}
}
}
},
"settings": {
"index": {
"analysis": {
"analyzer": {
"index_analyzer": {
"filter": [
"standard",
"lowercase",
"asciifolding"
],
"tokenizer": "standard"
},
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
},
"search_analyzer": {
"filter": [
"standard",
"lowercase",
"asciifolding"
],
"tokenizer": "standard"
},
"sortable": {
"filter": "lowercaseFilter",
"tokenizer": "keyword",
"type": "custom"
}
},
"filter": {
"lowercaseFilter": {
"type": "lowercase"
},
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"tokenizer": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
现在您可以使用任何分析字段进行查询,如下所示
POST some_index/_search
{
"query": {
"term": {
"state_name.index_analyzed": {
"value": "VALUE"
}
}
}
}
谢谢