rya*_*all 6 elasticsearch nest
我想使用NEST库的Fluent界面来创建索引,其中包括设置自定义过滤器,分析器和类型映射.我想避免使用特定于NEST的注释来装饰我的类.
我在http://nest.azurewebsites.net/indices/create-indices.html和 http://nest.azurewebsites.net/indices/put-mapping.html上看过这些文档.本文档虽然显示了一些示例,但还不够完整,无法帮助我弄清楚如何使用Fluent API构建一些复杂的索引方案.
我发现http://euphonious-intuition.com/2012/08/more-complicated-mapping-in-elasticsearch/上的教程非常有用; 一些代码显示如何通过NEST Fluent接口代替直接JSON来构建本教程中的过滤器,分析器和映射,这将是这个问题的一个很好的答案.
您的问题越具体,您收到的答案就越好.不过,这里有一个索引,用于设置分析器(带过滤器)和标记器(EdgeNGram),然后使用它们在Tag类的Name字段上创建自动完成索引.
public class Tag
{
public string Name { get; set; }
}
Nest.IElasticClient client = null; // Connect to ElasticSearch
var createResult = client.CreateIndex(indexName, index => index
.Analysis(analysis => analysis
.Analyzers(a => a
.Add(
"autocomplete",
new Nest.CustomAnalyzer()
{
Tokenizer = "edgeNGram",
Filter = new string[] { "lowercase" }
}
)
)
.Tokenizers(t => t
.Add(
"edgeNGram",
new Nest.EdgeNGramTokenizer()
{
MinGram = 1,
MaxGram = 20
}
)
)
)
.AddMapping<Tag>(tmd => tmd
.Properties(props => props
.MultiField(p => p
.Name(t => t.Name)
.Fields(tf => tf
.String(s => s
.Name(t => t.Name)
.Index(Nest.FieldIndexOption.not_analyzed)
)
.String(s => s
.Name(t => t.Name.Suffix("autocomplete"))
.Index(Nest.FieldIndexOption.analyzed)
.IndexAnalyzer("autocomplete")
)
)
)
)
)
);
Run Code Online (Sandbox Code Playgroud)
NEST在github上的单元测试项目中也有一个相当完整的映射示例. https://github.com/elasticsearch/elasticsearch-net/blob/develop/src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs
编辑:
要查询索引,请执行以下操作:
string queryString = ""; // search string
var results = client.Search<Tag>(s => s
.Query(q => q
.Text(tq => tq
.OnField(t => t.Name.Suffix("autocomplete"))
.QueryString(queryString)
)
)
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3583 次 |
最近记录: |