码迷,mamicode.com
首页 > Web开发 > 详细

.net core 如何向elasticsearch中创建索引,插入数据。

时间:2020-07-26 19:09:34      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:插入数据   安装和使用   void   ons   界面   集群   datetime   运行程序   document   

首先我们要建一个webapi工程,然后下载两个插件:

 1,Nest        2,Elasticsearch.Net

技术图片

 

 

技术图片

 

 下载后,下面就上代码了,首先是elasticsearchHelp帮助类

using Elasticsearch.Net;
using Nest;
using System;
namespace ESAPI.Common
{
    public static class ElasticSearchHelper
    {
public static readonly string url = "http://ip:9200/";//这个是elasticsearch远程访问ip public static void insert(object t,string index) { //设置连接字符串,DefaultIndex中的表名要小写 var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(index); var client = new ElasticClient(settings); var doc = t; //通过 IndexDocument() 方法插入数据 var ndexResponse = client.IndexDocument(doc); } /// <summary> /// 单点链接到ElasticSearch /// </summary> /// <param name="url">ElasticSearch的ip地址</param> /// <returns></returns> public static ElasticClient OneConnectES(string url) { var node = new Uri(url); var settings = new ConnectionSettings(node); var client = new ElasticClient(settings); return client; } /// <summary> /// 指定多个节点使用连接池链接到Elasticsearch集群 /// </summary> /// <param name="serverurl">链接ip数组</param> /// <returns></returns> public static ElasticClient ManyConnectES(string[] serverurl) { Uri[] nodes = new Uri[serverurl.Length]; for (int i = 0; i < serverurl.Length; i++) { nodes[i] = new Uri(serverurl[i]); } var pool = new StaticConnectionPool(nodes); var settings = new ConnectionSettings(pool); var client = new ElasticClient(settings); return client; } /// <summary> /// 创建索引 /// </summary> /// <param name="elasticClient"></param> public static CreateIndexResponse CreateIndex(this IElasticClient elasticClient, string indexName, int numberOfReplicas = 1, int numberOfShards = 5) { IIndexState indexState = new IndexState { Settings = new IndexSettings { NumberOfReplicas = numberOfReplicas, // [副本数量] NumberOfShards = numberOfShards } }; Func<CreateIndexDescriptor, ICreateIndexRequest> func = x => x.InitializeUsing(indexState).Map(m => m.AutoMap()); CreateIndexResponse response = elasticClient.Indices.Create(indexName, func); return response; } } }

控制器代码:

using System;
using ESAPI.Common;
using Microsoft.AspNetCore.Mvc;
namespace ESAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
         /// <summary>
         /// 删除地址
         /// </summary>
         /// <param name="id"></param>
         [HttpGet("InsertLog")]
         public void InsertLog()
         {
//插入200条数据
for (int i = 0; i < 200; i++) { var d = new { Time = DateTime.Now, Num = 5, Name = "12313", info = "hello world!" }; ElasticSearchHelper.insert(d, "demo"); } } } }

运行程序:

技术图片

 

 查看elasticsearch可视化界面:

技术图片

 

很显然,插入成功了!既然是ELK,后续我还会对logstash,Kibana的安装和使用也进行详细的介绍,下一篇,会详细介绍logstash的安装和使用!

 

.net core 如何向elasticsearch中创建索引,插入数据。

标签:插入数据   安装和使用   void   ons   界面   集群   datetime   运行程序   document   

原文地址:https://www.cnblogs.com/zpy1993-09/p/13380197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!