服务器之家

服务器之家 > 正文

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

时间:2020-04-02 14:42     来源/作者:天风隼

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

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

elasticsearch插件elasticsearch-head安装:

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

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

Program.cs代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
   }
  }
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public class ESProvider
 {
  public static ElasticClient client = new ElasticClient(Setting.ConnectionSettings);
 
  public bool Index(PersonDetail person)
  {
   var client = new ElasticClient(Setting.ConnectionSettings);
   try
   {
    //添加数据
    //在调用下面的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%,但是这样的做法会导致在用完整的单词来搜索的时候搜索不到结果,所以我们需要使用下面的方式
 
   //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;
   //}
   //var searchResults = client.Search<Person>(s => s
   // .Index("zhixiao-application")
   // .Query(query)
   //);
 
   #endregion
 
   #region 指定属性搜索
 
   //使用term Query
   //Term是一个被索引的精确值,也就是说Foo, foo, FOO是不相等的,因此
   //在使用term query的时候要注意,term query在搜索的Field已经被索引的时候,是不支持大写的。
   // QueryContainer query2 = new TermQuery { Field = item.Key, Value = item.Value.ToLower() };
   //--------------------------------------------------------------------------------------
   //var searchResults = client.Search<PersonDetail>(s => s
   // .Index("elastic-search-app")
   // .Query(q => q.Term(t => t.OnField(f => f.LastName == "keyword")))
   //);
   //效果同上
   //QueryContainer termQuery = new TermQuery { Field = "lastname", Value = "keyword" };
   //var searchResults = client.Search<PersonDetail>(s => s
   // .Index("elastic-search-app")
   // .Query(termQuery)
   //);
   //--------------------------------------------------------------------------------------
   //使用 Query String query
   //QueryString query一般用于全文搜索,但是也可以用于单个属性的搜索(设置DefaultField属性),queryString query可以不区分大小写。QueryString还有一个好处就是我们可以搜索一个term中的一部分,
   //例如lastname为"t Boterhuis 1",那么我们可以用"terhuis"搜索到这个数据(虽然需要在外面包上**),在term query里面就做不到,因为ES把每一个属性的值都分析成一个个单独的term,提高了搜索的效率。
   //keyword = "t Boterhuis 2";
   //QueryContainer wholeWordQuery = new QueryStringQuery() { Query = keyword, DefaultOperator = Operator.And };
   //var searchResults = client.Search<PersonDetail>(s => s
   // .Index("elastic-search-app")
   // .Query(wholeWordQuery)
   //);
 
   #endregion
 
   return searchResults.Documents.ToList();
  }
 
  public List<PersonDetail> Sort(string keyword)
  {
   // 首先我们把原先的索引先删除了
   var response =
    client.DeleteIndex(
     new DeleteIndexRequest(new IndexNameMarker()
     {
      Name = "elastic-search-app",
      Type = typeof(PersonDetail)
     }));
 
   //然后重新创建索引
   var indexResult = client.CreateIndex("PD-application");
   var response1 = client.Map<PersonDetail>(m => m.MapFromAttributes());
   IEnumerable<PersonDetail> persons = new List<PersonDetail>
   {
    new PersonDetail()
    {
     Id = 4,
     FirstName = "Boterhuis-040",
     LastName = "Gusto-040",
    },
    new PersonDetail()
    {
     Id = 5,
     FirstName = "sales@historichousehotels.com",
     LastName = "t Boterhuis 1",
    },
    new PersonDetail()
    {
     Id = 6,
     FirstName = "Aberdeen #110",
     LastName = "sales@historichousehotels.com",
    },
    new PersonDetail()
    {
     Id = 7,
     FirstName = "Aberdeen #110",
     LastName = "t Boterhuis 2",
    },
   };
   foreach (var person in persons)
   {
    client.Index(person);
   }
   var searchResults = client.Search<PersonDetail>(s => s
    .Index("PD-application")
    .Sort(sort => sort.OnField(f => f.Id).Order(SortOrder.Ascending))
 
   );
   return searchResults.Documents.ToList();
  }
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public static class Util
 {
 
  //生成10000条sqlserver测试数据
  public static List<PersonDetail> Get10000PersonDetails()
  {
   var personDetailsList = new List<PersonDetail>();
    
   for (int i = 0; i < 10000; i++)
   {
    personDetailsList.Add(new PersonDetail()
    {
     FirstName = "FN" + new Random().Next(int.MaxValue),
     LastName = "LN" + new Random().Next(int.MaxValue)
    });
   }
   return personDetailsList;
  }
 
  //生成10000条ElasticSearch测试数据
  public static List<PersonDetail> Get10000PersonDetailsWithID()
  {
   var personDetailsList = new List<PersonDetail>();
    
   for (int i = 0; i < 10000; i++)
   {
    personDetailsList.Add(new PersonDetail()
    {
     Id = i * new Random().Next(99),
     FirstName = "FN" + new Random().Next(int.MaxValue),
     LastName = "LN" + new Random().Next(int.MaxValue)
    });
   }
   return personDetailsList;
  }
 
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static class Setting
 {
  public static Uri Node
  {
   get
   {
    return new Uri("http://localhost:9200");
   }
  }
  //连接配置
  public static ConnectionSettings ConnectionSettings
  {
   get
   {
    return new ConnectionSettings(Node, defaultIndex: "es-index-app");
   }
  }
 
 }

Model层代码:

?
1
2
3
4
5
6
public partial class PersonDetail
 {
  public long Id { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public partial class PersonContext : DbContext
 {
  static PersonContext()
  {
   Database.SetInitializer<PersonContext>(null);
  }
 
  public PersonContext()
   : base("Name=PersonContext")
  {
  }
 
  public DbSet<PersonDetail> PersonDetails { get; set; }
 
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
   //在重写OnModelCreating方法中则可以直接调用映射类,从而减少了OnModelCreating方法的复杂度,同时也增强了代码维护的可读性
   modelBuilder.Configurations.Add(new PersonDetailMap()); //属性映射约定
  }
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Fluent API配置Configuration映射类
 public class PersonDetailMap : EntityTypeConfiguration<PersonDetail>
 {
  public PersonDetailMap()
  {
   // 主键
   this.HasKey(t => new { t.Id, t.FirstName, t.LastName });
 
   // 属性
   this.Property(t => t.Id)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
 
   this.Property(t => t.FirstName)
    .IsRequired();
 
   this.Property(t => t.LastName)
    .IsRequired();
 
   // 表 & 列 映射
   this.ToTable("PersonDetails");
   this.Property(t => t.Id).HasColumnName("Id");
   this.Property(t => t.FirstName).HasColumnName("FirstName");
   this.Property(t => t.LastName).HasColumnName("LastName");
  }
 }

sqlserver脚本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
USE [Person]
GO
 
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE TABLE [dbo].[PersonDetails](
 [Id] [bigint] IDENTITY(1,1) NOT NULL,
 [FirstName] [nvarchar](max) NOT NULL,
 [LastName] [nvarchar](max) NOT NULL
) ON [PRIMARY]
 
GO

结果图:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部