"I'm trying to create in index for auto complete seach using ElasticSearch and its NEST .NET client. I'm following the tutorial on http://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch and have run into problems when creating the index and its settings. Sepcifically I would like to create the index with the following settings (taken straight out of the tutorial's TL;DR, to begin with):
PUT /test_index
{
"settings": {
"analysis": {
"filter": {
"edge_ngram_filter": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 20
}
},
"analyzer": {
"edge_ngram_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"edge_ngram_filter"
]
}
}
}
}
My problem is that I don't know how to send the "filter": [ "lowercase", "edge_ngram_filter" ]
part using NEST. My current attempt looks like this:
esclient.CreateIndex("test_index", s => s
.Settings(settings => settings
.Add("analysis.filter.edge_ngram_filter.type", "edge_ngram")
.Add("analysis.filter.edge_ngram_filter.min_gram", "2")
.Add("analysis.filter.edge_ngram_filter.max_gram", "20")
.Add("analysis.analyzer.edge_ngram_analyzer.type", "custom")
.Add("analysis.analyzer.edge_ngram_analyzer.tokenizer", "standard")
// Interesting part on the line below!
.Add("analysis.analyzer.edge_ngram_analyzer.filter", new string[] { "lowercase", "edge_ngram_filter" })
)
);
but this fail withe a JsonWriterException: "Unsupported type: System.String[]. Use the JsonSerializer class to get the object's JSON representation. Path 'settings.index'.". I have tried to simply provide the JSON array manually ( .Add("analysis.analyzer.edge_ngram_analyzer.filter", "[ \\"lowercase\\", "\\edge_ngram_analyzer\\" ]")
) the JSON is instead escaped as a string.
Does the NEST API provide a way of doing this?
UpdateSettings(..)
has much more pleasant syntax to set up analysis settings.
This is how you can handle your case:
client.UpdateSettings(s => s
.Index(indexName)
.Analysis(a => a
.TokenFilters(f => f.Add("edge_ngram_filter", new EdgeNGramTokenFilter
{
MinGram = 2,
MaxGram = 20
}))
.Analyzers(analyzer => analyzer.Add("edge_ngram_analyzer", new CustomAnalyzer
{
Tokenizer = "standard",
Filter = new List<string> {"lowercase", "edge_ngram_filter"}
}))));
Remember to close index before you will update index settings.
Hope it helps.
UPDATE:
You can achieve this during index creation as well. No need to update settings.
client.CreateIndex(indexName, i => i
.Analysis(a => a
.TokenFilters(f => f.Add("edge_ngram_filter", new EdgeNGramTokenFilter
{
MinGram = 2,
MaxGram = 20
}))
.Analyzers(analyzer => analyzer.Add("edge_ngram_analyzer", new CustomAnalyzer
{
Tokenizer = "standard",
Filter = new List<string> {"lowercase", "edge_ngram_filter"}
}))));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.