"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:
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.