5

Elasticsearch.Nest 教程系列 9-6 转换:Document paths | 文档路径

 2 years ago
source link: https://blog.zhuliang.ltd/2020/01/backend/Elasticsearch-Nest-Documents-path.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Elasticsearch.Nest 教程系列 9-6 转换:Document paths | 文档路径

create: 2020-01-23 09:43:01 | update: 2020-01-23 12:15:44 本文总阅读量:  次  |  文章总字数: 454 字  |  阅读约需: 2 分钟


Elasticsearch中的许多API描述了文档的路径。在NEST中,除了生成分别带有 Index,Type 和 Id 类型的构造函数外,还有一个构造函数允许你使用 DocumentPath 类型的实例更简洁地描述文档的路径。

eg:创建一个基于 Project 类的 Document,Id=1

IDocumentPath path = new DocumentPath<Project>(1);

你也可以指定 Index 的名字:

var path = new DocumentPath<Project>(1).Index("project1");

通过 DocumentPath 静态方法来进行指定:

//以下方式等效于上方
var path = DocumentPath<Project>.Id(1).Index("projectindex");

通过 CLR 类型实例创建文档类型:

  • DocumentPath 提供了重载方法可以使用 CLR 类型

    ` csharp
    var project = new Project { Name = “hello-world” };

IDocumentPath path = new DocumentPath(project);Expect(“project”).WhenSerializing(path.Index);Expect(“hello-world”).WhenSerializing(path.Id);

//指定 Index
path = new DocumentPath(project).Index(“project1”);

//或者使用静态方法
path = DocumentPath.Id(project);Expect(“project”).WhenSerializing(path.Index);


# 使用 IndexRequest
<p class="code-caption" data-lang="csharp" data-line_number="backend" data-trim_indent="backend" data-label_position="outer" data-labels_left="Code" data-labels_right=":" data-labels_copy="Copy Code"><span class="code-caption-label"></span></p>
``` csharp 
var project = new Project { Name = "hello-world" };
var request = new IndexRequest<Project>(2) { Document = project };
request = new IndexRequest<Project>(project) { };

//使用其他重载
request = new IndexRequest<Project>(IndexName.From<Project>(), 2)
{
    Document = project
};

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK