Elasticsearch.Net使用入门教程(1)

编程学习 2021-07-04 22:41www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了Elasticsearch.Net使用入门教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Elasticsearch.Net使用教程,供大家参考,具体内容如下

首先去官网下载Elasticsearch 2.3.4安装包,解压后,在cmd命令行进入安装目录,再进入 bin目录,运行elasticsearch.bat命令。

elasticsearch插件elasticsearch-head安装:

bin目录下执行命令plugin -install mobz/elasticsearch-head

然后开始.net编程,构建控制台应用程序

Program.cs代码如下:

class Program 
 { 
  static void Main(string[] args) 
  { 
   Console.WriteLine("*Program 开始运行 : " + DateTime.Now); 
   var business = new Business(); 
 
   var swRead = new Stopwatch(); 
   //swRead.Start(); 
   //business.AddToDb();//sqlserver数据库增加数据 
   //swRead.Stop(); 
   //Console.WriteLine("DB 写入时间 : " + swRead.ElapsedMilliseconds); 
 
   //swRead.Reset(); 
   //swRead.Start(); 
   //business.AddToElasticIndex(); 
   //swRead.Stop(); 
   //Console.WriteLine("ES 写入时间 : " + swRead.ElapsedMilliseconds); 
    
   var sw = new Stopwatch(); 
   sw.Start(); 
   var personsFromDB = business.GetFromDB(); 
   sw.Stop(); 
   Console.WriteLine("DB 读时间 : " + sw.ElapsedMilliseconds); 
    
   sw.Reset(); 
   sw.Start(); 
   var personsFromEs = business.GetFromES(); 
   sw.Stop(); 
   Console.WriteLine("ES 读时间 : " + sw.ElapsedMilliseconds); 
 
   Console.ReadLine(); 
  } 
 } 

BLL层的Business.cs类:

public class Business 
 { 
  private List<PersonDetail> _personList = new List<PersonDetail>(); 
   
  //SQLSERVER数据库 
  PersonDbProvider dbProvider = new PersonDbProvider(); 
 
  //ElasticSearch 
  ESProvider esProvider = new ESProvider(); 
 
  public void AddToDb() 
  { 
   _personList = Util.Get10000PersonDetails();//辅助类,生成10000条数据 
 
   foreach (var personDetail in _personList) 
   { 
    dbProvider.AddPerson(personDetail); 
   } 
  } 
 
  public void AddToElasticIndex() 
  { 
   _personList = Util.Get10000PersonDetailsWithID(); 
   foreach (var personDetail in _personList) 
   { 
    esProvider.Index(personDetail); 
   } 
  } 
 
  public List<PersonDetail> GetFromDB() 
  { 
   return dbProvider.GetAllPersonDetails(); 
  } 
 
  public List<PersonDetail> GetFromES() 
  { 
   return esProvider.GetAll(); 
  } 
 
 } 

PersonDbProvider.cs和ElasticSearchProvider.cs以及Util.cs,Setting.cs类:

public class PersonDbProvider 
 { 
  public bool AddPerson(PersonDetail personDetail) 
  { 
   try 
   { //数据库上下文 
    using (var db = new PersonContext()) 
    { 
     db.PersonDetails.Add(personDetail); 
     db.SaveChanges(); 
     return true; 
    } 
   } 
   catch (Exception) 
   { 
    return false; 
   } 
  } 
 
  public List<PersonDetail> GetAllPersonDetails() 
  { 
   try 
   { 
    using (var db = new PersonContext()) 
    { 
     return db.PersonDetails.ToList(); 
    } 
   } 
   catch (Exception) 
   { 
    return null; 
   } 
  } 
 } 
public class ESProvider 
 { 
  public static ElasticClient client = new ElasticClient(Setting.ConnectionSettings); 
 
  public bool Index(PersonDetail person) 
  { 
   var client = new ElasticClient(Setting.ConnectionSettings); 
   try 
   { 
    //添加数据 
    //在调用狼蚁网站SEO优化的index方法的时候,如果没有指定使用哪个index,ElasticSearch会直接使用我们在setting中的defaultIndex,如果没有,则会自动创建 
    var index = client.Index(person); 
    return index.Created; 
   } 
   catch (Exception ex) 
   { 
    Console.WriteLine(" Excepton Message : " + ex.Message); 
   } 
   return false; 
  } 
 
 
  public List<PersonDetail> GetAll() 
  { 
   var searchResults = client.Search<PersonDetail>(s => s 
    .From(0) 
    .Size(10000) 
    ); 
   return searchResults.Documents.ToList(); 
  } 
 
  public List<PersonDetail> GetEntities(string keyword) 
  { 
   var client = new ElasticClient(Setting.ConnectionSettings); 
 
   #region 全文搜索 
 
   keyword = String.Format("*{0}*", keyword); 
   //默认的Operator是Or,当keyword是类似于"One Two"之类的中间有空格的时候,会被当成两个关键词搜索,然后搜索结果进行or运算 
   //所以我们需要根据需求来调整Operator 
   var searchResults = client.Search<PersonDetail>(s => s 
    .Index("elastic-search-app") 
    .Query(q => q.QueryString(qs => qs.Query(keyword).DefaultOperator(Operator.And))) 
    ); 
 
   //-------------------------------------------------------------------------------------- 
   //另外由于ES是分词搜索,所以当我们要用"One"来搜索完整的单词"JustOne"的时候,就必须在"One"外面添加**,类似于SQL里面的%keyword%,但是这样的做法会导致在用完整的单词来搜索的时候搜索不到结果,所以我们需要使用狼蚁网站SEO优化的方式 
 
   //wholeKeyword = keyword; 
   //keyword = String.Format("*{0}*", keyword); 
   //QueryContainer query = new QueryStringQuery() { Query = keyword, DefaultOperator = Operator.And }; 
   //if (!String.IsNullOrEmpty(wholeKeyword)) 
   //{ 
   // QueryContainer wholeWordQuery = new QueryStringQuery() { Query = wholeKeyword }; 
   // query = query || wholeWordQuery; 
   / 

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板