我已经能够获得正确的ElasticSearch命令,以根据以下原始请求插入并搜索我的记录:
Original ElasticSearch trying to be converted to NEST
PUT /sample
{
"settings": {
"index.number_of_shards": 5,
"index.number_of_replicas": 0,
"analysis": {
"filter": {
"nGram_filter": {
"type": "nGram",
"min_gram": 2,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
]
},
"edgenGram_filter": {
"type": "edgeNGram",
"min_gram": 2,
"max_gram": 20
}
},
"analyzer": {
"ngram_index_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"lowercase",
"nGram_filter"
]
},
"edge_ngram_index_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"lowercase",
"edgenGram_filter"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"name": {
"type": "string",
"fields": {
"prefixes": {
"type": "string",
"analyzer": "edge_ngram_index_analyzer",
"search_analyzer": "standard"
},
"substrings": {
"type": "string",
"analyzer": "ngram_index_analyzer",
"search_analyzer": "standard"
}
}
}
}
}
}
}
但是,现在在尝试使用NEST命令转换此样本时遇到问题。到目前为止,这是我所做的事情,它将编译并创建索引,但是前缀和子字符串的子字段不存在。
Client.CreateIndex("sample", i => i
.Settings(s => s
.NumberOfShards(10)
.NumberOfReplicas(0)
.Analysis(a => a
.TokenFilters(tf => tf
.NGram("nGram_filter", td => td
.MinGram(2)
.MaxGram(20)
)
.EdgeNGram("edgeNGram", td => td
.MinGram(2)
.MaxGram(20)
)
)
.Analyzers(anz => anz
.Custom("ngram_index_analyzer", cc => cc
.Tokenizer("keyword")
.Filters("lowercase", "nGram_filter")
)
.Custom("edge_ngram_index_analyzer", cc => cc
.Tokenizer("keyword")
.Filters("lowercase", "edgenGram_filter")
)
)
)
)
.Mappings(m => m
.Map<test>(map => map
.Properties(ps => ps
.Text(t => t
.Name(n => n.name)
.Fields(f => f
.Text(tt => tt
.Name("prefixes")
.Analyzer("edge_ngram_index_analyzer")
.SearchAnalyzer("standard")
)
.Text(tt => tt
.Name("substrings")
.Analyzer("ngram_index_analyzer")
.SearchAnalyzer("standard")
)
)
)
)
)
)
);
最佳答案
EdgeNGram
token 过滤器名称似乎有错字。一个条目是edgeNGram
,而另一个条目是edgenGram_filter
。所以当我使用nest运行以下映射时
client.CreateIndex("sample", i => i
.Settings(s => s
.NumberOfShards(10)
.NumberOfReplicas(0)
.Analysis(a => a
.Analyzers(anz => anz
.Custom("ngram_index_analyzer", cc => cc
.Tokenizer("keyword")
.Filters("lowercase", "nGram_filter")
)
.Custom("edge_ngram_index_analyzer", cc => cc
.Tokenizer("keyword")
.Filters("lowercase", "edgeNGram_filter")
)
)
.TokenFilters(tf => tf
.NGram("nGram_filter", td => td
.MinGram(2)
.MaxGram(20)
)
.EdgeNGram("edgeNGram_filter", td => td
.MinGram(2)
.MaxGram(20)
)
)
)
)
.Mappings(m => m
.Map<test>(map => map
.Properties(ps => ps
.Text(t => t
.Name(n => n.name)
.Fields(f => f
.Text(tt => tt
.Name("prefixes")
.Analyzer("edge_ngram_index_analyzer")
.SearchAnalyzer("standard")
)
.Text(tt => tt
.Name("substrings")
.Analyzer("ngram_index_analyzer")
.SearchAnalyzer("standard")
)
)
)
)
)
)
);
它将其转换为包含两个子字段的正确JSON。希望能有所帮助。