Elasticsearch 自定义分析器问题
Elasticsearch custom analyzer issue
我们不得不重新创建 registry_promotion_condition 索引器,它具有名为 custom_value_analyzer 的自定义分析器。因为分析器不是由索引器配置的,所以我们通过 api 调用应用了它。
POST /registry_promotion_condition/_close
PUT /registry_promotion_condition/_settings
{
"analysis" : {
"analyzer" : {
"condition_value_analyzer" : {
"type" : "custom",
"tokenizer" : "punctuation"
}
},
"tokenizer" : {
"punctuation" : {
"pattern" : ",",
"type" : "pattern"
}
}
}
}
POST /registry_promotion_condition/_open
在开发和暂存中应用此分析器后,我们可以使用以下 api 调用毫无问题地获取数据。
POST /registry_promotion_condition/promotion_conditions/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"term": {
"promotionDisplayStatus": {
"value": true,
"boost": 1
}
}
},
{
"match_phrase": {
"conditionValue": {
"query": 4242560,
"analyzer": "condition_value_analyzer",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 0.8
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"sort": [
{
"promotionId": {
"order": "asc"
}
}
]
}
但在直播中它不会给出空响应。你知道这是为什么吗?是否有任何配置允许自定义分析器
这是我在现场环境中的映射
{
"registry_promotion_condition" : {
"mappings" : {
"promotion_conditions" : {
"properties" : {
"conditionDataType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionEntityFieldId" : {
"type" : "long"
},
"conditionId" : {
"type" : "long"
},
"conditionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionValue" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionVersion" : {
"type" : "long"
},
"createYear" : {
"type" : "long"
},
"promotionActiveFrom" : {
"type" : "date"
},
"promotionActiveUntil" : {
"type" : "date"
},
"promotionCode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionCreatedAt" : {
"type" : "date"
},
"promotionDescription" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDiscountAmount" : {
"type" : "float"
},
"promotionDiscountType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayStatus" : {
"type" : "boolean"
},
"promotionId" : {
"type" : "long"
},
"promotionMaxDiscountAmount" : {
"type" : "float"
},
"promotionName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionRuleFile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionServiceType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionVersion" : {
"type" : "long"
},
"routingKey" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
尝试更改您的映射:
{
"registry_promotion_condition" : {
"mappings" : {
"promotion_conditions" : {
"properties" : {
"conditionDataType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionEntityFieldId" : {
"type" : "long"
},
"conditionId" : {
"type" : "long"
},
"conditionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionValue" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionVersion" : {
"type" : "long"
},
"createYear" : {
"type" : "long"
},
"promotionActiveFrom" : {
"type" : "date"
},
"promotionActiveUntil" : {
"type" : "date"
},
"promotionCode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionCreatedAt" : {
"type" : "date"
},
"promotionDescription" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDiscountAmount" : {
"type" : "float"
},
"promotionDiscountType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayStatus" : {
"type" : "boolean"
},
"promotionId" : {
"type" : "long"
},
"promotionMaxDiscountAmount" : {
"type" : "float"
},
"promotionName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionRuleFile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionServiceType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionVersion" : {
"type" : "long"
},
"routingKey" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
收件人(将分析器添加到您的 conditionValue 字段):
{
"registry_promotion_condition" : {
"mappings" : {
"promotion_conditions" : {
"properties" : {
"conditionDataType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionEntityFieldId" : {
"type" : "long"
},
"conditionId" : {
"type" : "long"
},
"conditionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionValue" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"analyzer":"condition_value_analyzer"
},
"conditionVersion" : {
"type" : "long"
},
"createYear" : {
"type" : "long"
},
"promotionActiveFrom" : {
"type" : "date"
},
"promotionActiveUntil" : {
"type" : "date"
},
"promotionCode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionCreatedAt" : {
"type" : "date"
},
"promotionDescription" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDiscountAmount" : {
"type" : "float"
},
"promotionDiscountType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayStatus" : {
"type" : "boolean"
},
"promotionId" : {
"type" : "long"
},
"promotionMaxDiscountAmount" : {
"type" : "float"
},
"promotionName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionRuleFile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionServiceType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionVersion" : {
"type" : "long"
},
"routingKey" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
您必须删除索引才能更新映射并重新建立索引。请参阅我的 answer 关于重建索引的内容。
我们不得不重新创建 registry_promotion_condition 索引器,它具有名为 custom_value_analyzer 的自定义分析器。因为分析器不是由索引器配置的,所以我们通过 api 调用应用了它。
POST /registry_promotion_condition/_close
PUT /registry_promotion_condition/_settings
{
"analysis" : {
"analyzer" : {
"condition_value_analyzer" : {
"type" : "custom",
"tokenizer" : "punctuation"
}
},
"tokenizer" : {
"punctuation" : {
"pattern" : ",",
"type" : "pattern"
}
}
}
}
POST /registry_promotion_condition/_open
在开发和暂存中应用此分析器后,我们可以使用以下 api 调用毫无问题地获取数据。
POST /registry_promotion_condition/promotion_conditions/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"term": {
"promotionDisplayStatus": {
"value": true,
"boost": 1
}
}
},
{
"match_phrase": {
"conditionValue": {
"query": 4242560,
"analyzer": "condition_value_analyzer",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 0.8
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"sort": [
{
"promotionId": {
"order": "asc"
}
}
]
}
但在直播中它不会给出空响应。你知道这是为什么吗?是否有任何配置允许自定义分析器
这是我在现场环境中的映射
{
"registry_promotion_condition" : {
"mappings" : {
"promotion_conditions" : {
"properties" : {
"conditionDataType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionEntityFieldId" : {
"type" : "long"
},
"conditionId" : {
"type" : "long"
},
"conditionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionValue" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionVersion" : {
"type" : "long"
},
"createYear" : {
"type" : "long"
},
"promotionActiveFrom" : {
"type" : "date"
},
"promotionActiveUntil" : {
"type" : "date"
},
"promotionCode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionCreatedAt" : {
"type" : "date"
},
"promotionDescription" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDiscountAmount" : {
"type" : "float"
},
"promotionDiscountType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayStatus" : {
"type" : "boolean"
},
"promotionId" : {
"type" : "long"
},
"promotionMaxDiscountAmount" : {
"type" : "float"
},
"promotionName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionRuleFile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionServiceType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionVersion" : {
"type" : "long"
},
"routingKey" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
尝试更改您的映射:
{
"registry_promotion_condition" : {
"mappings" : {
"promotion_conditions" : {
"properties" : {
"conditionDataType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionEntityFieldId" : {
"type" : "long"
},
"conditionId" : {
"type" : "long"
},
"conditionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionValue" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionVersion" : {
"type" : "long"
},
"createYear" : {
"type" : "long"
},
"promotionActiveFrom" : {
"type" : "date"
},
"promotionActiveUntil" : {
"type" : "date"
},
"promotionCode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionCreatedAt" : {
"type" : "date"
},
"promotionDescription" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDiscountAmount" : {
"type" : "float"
},
"promotionDiscountType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayStatus" : {
"type" : "boolean"
},
"promotionId" : {
"type" : "long"
},
"promotionMaxDiscountAmount" : {
"type" : "float"
},
"promotionName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionRuleFile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionServiceType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionVersion" : {
"type" : "long"
},
"routingKey" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
收件人(将分析器添加到您的 conditionValue 字段):
{
"registry_promotion_condition" : {
"mappings" : {
"promotion_conditions" : {
"properties" : {
"conditionDataType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionEntityFieldId" : {
"type" : "long"
},
"conditionId" : {
"type" : "long"
},
"conditionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"conditionValue" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"analyzer":"condition_value_analyzer"
},
"conditionVersion" : {
"type" : "long"
},
"createYear" : {
"type" : "long"
},
"promotionActiveFrom" : {
"type" : "date"
},
"promotionActiveUntil" : {
"type" : "date"
},
"promotionCode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionCreatedAt" : {
"type" : "date"
},
"promotionDescription" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDiscountAmount" : {
"type" : "float"
},
"promotionDiscountType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionDisplayStatus" : {
"type" : "boolean"
},
"promotionId" : {
"type" : "long"
},
"promotionMaxDiscountAmount" : {
"type" : "float"
},
"promotionName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionRuleFile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionServiceType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotionVersion" : {
"type" : "long"
},
"routingKey" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
您必须删除索引才能更新映射并重新建立索引。请参阅我的 answer 关于重建索引的内容。