使用Elasticsearch NEST C#索引Json文档 [英] Index Json Document using Elasticsearch NEST C#

查看:172
本文介绍了使用Elasticsearch NEST C#索引Json文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Elasticsearch还是很陌生,想知道如何使用NEST C#为json文档创建索引和对json文档的索引?

I'm very new to Elasticsearch and Want to know How to create index and index following json document to Elasticsearch using NEST C#?

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

推荐答案

使用NEST创建索引很简单

To create an index with NEST is as simple as

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.

要将以json表示的文档索引到索引中将是

To index a document represented as json into the index would be

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进行索引,该json可通过ElasticClient上的.LowLevel属性在NEST中使用.

Here we use the low level client to index json, available in NEST through the .LowLevel property on ElasticClient.

要搜索被索引的文档

// 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类型.

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.

创建索引时,我们没有为我们的类型type-name指定映射,因此Elasticsearch从json文档的结构推断出该映射. 这是动态映射,可用于但是,在许多情况下,如果您知道将要发送的文档结构并且不会进行破坏性的更改,那么您 object 类型映射,但是如果要在单个章的章名称和章描述中进行搜索,则可能要映射chapter作为 nested 类型.

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. 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.

这篇关于使用Elasticsearch NEST C#索引Json文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆