ElasticSearch学习总结3(.NetCore操作ES)

首先将ElasticSearch服务运行起来。

使用.NetCore操作ElasticSearch数据库实例:

1.创建一个.NetCore控制台程序

2.通过NuGet添加程序集

 3.床架TestData类,用于数据操作

using Nest;
using System;
using System.Collections.Generic;

namespace ElasticSearch
{
    public class TestData
    {
        /// <summary>
        /// 插入多条数据
        /// </summary>
        public void IndexMany()
        {
            //链接es数据库
            var settings = new ConnectionSettings(new Uri(Url.url)).DefaultIndex("people");//people 索引库名称

            //创建链接es数据库的管道
            var client = new ElasticClient(settings);

            List<Person> peoples = new List<Person>();

            for (int i = 0; i < 10; i++)
            {
                Person person = new Person()
                {
                    Id = i,
                    FirstName = "Fname" + i,
                    LastName = "Lname" + i
                };
                peoples.Add(person);
            }

            client.IndexMany<Person>(peoples);
        }

    }

    public class TraceInfo
    {
        public string RpcID { get; set; }
        public string message { get; set; }
        public DateTime Time { get; set; }
    }

    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class Url
    {
        public static string url = "http://localhost:9200/";
    }
}

4.在main方法中调用testData类的IndexMany方法,一次插入多条记录。

using System;

namespace ElasticSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            TestData testData = new TestData();
            testData.IndexMany();

            Console.WriteLine("ok");
            Console.ReadLine();
        }
    }
}

程序执行结果: 

通过kibana工具查询数据:

 可以看到多了名称为people的索引库。

下面查询people索引库中的记录:

 可以看到通过程序插入的记录都在里面了。

在TestData类中加入数据查询方法:

        /// <summary>
        /// 查询数据
        /// </summary>
        public void SearchData()
        {
            //链接es数据库
            var settings = new ConnectionSettings(new Uri(Url.url)).DefaultIndex("people");//people 索引库名称

            //创建链接es数据库的管道
            var client = new ElasticClient(settings);

            var searchPesponse = client.Search<Person>(s => s
                .From(0)
                .Size(10)
                .Query(q => q
                   .Match(m => m
                   .Field(f => f.FirstName)
                   .Query("Fname1")
                   )
                )
            );

            var doc = searchPesponse.Documents;
            Console.WriteLine("查询结果");
            foreach (var item in doc)
            {
                Console.WriteLine($"id:{item.Id},firstname:{item.FirstName},lastname:{item.LastName}");
            }
        }

在main方法中调用该方法:

 执行结果:

可以看到插入到es数据库中的数据,被查询出来了。 

通过查询官方文档,可以学习更全面的操作:

https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest-getting-started.html

猜你喜欢

转载自blog.csdn.net/liangmengbk/article/details/120298223