"我尝试使用ElasticSearch及其NEST .NET客户端在索引中创建自动完成搜索。我正在关注http://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch上的教程,并在创建索引及其设置时遇到问题。顺便提一下,我想用以下设置创建索引(直接从教程的TL; DR开始):
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"
]
}
}
}
}
我的问题是我不知道如何使用NEST发送"filter": [ "lowercase", "edge_ngram_filter" ]
部分。我目前的尝试看起来像这样:
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" })
)
);
但是这与JsonWriterException失败:"不支持的类型:System.String []。使用JsonSerializer类获取对象的JSON表示。路径' settings.index'。"。我试图简单地手动提供JSON数组(.Add("analysis.analyzer.edge_ngram_analyzer.filter", "[ \"lowercase\", "\edge_ngram_analyzer\" ]")
)而是将JSON转义为字符串。
NEST API是否提供了这样做的方法?
答案 0 :(得分:1)
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"}
}))));
有更多令人愉快的语法来设置分析设置。
这就是你处理案件的方法:
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"}
}))));
请记住在更新索引设置之前关闭索引。
希望它有所帮助。
更新:
您也可以在创建索引期间实现此目的。无需更新设置。
function custom_rewrite_tag() {
add_rewrite_tag('%country%', '([^&]+)');
add_rewrite_tag('%city%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_rule() {
add_rewrite_rule('timezone/([^/]*)/([^/]*)','index.php?page_id=4&country=$matches[1]&city=$matches[2]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
?>