使用 Elasticsearch NEST C# 索引 Json 文档
Posted
技术标签:
【中文标题】使用 Elasticsearch NEST C# 索引 Json 文档【英文标题】:Index Json Document using Elasticsearch NEST C# 【发布时间】:2016-06-14 18:28:32 【问题描述】:我是 Elasticsearch 的新手,想知道如何使用 NEST C# 为 Elasticsearch 创建以下 json 文档的索引和索引?
"BookName": "Book1",
"ISBN": "978-3-16-148410-0",
"chapter" : [
"chapter_name": "Chapter1",
"chapter_desc": "Before getting into computer programming, let us first understand computer programs and what they..."
,
"chapter_name": "Chapter2",
"chapter_desc": "Today computer programs are being used in almost every field, household, agriculture, medical, entertainment, defense.."
,
"chapter_name": "Chapter3",
"chapter_desc": "MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are..."
,
"chapter_name": "Chapter4",
"chapter_desc": "Computer programs are being used to develop graphics and special effects in movie..."
]
【问题讨论】:
github.com/elastic/elasticsearch-net/issues/336 【参考方案1】:用 NEST 创建索引很简单
var client = new ElasticClient();
client.CreateIndex("index-name");
这将使用为节点定义的默认分片和副本数创建一个索引。
要将表示为 json 的文档索引到索引中
var json = @"
""BookName"": ""Book1"",
""ISBN"": ""978-3-16-148410-0"",
""chapter"" : [
""chapter_name"": ""Chapter1"",
""chapter_desc"": ""Before getting into computer programming, let us first understand computer programs and what they...""
,
""chapter_name"": ""Chapter2"",
""chapter_desc"": ""Today computer programs are being used in almost every field, household, agriculture, medical, entertainment, defense..""
,
""chapter_name"": ""Chapter3"",
""chapter_desc"": ""MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are...""
,
""chapter_name"": ""Chapter4"",
""chapter_desc"": ""Computer programs are being used to develop graphics and special effects in movie...""
]
";
var indexResponse = client.LowLevel.Index<string>("index-name", "type-name", json);
if (!indexResponse.Success)
Console.WriteLine(indexResponse.DebugInformation);
这里我们使用低级客户端来索引 json,可通过 ElasticClient
上的 .LowLevel
属性在 NEST 中使用。
要搜索索引文档将是
// refresh the index so that newly indexed documents are available
// for search without waiting for the refresh interval
client.Refresh("index-name");
var searchResponse = client.Search<dynamic>(s => s
.Index("index-name")
.Type("type-name")
.Query(q => q
.Match(m => m
.Query("Photoshop")
.Field("chapter.chapter_desc")
)
)
);
这将返回索引的文档。这里Search<T>()
中使用的泛型类型参数dynamic
表示生成的文档会反序列化为Json.Net的JObject
类型。
当我们创建索引时,我们没有为我们的类型指定映射,type-name
,所以 Elasticsearch 从 json 文档的结构中推断出映射。 This is dynamic mapping 并且在许多情况下都很有用,但是,如果您知道要发送的文档的结构并且不会被破坏性地改变,那么您 specify a mapping for the type。在这个特定示例中这样做的好处是 chapter
数组将被推断为 object
type 映射,但如果您想搜索单个章节的章节名称和章节描述,那么您可能想要映射chapter
作为nested
type。
【讨论】:
嗨,我尝试使用 NEST 版本 7.10.1。它在var indexResponse = _elasticClient.LowLevel.Index<string>("index-name", "type-name", json);
处有语法错误。错误是The type 'string' cannot be used as type parameter 'TResponse' in the generic type or method 'IElasticLowLevelClient.Index<TResponse>(string, string, PostData, IndexRequestParameters)'. There is no implicit reference conversion from 'string'
嘿@Steve,自从这个答案以来,客户已经改变了一点。查看elastic.co/guide/en/elasticsearch/client/net-api/current/… 和elastic.co/guide/en/elasticsearch/client/net-api/current/… 中的注释。具体来说,响应类型现在是StringResponse
,并且不支持文档类型的_type
参数以上是关于使用 Elasticsearch NEST C# 索引 Json 文档的主要内容,如果未能解决你的问题,请参考以下文章
使用 C# 中的 NEST 库调用 elasticsearch 时,如何向 linq 语句添加条件逻辑?
如何在 C# Nest 中将日期值发送到 elasticsearch 聚合查询
从 NEST C# 嵌套聚合中获取 Elasticsearch 结果
Elasticsearch NEST – Examples for mapping between Query and C#