From a436936d90da88b7b826b787ba33073eb5676690 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Fri, 14 Apr 2023 00:08:56 +0800 Subject: [PATCH] =?UTF-8?q?feats:=20=E7=A8=8B=E5=BA=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ElasticSearchStudy.App/Program.cs | 15 +++++----- ElasticSearchStudy.Core/Student.cs | 15 ++++++++++ ElasticSearchStudy.Core/Tweet.cs | 6 ++-- .../UseElasticSearchTest.cs | 30 ++++++++++++++++++- 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 ElasticSearchStudy.Core/Student.cs diff --git a/ElasticSearchStudy.App/Program.cs b/ElasticSearchStudy.App/Program.cs index dd686f1..4e942cd 100644 --- a/ElasticSearchStudy.App/Program.cs +++ b/ElasticSearchStudy.App/Program.cs @@ -13,6 +13,14 @@ namespace ElasticSearchStudy.App static void Main(string[] args) { Console.WriteLine("使用 ElistcSearch 简单测试项目"); + if (args == null || args.Length == 0) + { + Console.WriteLine("没有启动参数"); + } + else + { + Console.WriteLine($"有 {args.Length} 个启动参数, 分别是 [{string.Join(" | ",args)}]"); + } var option = new RestClientOptions() { @@ -28,13 +36,6 @@ namespace ElasticSearchStudy.App var context = healthResponse.Content; - var ddd = context.Replace("\t", ""); - - var ccc = context.Split("\r\n"); - var ccc2 = context.Split("\r"); - var ccc3 = context.Split("\n"); - - Console.WriteLine(context); } } diff --git a/ElasticSearchStudy.Core/Student.cs b/ElasticSearchStudy.Core/Student.cs new file mode 100644 index 0000000..1c5e7db --- /dev/null +++ b/ElasticSearchStudy.Core/Student.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ElasticSearchStudy.Core +{ + public class Student + { + public string? Name { get; set; } + public int Age { get; set; } + public string? School { get; set; } + public string? Grade { get; set; } + public string? Address { get; set; } + } +} diff --git a/ElasticSearchStudy.Core/Tweet.cs b/ElasticSearchStudy.Core/Tweet.cs index e5ad868..7dea0bc 100644 --- a/ElasticSearchStudy.Core/Tweet.cs +++ b/ElasticSearchStudy.Core/Tweet.cs @@ -8,8 +8,8 @@ namespace ElasticSearchStudy.Core public class Tweet { public int Id { get; set; } - public string User { get; set; } - public DateTime PostDate { get; set; } - public string Message { get; set; } + public string? User { get; set; } + public DateTime? PostDate { get; set; } + public string? Message { get; set; } } } diff --git a/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs b/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs index 535f92b..1120dfa 100644 --- a/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs +++ b/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs @@ -1,4 +1,6 @@ +using Elastic.Clients.Elasticsearch.IndexManagement; + using ElasticSearchStudy.Core; namespace ElasticSearchStudy.UnitTest @@ -14,7 +16,9 @@ namespace ElasticSearchStudy.UnitTest var elasticSetting = new ElasticsearchClientSettings(new Uri("https://127.0.0.1:9201")) .CertificateFingerprint("e57ce099af49fb34742cddffe1271c4ed709ef29649537fe89e6bee32728444d") - .Authentication(new BasicAuthentication("elastic", "es-461400")); + .Authentication(new BasicAuthentication("elastic", "es-461400")) + //.DefaultIndex("student") + ; _client = new ElasticsearchClient(elasticSetting); } @@ -96,5 +100,29 @@ namespace ElasticSearchStudy.UnitTest _output.WriteLine($"Ϣ{response.ElasticsearchServerError}"); _output.WriteLine($"Ϣ{string.Join("", response.ElasticsearchWarnings)}"); } + + /// + /// studentĵ + /// + [Fact] + public void AddDocument_Test() + { + var stu = new Student() + { + Name = "Test", + Age =55, + School = "̫", + Grade = "һ", + Address ="̫ί¥xxx" + }; + + var request = new IndexRequest(document: stu,index: "student"); + + var response = _client.Index(request); + + Assert.True(response.IsSuccess()); + + + } } } \ No newline at end of file