简体   繁体   English

使用Elasticsearch NEST C#索引Json文档

[英]Index Json Document using Elasticsearch NEST C#

I'm very new to Elasticsearch and Want to know How to create index and index following json document to Elasticsearch using NEST C#? 我是Elasticsearch的新手,想要了解如何使用NEST C#在Jlastic文档之后创建索引和索引到Elasticsearch?

{
    "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..."
        }
    ]
}

To create an index with NEST is as simple as 使用NEST创建索引非常简单

var client = new ElasticClient();
client.CreateIndex("index-name");

This will create an index with the default number of shards and replicas defined for the node. 这将创建一个索引,其中包含为节点定义的默认分片数和副本数。

To index a document represented as json into the index would be 将表示为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);

Here we use the low level client to index json, available in NEST through the .LowLevel property on ElasticClient . 在这里,我们通过使用低等级客户机索引JSON,提供NEST .LowLevel物业ElasticClient

To search the indexed document would be 要搜索索引文档

// 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")
        )
    )
);

This returns the document indexed. 这将返回索引的文档。 The generic type parameter dynamic used in Search<T>() here means that the resulting documents will be deserialized to Json.Net JObject types. Search<T>()使用的泛型类型参数dynamic意味着生成的文档将被反序列化为JObject类型。

When we created the index, we didn't specify a mapping for our type, type-name , so Elasticsearch inferred the mapping from the structure of the json document. 当我们创建索引时,我们没有为我们的类型type-name指定映射,因此Elasticsearch从json文档的结构推断出映射。 This is dynamic mapping and can be useful for many situations however, if you know the structure of documents that you're going to be sending and it's not going to be destructively changed, then you specify a mapping for the type . 这是动态映射 ,对于许多情况都很有用,但是,如果您知道要发送的文档的结构并且不会破坏性地更改它,那么您可以为该类型指定映射 The advantage of doing this in this particular example is that the chapter array will be inferred as an object type mapping, but if you wanted to search across both chapter name and chapter description of an individual chapter, then you probably want to map chapter as a nested type . 在这个特定示例中执行此操作的优点是, chapter数组将被推断为object类型映射,但如果您想要搜索单个章节的章节名称和章节描述,那么您可能希望将chapter映射为nested类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM