指数使用NEST动态对象

指数使用NEST动态对象

本文介绍了指数使用NEST动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个API应用程序,基本上允许用户建立一个文件,但可以自己想的结构,将存储在Elasticsearch。从本质上讲,我提供了一个简单的界面,用户可以访问我们的Elasticsearch实例。我试图使实现尽可能简单。下面是我处理为止。

I am building an API application that essentially allows a user to build a document, which can be structured however they want, that will be stored in Elasticsearch. Essentially, I'm providing a simple interface for users to access our Elasticsearch instance. I'm trying to keep the implementation as simple as possible. Here's what I'm dealing with so far.

有关预期的体对象:

public class DocumentModel
{
    public string Index { get; set; }
    public string Type { get; set; }
    public string Id { get; set; }
    [ElasticProperty(Type = FieldType.Nested)]
    public dynamic Document { get; set; }
}

简单实现:

[HttpPost]
[Route("")]
public IHttpActionResult Post(DocumentModel document)
{
    Uri nodeLocation = new Uri("http://localhost:9200");
    IConnectionPool connectionPool = new SniffingConnectionPool(new List<Uri> { nodeLocation });
    ConnectionSettings settings = new ConnectionSettings(connectionPool);
    ElasticClient esClient = new ElasticClient(settings);

    IIndexResponse result = esClient.Index(document, i => i
        .Index(document.Index)
        .Type(document.Type)
        .Id(document.Id));

    return Ok(result.IsValid);
}

这工作得很好,但它包含索引,类型和编号源。我真正想要做的是简单的索引时提供这些三部分信息,但实际上只是指数document.Document,这是一个动态的类型。但是,这似乎与鸟巢不同意,因为它在IDE中抛出一个错误,在编译时:

This works fine, but it includes the Index, Type, and Id in the source. What I'd really like to do is simply provide those three pieces of information when indexing, but actually just index document.Document, which is of a dynamic type. But, that seems to disagree with Nest, as it throws an error in the IDE and at compile time:

匿名函数或方法组不能被用作动态绑定操作的构成值

不能使用lambda前pression作为参数传递给动态调度的操作,而不首先将其转换为一个委托或前pression乔木型。

"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type".

我怎么能指标只是 document.Document ?有没有更好的方式来处理未知结构的传入的JSON文件比使用动态类型?

How can I index just document.Document? Is there a better way to handle an incoming JSON document of unknown structure than using a dynamic type?

推荐答案

有一对夫妇方法可以做到这一点。

There's a couple ways to do this.

试图索引文档类型动态将无法正常工作,但你可以索引它作为通过IndexRequest对象的对象。

Trying to index the document as type dynamic won't work, but you can index it as an object through the IndexRequest object.

dynamic dynamicDoc = new { /*fill in document format here*/ };
ElasticClient esClient = new ElasticClient(esSettings);

IndexRequest<object> request = new IndexRequest<object>(dynamicDoc)
{
    Index = "someindex",
    Type = "SomeType",
    Id = "someid"
};

esClient.Index<object>(request);

或者,如果处理大量文件

Or if dealing with documents in bulk

List<dynamic> Documents = new List<dynamic>();
//Populate Documents

BulkDescriptor descriptor = new BulkDescriptor();
foreach(var doc in Documents)
{
    descriptor.Index<object>(i => i
        .Index("someindex")
        .Type("SomeType")
        .Id("someid")
        .Document(doc));
}

esClient.Bulk(descriptor);

NEST(或更精确地,Elasticsearch.Net)也具有附着到ElasticClient类.RAW方法变型中,它可以索引原始JSON。使用Raw.Index()让我们我们做这样的事情:

NEST (or more accurately, Elasticsearch.Net) also has a .Raw method variant attached to the ElasticClient class, which can index raw JSON. Using Raw.Index() let's us do things like this:

string documentJson = JsonConvert.SerializeObject(document.Document);

ElasticsearchResponse<string> result = esClient.Raw.Index(document.Index, document.Type, document.Id, documentJson);

为响应类型描述符的类型,你会期待的回应是在(字符串意味着你将有一个序列化的JSON响应,你可以反序列化,做一些有)。这使我们能够避开整个对象类型的问题和NEST索引文档分成Elasticsearch完全按预期。

The type descriptor for the response is the type you'll expect the response to be in (string means you'll have a serialized json response which you can deserialize and do something with). This allows us to sidestep the entire object type issue and NEST indexes the document into Elasticsearch exactly as expected.

这篇关于指数使用NEST动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:26