本示例采用Elasticsearch+Nest
网上查了很多资料,发现用C#调用Elasticsearch搜索引擎的功能代码很分散,功能不完整,多半是非常简单的操作,没有成型的应用示例。比如新增或修改索引,都是发起一个request新增或修改一条数据,当一次性修改几千条数据时,发起的requst请求过多容易导致429 Too Many Request的错误,单个新增修改索引就非常不适用。其实Nest有批量新增、修改索引的功能,批量删除也可以。现将项目中采用Elasticsearch的C#代码分享如下:
使用NEST客户端
1.连接
- public ElasticClient GetElasticClient(string esServer, string IndexName)
- {
- ElasticClient client = null;
- string[] server = esServer.Split(',');
- Uri[] nodes = new Uri[server.Length];
- for (int i = 0; i < server.Length;i++ )
- {
- nodes[i] = new Uri(server[i]);
- }
- var connectionPool = new StaticConnectionPool(nodes);
- var settings = new ConnectionSettings(
- connectionPool
- );
- settings.DefaultIndex(IndexName);
- client = new ElasticClient(settings);
- return client;
- }
2.添加索引
- var indexExist = client.IndexExists(IndexName);
- if (!indexExist.Exists)
- {
- //基本配置
- IIndexState indexState = new IndexState()
- {
- Settings = new IndexSettings()
- {
- NumberOfReplicas = 1,//副本数
- NumberOfShards = 6//分片数
- }
- };
- //ICreateIndexResponse response = client.CreateIndex(IndexName, p => p.Mappings(m => m.Map<ES_PUB_Stock>(mp => mp.AutoMap())));
- ICreateIndexResponse response = client.CreateIndex(IndexName, p => p
- .InitializeUsing(indexState)
- .Mappings(ms =>
- ms.Map<ES_PUB_Stock>(m =>
- m.AutoMap()
- .Properties(ps =>
- ps.Nested<ES_PUB_StockPrice>(n =>
- n.Name(c => c.stockPrice)
- )
- .Nested<ES_PUB_SpecValue>(q=>
- q.Name(c=>c.specValue))))));
- if (response.IsValid)
- {
- string msg = string.Format("索引创建" + IncrementIndexName + "成功!");
- this.WriteLog(msg);
- }
- else
- {
- string msg = string.Format("索引创建" + IncrementIndexName + "失败!");
- this.WriteLog(msg);
- Thread.CurrentThread.Abort();
- }
- }
这里创建索引设置6个分片数,并Mapping自定义的结构。
Mapping相关类型如下:
- <span style="font-size:14px;">using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Nest;
- namespace SearchMaker.Model.ES.PUB
- {
- [ElasticsearchType(IdProperty = "sid", Name = "ES_PUB_Stock")]
- public class ES_PUB_Stock
- {
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public long? sid { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string model { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string brand { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string encapsulation { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string batchNo { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int invQty { get; set; }
- [Date(Format = "yyyy-MM-dd HH:mm:ss")]
- public string updateTime { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int upByMemberID { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string upByMemberName { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string guid { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public decimal? price { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int? leastQty { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int limitTime { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string proImg { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string categoryNO { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string proRemark { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int type { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int sendToday { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int pickedToday { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string categoryName { get; set; }
- [Object(Path = "specValue")]
- public List<ES_PUB_SpecValue> specValue { get; set; }
- [Object(Path = "stockPrice")]
- public List<ES_PUB_StockPrice> stockPrice { get; set; }
- [String(Analyzer = "ik", Index = FieldIndexOption.Analyzed)]
- public string specsName { get; set; }
- [String(Analyzer = "ik", Index = FieldIndexOption.Analyzed)]
- public string keyword { get; set; }
- [String(Analyzer = "ik", Index = FieldIndexOption.Analyzed)]
- public string modelAS { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int modelLength { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public decimal score { get; set; }
- }
- [ElasticsearchType(Name = "ES_PUB_StockPrice")]
- public class ES_PUB_StockPrice
- {
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public int? minval { get; set; }
- [Number(Index = NonStringIndexOption.NotAnalyzed, DocValues = false, IgnoreMalformed = true, Coerce = true)]
- public decimal? price { get; set; }
- }
- [ElasticsearchType(Name = "ES_PUB_Specs")]
- public class ES_PUB_SpecValue
- {
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string spec { get; set; }
- [String(Index = FieldIndexOption.NotAnalyzed)]
- public string value { get; set; }
- }
- }
- </span>
这里中文分词采用IK分词
3.单个新增、修改索引
- <span style="font-size:14px;"> private int CreateIndex(ES_PUB_Stock param)
- {
- int indexCnt = 0;
- if (param.sid.GetValueOrDefault(0) > 0)
- {
- var response = client.Index<ES_PUB_Stock>(param, i => i.Index(IndexName).Type(IndexType));
- if (response.IsValid)
- {
- indexCnt++;
- }
- else
- {
- this.WriteWarmessage("创建" + IncrementIndexName + "索引,发生异常:SID:" + param.sid.ToString() + "," + response.DebugInformation, "");
- }
- }
- return indexCnt;
- }</span>
4.批量新增、修改索引
- <span style="font-size:14px;"><span style="white-space:pre"> </span>BulkDescriptor descriptor = new BulkDescriptor();</span><pre name="code" class="csharp"><span style="font-size:14px;"><span style="white-space:pre"> </span>descriptor.Index<ES_PUB_Stock>(op => op.Document(esStock));</span></pre><pre name="code" class="csharp"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;"><span style="white-space:pre"> </span> private int CreateIndex(BulkDescriptor param)</span></span><pre name="code" class="csharp"> {</pre><pre name="code" class="csharp"> int count = 0;
- var result = client.Bulk(param);
- if (!result.Errors)
- count = result.Items.Count();
- return count;
- }</pre>
- <pre></pre>
- <pre></pre>
- <p></p>
- <pre></pre>
- <pre></pre>
- <p></p>
- <p><span style="font-size:14px"><span style="white-space:pre"></span><strong>5.单个删除索引</strong></span></p>
- <pre name="code" class="csharp"><span style="font-size:14px;"> private int DeleteIndex(ES_PUB_Stock param)
- {
- int indexCnt = 0;
- if (param.sid.GetValueOrDefault(0) > 0)
- {
- var response = client.Delete<ES_PUB_Stock>(param.sid, i => i.Index(IndexName).Type(IndexType));
- if (response.IsValid)
- {
- indexCnt++;
- }
- }
- return indexCnt;
- }</span></pre><span style="font-size:14px"><br>
- </span>
- <p></p>
- <p><span style="font-size:14px">按上方所述,即可实现C#对Elasticsearch的操作。</span></p>
- <p><span style="font-size:14px">NEST对Elasticsearch的结构化查询,待下篇......<br>
- </span><br>
- </p>
- <pre></pre>
- <pre></pre>
- </pre>