我第一次嘗試在 C# 中使用彈性搜索,我正在嘗試創建一個類似于 sql 中的行的檔案。
據我了解,索引類似于表,檔案是一行。我嘗試使用 CreateDocumentAsync 方法,但它沒有任何引數可以傳入索引,所以我不確定如何創建具有特定索引的檔案。我不能使用默認索引,因為我們的產品可以有很多索引。首先,我檢查索引是否存在,如果不存在,則創建索引,然后創建檔案
一些代碼在這里
public async Task<CreateResponse> CreateDocumentAndIndex<T>(T document, string index) where T : class
{
CreateResponse response = new();
if (_client.Indices.Exists(index).Exists)
{
response = await _client.CreateDocumentAsync<T>(document);
}
else
{
await _client.IndexAsync(document, idx => idx.Index(index));
response = await _client.CreateDocumentAsync<T>(document);
}
return response;
}
現在要使用它,我有一個呼叫此方法的函式
var response = await elasticSearchClient.CreateDocumentAndIndex<DbContextEventData>(eventData,"test");
但是在嘗試創建檔案時它給了我一個錯誤。在彈性搜索中創建行/檔案時有沒有辦法傳入索引
uj5u.com熱心網友回復:
問題被標記為elasticsearch-5,因此假設您使用的是 NEST 5.6.6,則可以在創建檔案的同時指定索引
var client = new ElasticClient();
var createResponse = await client.CreateAsync(new { foo = "bar" }, c => c
.Index("my-index") // index
.Type("_doc") // document type
.Id("1") // document id
);
編輯:
由于您使用的是 Elasticsearch 6.3.0,您必須使用最新的 NEST 6.x 客戶端,在撰寫本文時為6.8.10。使用 NEST 6.8.10,創建檔案 API 呼叫如下
private static void Main()
{
var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DisableDirectStreaming()
.PrettyJson()
.DefaultTypeName("_doc") // doc types are gone in future versions, so best to index with this default doc type name
.OnRequestCompleted(LogToConsole);
var client = new ElasticClient(settings);
if (client.IndexExists("my-index").Exists)
{
client.DeleteIndex("my-index");
}
var document = new MyDocument
{
Foo = "Bar",
Baz = 1
};
var createResponse = client.Create(document, c => c
.Index("my-index") // index
.Id("1") // document id
);
var returnedDocument = client.Get<MyDocument>("1", g => g.Index("my-index"));
}
public class MyDocument
{
public string Foo { get; set; }
public int Baz { get; set; }
}
private static void LogToConsole(IApiCallDetails callDetails)
{
if (callDetails.RequestBodyInBytes != null)
{
var serializer = new JsonSerializer();
var jObjects = new List<JObject>();
using (var sr = new StringReader(Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)))
using (var jsonTextReader = new JsonTextReader(sr))
{
jsonTextReader.SupportMultipleContent = true;
while (jsonTextReader.Read())
jObjects.Add((JObject)JObject.ReadFrom(jsonTextReader));
}
var formatting = jObjects.Count == 1
? Newtonsoft.Json.Formatting.Indented
: Newtonsoft.Json.Formatting.None;
var json = string.Join("\n", jObjects.Select(j => j.ToString(formatting)));
Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri} \n{json}");
}
else
{
Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
}
Console.WriteLine();
if (callDetails.ResponseBodyInBytes != null)
{
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n"
$"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n"
$"{new string('-', 30)}\n");
}
else
{
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n"
$"{new string('-', 30)}\n");
}
}
將以下內容記錄到控制臺
PUT http://localhost:9200/my-index/_doc/1/_create?pretty=true
{
"foo": "Bar",
"baz": 1
}
Status: 201
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
------------------------------
GET http://localhost:9200/my-index/_doc/1?pretty=true
Status: 200
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"foo" : "Bar",
"baz" : 1
}
}
------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/398109.html
