我是 Web 开发和 Elastic 新手,目前正在将项目从 Elastic NEST v7 迁移到 .NET Elastic Client v8。我在其中一个项目中遇到了批量索引的问题。
以下方法按预期工作,当我检查弹性云时可以看到文档已被索引:
public static async Task Flush()
{
if (Instance._messageList.Count == 0)
return;
try
{
List<object> messageList;
lock (_padlock)
{
if (Instance._messageList.Count == 0)
return;
messageList = new List<object>(Instance._messageList);
Instance._messageList.Clear();
Instance._lastFlush = DateTime.Now;
}
if (messageList.Count == 0)
return;
var bulkResponse = await Instance._client.BulkAsync(x => x
.Index(_elasticIndex)
.IndexMany(messageList));
if (bulkResponse.Errors)
{
foreach (var item in bulkResponse.ItemsWithErrors)
{
Logger.LogError(item.Error?.Reason, null, true);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
Logger.LogError(ex);
}
}
messageList 有效负载:
[0] { index = { _index = "working_index" } }
[1] { version = "1.0", short_message = "test (flushed)", full_message = "", host = "HOSTNAME", applicationname = "***", logic_facility = "***", level = 7, category = "", timestamp = "2024-12-12T15:49:46.000000" }
回复:
'Valid Elasticsearch response built from a successful (200) low level call on POST: /lworking_index/_bulk?pretty=true&error_trace=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: https://b7da72d8bd20404fb73eb8f3eca72424.europe-west1.gcp.cloud.es.io:9243/ Took: 00:00:00.6847335
# Request:
{"index":{}}
{"index":{"_index":"working_index"}}
{"index":{}}
{"version":"1.0","short_message":"\r\ntest (flushed)","full_message":"","host":"HOSTNAME","applicationname":"***","logic_facility":"***","level":7,"category":"","timestamp":"2024-12-12T17:27:20.000000"}
# Response:
{
"errors" : false,
"took" : 0,
"items" : [
{
"index" : {
"_index" : "working_index",
"_id" : "uaDpu5MBZyEfzT_DmF1O",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1806437,
"_primary_term" : 31,
"status" : 201
}
},
{
"index" : {
"_index" : "working_index",
"_id" : "uqDpu5MBZyEfzT_DmF1O",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1806438,
"_primary_term" : 31,
"status" : 201
}
}
]
}
# TCP states:
Established: 30
TimeWait: 9
# ThreadPool statistics:
Worker:
Busy: 2
Free: 32765
Min: 16
Max: 32767
IOCP:
Busy: 0
Free: 1000
Min: 1
Max: 1000'
我的弹性客户端设置:
var settings = new ElasticsearchClientSettings(new Uri(logSettings.LoggingUri))
.DefaultIndex(_elasticIndex)
.RequestTimeout(TimeSpan.FromMinutes(5d))
.EnableHttpCompression()
.Authentication(new BasicAuthentication("working_index", "xxx"))
.EnableDebugMode();
以下方法无法按预期工作。它成功返回,但是,当我检查弹性云时,我看不到索引文档:
public async Task<bool> BulkIndex(object[] docs)
{
if (docs == null || docs.Length == 0) return false;
var response = await _elastic.BulkAsync(x => x
.Index(_elastic.ElasticsearchClientSettings.DefaultIndex)
.IndexMany(docs)
);
return response.IsValidResponse;
}
文档有效负载:
[0] { index = { _index = "broken_index" } }
[1] { TransactionDate = {12/12/2024 18:41:59}, ClickDate = {12/12/2024 18:41:59} }
回复:
Valid Elasticsearch response built from a successful (200) low level call on POST: /broken_index/_bulk?pretty=true&error_trace=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: https://b7da72d8bd20404fb73eb8f3eca72424.europe-west1.gcp.cloud.es.io:9243/ Took: 00:00:01.1513287
# Request:
{"index":{}}
{"index":{"_index":"broken_index"}}
{"index":{}}
{"transactionDate":"2024-12-12T18:41:59.1448351+01:00","clickDate":"2024-12-12T18:41:59.1448428+01:00"}
# Response:
{
"errors" : false,
"took" : 0,
"items" : [
{
"index" : {
"_index" : "broken_index",
"_id" : "eDn2u5MBwRlDSf6Nzl8G",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 102421921,
"_primary_term" : 78,
"status" : 201
}
},
{
"index" : {
"_index" : "broken_index",
"_id" : "eTn2u5MBwRlDSf6Nzl8G",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 102421922,
"_primary_term" : 78,
"status" : 201
}
}
]
}
# TCP states:
Established: 24
TimeWait: 9
SynSent: 6
CloseWait: 2
# ThreadPool statistics:
Worker:
Busy: 1
Free: 32766
Min: 16
Max: 32767
IOCP:
Busy: 0
Free: 1000
Min: 1
Max: 1000
我的弹性客户端设置:
var productionSettings = new ElasticsearchClientSettings(new Uri(Constants.Elastic.ProductionApiBaseUrl))
.Authentication(new BasicAuthentication(Constants.Elastic.AffiliateUser, Constants.Elastic.AffiliatePassword))
.DefaultIndex("broken_index")
.RequestTimeout(TimeSpan.FromMinutes(Constants.Elastic.RequestTimeoutMinutes))
.EnableHttpCompression()
.EnableDebugMode();
尽管两个项目之间的有效负载和响应看起来非常相似,但“BulkIndex()”方法似乎没有按预期工作。索引操作成功(状态 201),但我在云中看不到该文档。
任何人都可以帮助我确定可能导致问题的原因或为我可能忽略的内容提供指导吗?
我的问题是字段名称被设置为驼峰式大小写而不是帕斯卡格式。通过使用“ElasticsearchClientSettings”中的“DefaultFieldNameInferrer”保持我的属性名称不变,可以解决此问题。
new ElasticsearchClientSettings(uri)
.DefaultFieldNameInferrer(fieldName => fieldName);
这将获取您的属性名称并返回未修改的相同字符串。