问题
I'm trying to replicate the below mappings using NEST and facing an issue while mapping the token chars to the tokenizer.
{
"settings": {
"analysis": {
"filter": {
"nGram_filter": {
"type": "nGram",
"min_gram": 2,
"max_gram": 20,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
}
},
"analyzer": {
"nGram_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"asciifolding",
"nGram_filter"
]
}
}
}
}
I was able to replicate everything except the token chars part. Can some one help in doing so. Below is my code replicating the above mappings. (except for the token chars part)
var nGramFilters1 = new List<string> { "lowercase", "asciifolding", "nGram_filter" };
var tChars = new List<string> { "letter", "digit", "punctuation", "symbol" };
var createIndexResponse = client.CreateIndex(defaultIndex, c => c
.Settings(st => st
.Analysis(an => an
.Analyzers(anz => anz
.Custom("nGram_analyzer", cc => cc
.Tokenizer("whitespace").Filters(nGramFilters1)))
.TokenFilters(tf=>tf.NGram("nGram_filter",ng=>ng.MinGram(2).MaxGram(20))))));
References
- SO Question
- GitHub Issue
回答1:
NGram Tokenizer supports token characters (token_chars
), using these to determine which characters should be kept in tokens and split on anything that isn't represented in the list.
NGram Token Filter on the other hand operates on the tokens produced by a tokenizer, so only has options for the min and max grams that should be produced.
Based on your current analysis chain, it's likely you want something like the following
var createIndexResponse = client.CreateIndex(defaultIndex, c => c
.Settings(st => st
.Analysis(an => an
.Analyzers(anz => anz
.Custom("ngram_analyzer", cc => cc
.Tokenizer("ngram_tokenizer")
.Filters(nGramFilters))
)
.Tokenizers(tz => tz
.NGram("ngram_tokenizer", td => td
.MinGram(2)
.MaxGram(20)
.TokenChars(
TokenChar.Letter,
TokenChar.Digit,
TokenChar.Punctuation,
TokenChar.Symbol
)
)
)
)
)
);
来源:https://stackoverflow.com/questions/38065966/token-chars-mapping-to-ngram-filter-elasticsearch-nest