2

我使用以下命令创建一个test通过PUT请求命名的索引:

PUT http://localhost:9250/test
{
    "settings": {
        "analysis": {
            "char_filter": {
                "&_to_and": {
                    "type": "mapping",
                    "mappings": ["& => and"]
                }
            },
            "filter": {
                "my_stopwords": {
                    "type":       "stop",
                    "stopwords": ["the", "a"]
                }
            },
            "analyzer": {
                "my_analyzer": {
                    "type":         "custom",
                    "char_filter":  ["html_strip", "&_to_and"],
                    "tokenizer":    "standard",
                    "filter":       ["lowercase", "my_stopwords"]
                },
                "folding": {
                    "token_filters": ["lowercase", "asciifolding"],
                    "tokenizer": "standard",
                    "type": "custom"
                }
            }
        }
    },
    "mappings": {
        "tweet": {
            "dynamic": "strict",
            "properties": {
                "author": {
                    "type": "string",
                    "index": "my_analyzer",
                    "store": true
                },
                "text": {
                    "type": "string",
                    "index": "folding",
                    "store": true
                },
                "timestamp": {
                    "type": "date",
                    "format": "yyyy-MM-dd'T'HH:mm:ssZ",
                    "store": true
                }
            }
        }
    }
}

但这会返回以下错误:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "wrong value for index [my_analyzer] for field [author]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [tweet]: wrong value for index [my_analyzer] for field [author]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "wrong value for index [my_analyzer] for field [author]"
    }
  },
  "status": 400
}

我发送的 json 似乎是有效的。这个错误的原因是什么?

我正在使用 ES 2.2.0。

4

1 回答 1

4

由于错误消息描述了自定义分析器,例如 不是映射my_analyzer中选项的有效值。index根据文档,它可以采用的唯一值是

不要将此字段值添加到索引中。使用此设置,该字段将不可查询。

未分析

将字段值添加到索引不变,作为单个术语。这是支持此选项的所有字段的默认值,字符串字段除外。not_analyzed 字段通常用于结构化搜索的术语级查询。

分析了

此选项仅适用于字符串字段,这是默认设置。首先分析字符串字段值以将字符串转换为术语(例如,单个单词的列表),然后对其进行索引。在搜索时,查询字符串(通常)通过相同的分析器以生成与索引中的格式相同的术语。正是这个过程使全文搜索成为可能。

如果您想为字段设置自定义分析器,请使用分析器选项

例子:

{
    "settings": {
        "analysis": {
            "char_filter": {
                "&_to_and": {
                    "type": "mapping",
                    "mappings": ["& => and"]
                }
            },
            "filter": {
                "my_stopwords": {
                    "type":       "stop",
                    "stopwords": ["the", "a"]
                }
            },
            "analyzer": {
                "my_analyzer": {
                    "type":         "custom",
                    "char_filter":  ["html_strip", "&_to_and"],
                    "tokenizer":    "standard",
                    "filter":       ["lowercase", "my_stopwords"]
                },
                "folding": {
                    "token_filters": ["lowercase", "asciifolding"],
                    "tokenizer": "standard",
                    "type": "custom"
                }
            }
        }
    },
    "mappings": {
        "tweet": {
            "dynamic": "strict",
            "properties": {
                "author": {
                    "type": "string",
                    "analyzer": "my_analyzer",
                    "store": true
                },
                "text": {
                    "type": "string",
                    "analyzer": "folding",
                    "store": true
                },
                "timestamp": {
                    "type": "date",
                    "format": "yyyy-MM-dd'T'HH:mm:ssZ",
                    "store": true
                }
            }
        }
    }
}
于 2016-03-01T20:17:31.680 回答