gpt4 book ai didi

c# - Nest(C#的Elasticsearch客户端)批量索引

转载 作者:行者123 更新时间:2023-12-02 23:32:09 26 4
gpt4 key购买 nike

Nooji在ElasticSearch并在此处嵌套。不确定我在这里做错了什么,但是此代码抛出了

"Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_NUMBER]".



我知道ES因为 JSON is malformed抛出此错误。我不知道为什么Nest无法生成正确的JSON?

注意:我希望能够执行批量索引操作,同时告诉它该有效负载应该进入哪个索引和类型。
public class Test
{
private static Uri _node;
private ElasticsearchClient _client;

static Test()
{
_node = new Uri("http://localhost:9200");
}

public Test()
{
_client = new ElasticsearchClient(new ConnectionSettings(_node));
}

public void Bulk<T>(List<T> data, string index, string type) where T : class
{
_client.Bulk(index, type, data);
}
}

最佳答案

当我认为要使用高级ElasticsearchClient时,您正在使用低级ElasticClient。基于低级客户端的名称,我假设您使用的是NEST 1.x,可能是最新版本1.7.1。请注意,NEST 1.x仅与Elasticsearch 1.x和NEST 2.x兼容,仅与Elasticsearch 2.x兼容。

要使用NEST 1.x批量编制索引,请使用fluent API指定索引名称和类型名称

void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));

// use NEST *ElasticClient*
var client = new ElasticClient(settings, connection: new InMemoryConnection());

var docs = new List<Doc>
{
new Doc(),
new Doc(),
new Doc(),
new Doc(),
new Doc()
};

var indexResponse = client.CreateIndex("docs", c => c
.AddMapping<Doc>(m => m.MapFromAttributes())
);

var bulkResponse = client.Bulk(b => b
.IndexMany(docs, (d, doc) => d.Document(doc).Index("index-name").Type("type-name"))
);
}

public class Doc
{
public Doc()
{
Id = Guid.NewGuid();
}

public Guid Id { get; set; }
}

关于c# - Nest(C#的Elasticsearch客户端)批量索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com