From 297ee21dd84297450bea0b203d2a6af38c214fc2 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 4 Apr 2023 12:09:11 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ElasticSearchStudy.App.csproj | 14 +++ ElasticSearchStudy.App/Program.cs | 10 ++ .../ElasticSearchStudy.Core.csproj | 12 +++ ElasticSearchStudy.Core/Tweet.cs | 12 +++ .../ElasticSearchStudy.UnitTest.csproj | 29 ++++++ .../UseElasticSearchTest.cs | 97 +++++++++++++++++++ ElasticSearchStudy.UnitTest/Usings.cs | 1 + ElasticSearchStudy.sln | 37 +++++++ 8 files changed, 212 insertions(+) create mode 100644 ElasticSearchStudy.App/ElasticSearchStudy.App.csproj create mode 100644 ElasticSearchStudy.App/Program.cs create mode 100644 ElasticSearchStudy.Core/ElasticSearchStudy.Core.csproj create mode 100644 ElasticSearchStudy.Core/Tweet.cs create mode 100644 ElasticSearchStudy.UnitTest/ElasticSearchStudy.UnitTest.csproj create mode 100644 ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs create mode 100644 ElasticSearchStudy.UnitTest/Usings.cs create mode 100644 ElasticSearchStudy.sln diff --git a/ElasticSearchStudy.App/ElasticSearchStudy.App.csproj b/ElasticSearchStudy.App/ElasticSearchStudy.App.csproj new file mode 100644 index 0000000..b03e64f --- /dev/null +++ b/ElasticSearchStudy.App/ElasticSearchStudy.App.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/ElasticSearchStudy.App/Program.cs b/ElasticSearchStudy.App/Program.cs new file mode 100644 index 0000000..b3b2adf --- /dev/null +++ b/ElasticSearchStudy.App/Program.cs @@ -0,0 +1,10 @@ +namespace ElasticSearchStudy.App +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} \ No newline at end of file diff --git a/ElasticSearchStudy.Core/ElasticSearchStudy.Core.csproj b/ElasticSearchStudy.Core/ElasticSearchStudy.Core.csproj new file mode 100644 index 0000000..ac1680c --- /dev/null +++ b/ElasticSearchStudy.Core/ElasticSearchStudy.Core.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.1 + enable + + + + + + + diff --git a/ElasticSearchStudy.Core/Tweet.cs b/ElasticSearchStudy.Core/Tweet.cs new file mode 100644 index 0000000..b99ec5a --- /dev/null +++ b/ElasticSearchStudy.Core/Tweet.cs @@ -0,0 +1,12 @@ +using System; + +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; } + } +} diff --git a/ElasticSearchStudy.UnitTest/ElasticSearchStudy.UnitTest.csproj b/ElasticSearchStudy.UnitTest/ElasticSearchStudy.UnitTest.csproj new file mode 100644 index 0000000..bc88a98 --- /dev/null +++ b/ElasticSearchStudy.UnitTest/ElasticSearchStudy.UnitTest.csproj @@ -0,0 +1,29 @@ + + + + net7.0 + enable + enable + + false + true + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs b/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs new file mode 100644 index 0000000..15598ef --- /dev/null +++ b/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs @@ -0,0 +1,97 @@ +using Elastic.Transport; +using Elastic.Transport.Diagnostics; +using Elastic.Transport.Extensions; +using Elastic.Transport.Products; + +using Elastic.Clients.Elasticsearch; +using Xunit.Abstractions; +using ElasticSearchStudy.Core; + +namespace ElasticSearchStudy.UnitTest +{ + public class UseElasticSearchTest + { + private ITestOutputHelper _output; + + public UseElasticSearchTest(ITestOutputHelper outputHelper) + { + _output = outputHelper; + } + + [Fact] + public void Test() + { + var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200")) + .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A") + .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx")); + + ElasticsearchClient client = new ElasticsearchClient(elasticSetting); + + var pingResponse = client.Ping(); + + if (pingResponse.IsSuccess()) + { + _output.WriteLine($"ÇëÇó³É¹¦,{pingResponse.DebugInformation}"); + } + else + { + _output.WriteLine("ÇëÇóʧ°Ü"); + } + + Assert.True(pingResponse.IsSuccess()); + } + + [Fact] + public void CreateIndex_Test() + { + var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200")) + .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A") + .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx")) + ; + + ElasticsearchClient client = new ElasticsearchClient(elasticSetting); + + + var tweet = new Tweet + { + Id = 1, + User = "stevejgordon", + PostDate = DateTime.Now, + Message = "Trying out the client, so far so good?" + }; + + var response = client.Index(tweet, "tweet-index"); + + if (response.IsValidResponse) + { + Console.WriteLine($"Index document with ID {response.Id} succeeded."); + } + + } + + [Fact] + public void GetDoc_Test() + { + var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200")) + .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A") + .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx")); + + ElasticsearchClient client = new ElasticsearchClient(elasticSetting); + + var tweet = new Tweet + { + Id = 2, + User = "stevejgordon", + PostDate = new DateTime(2009, 11, 15), + Message = "Trying out the client, so far so good?" + }; + + var response = client.Create(tweet, "my-tweet-index",3); + + if (response.IsValidResponse) + { + _output.WriteLine($"Index document with ID {response.Id} succeeded."); + } + } + } +} \ No newline at end of file diff --git a/ElasticSearchStudy.UnitTest/Usings.cs b/ElasticSearchStudy.UnitTest/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/ElasticSearchStudy.UnitTest/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/ElasticSearchStudy.sln b/ElasticSearchStudy.sln new file mode 100644 index 0000000..7d04d24 --- /dev/null +++ b/ElasticSearchStudy.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33516.290 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElasticSearchStudy.App", "ElasticSearchStudy.App\ElasticSearchStudy.App.csproj", "{215D6950-7B9F-4743-BD81-ACE3D7F1CAED}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElasticSearchStudy.Core", "ElasticSearchStudy.Core\ElasticSearchStudy.Core.csproj", "{86F7481E-5EBB-45A9-8621-F2CAA486D73A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElasticSearchStudy.UnitTest", "ElasticSearchStudy.UnitTest\ElasticSearchStudy.UnitTest.csproj", "{3008F5A9-43DA-48AF-883E-514FD71B65A2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {215D6950-7B9F-4743-BD81-ACE3D7F1CAED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {215D6950-7B9F-4743-BD81-ACE3D7F1CAED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {215D6950-7B9F-4743-BD81-ACE3D7F1CAED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {215D6950-7B9F-4743-BD81-ACE3D7F1CAED}.Release|Any CPU.Build.0 = Release|Any CPU + {86F7481E-5EBB-45A9-8621-F2CAA486D73A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {86F7481E-5EBB-45A9-8621-F2CAA486D73A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {86F7481E-5EBB-45A9-8621-F2CAA486D73A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {86F7481E-5EBB-45A9-8621-F2CAA486D73A}.Release|Any CPU.Build.0 = Release|Any CPU + {3008F5A9-43DA-48AF-883E-514FD71B65A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3008F5A9-43DA-48AF-883E-514FD71B65A2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3008F5A9-43DA-48AF-883E-514FD71B65A2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3008F5A9-43DA-48AF-883E-514FD71B65A2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8B5DC314-3806-4C06-ADF0-9342FE9F1DF0} + EndGlobalSection +EndGlobal