专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C#使用Elasticsearch(NEST)

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:19:07       共计:3623 浏览

本文介绍C#使用Elasticsearch的基本方法,并提供一个demo

以下说明中包含的http调用,为ElasticsearchTestController中编写的测试方法

初始化


引用NEST

创建ElasticClient对象


ElasticClient elasticClient = new ElasticClient(new ConnectionSettings(new Uri(address));




新增索引


关键代码


CreateIndexResponse createIndexResponse = await elasticClient.Indices.CreateAsync(indexName, createIndexDescriptor =>

{

   return createIndexDescriptor.

       Map(typeMappingDescriptor =>

       {

           return typeMappingDescriptor.Properties(propertiesSelector =>

           {

               foreach (PropertyInfo propertyInfo in typeof(StudentForElasticsearch).GetProperties())

               {

                   if (!propertyInfo.CanWrite)

                       continue;


                   switch (propertyInfo.PropertyType.Name)

                   {

                       case nameof(Int16):

                       case nameof(Int32):

                       case nameof(Int64):

                       case nameof(UInt16):

                       case nameof(UInt32):

                       case nameof(UInt64):

                       case nameof(Decimal):

                       case nameof(Single):

                       case nameof(Double):

                       case nameof(Byte):

                           propertiesSelector = propertiesSelector.Number(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));

                           break;


                       case nameof(Boolean):

                           propertiesSelector = propertiesSelector.Boolean(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));

                           break;


                       case nameof(DateTime):

                           propertiesSelector = propertiesSelector.Date(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));

                           break;


                       case nameof(String):

                           propertiesSelector = propertiesSelector.Keyword(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));

                           break;


                       default:

                           break;

                   }

               }


               return propertiesSelector;

           });

       });

});




为索引添加别名


PutAliasResponse putAliasResponse = await elasticClient.Indices.PutAliasAsync(indexName, indexAliasName);




调用http://localhost:5000/api/ElasticsearchTest/CreateIndex?indexName=stu&indexAliasName=stuAliasName创建索引


查询索引结构


GET

http://xxxxxxx:9200/stu/_mapping

返回结果  

{

   "stu": {

       "mappings": {

           "properties": {

               "email": {

                   "type": "keyword"

               },

               "id": {

                   "type": "float"

               },

               "name": {

                   "type": "keyword"

               }

           }

       }

   }

}


 


新增数据


关键代码


BulkResponse bulkResponse = await elasticClient.BulkAsync(bulkDescriptor =>

{

   foreach (StudentForElasticsearch document in datas)

   {

       bulkDescriptor = bulkDescriptor.Index<StudentForElasticsearch>(bulkIndexDescriptor =>

       {

           return bulkIndexDescriptor.Index(indexAliasName).Id(document.Id).Document(document);

       });

   }


   return bulkDescriptor;

});


调用http://localhost:5000/api/ElasticsearchTest/AddOrUpdateData?indexAliasName=stuAliasName新增数据


此时查询数据


GET

http://xxxxxxx:9200/stu/_search

{

   "from": 0,

   "size": 1000

}

返回结果

{

   "took": 33,

   "timed_out": false,

   "_shards": {

       "total": 1,

       "successful": 1,

       "skipped": 0,

       "failed": 0

   },

   "hits": {

       "total": {

           "value": 2,

           "relation": "eq"

       },

       "max_score": 1.0,

       "hits": [

           {

               "_index": "stu",

               "_type": "_doc",

               "_id": "1",

               "_score": 1.0,

               "_source": {

                   "id": 1,

                   "name": "Student1",

                   "email": "11111@qq.com"

               }

           },

           {

               "_index": "stu",

               "_type": "_doc",

               "_id": "2",

               "_score": 1.0,

               "_source": {

                   "id": 2,

                   "name": "Student2",

                   "email": "2222@qq.com"

               }

           }

       ]

   }

}


 


查询


关键代码


ISearchResponse<StudentForElasticsearch> searchResponse = await elasticClient.SearchAsync<StudentForElasticsearch>(searchDescriptor =>

{

   return searchDescriptor.Index(indexAliasName).Query(queryContainerDescriptor =>

   {

       return queryContainerDescriptor.Bool(boolQueryDescriptor =>

       {

           if (!string.IsNullOrEmpty(name))

           {

               IList<Func<QueryContainerDescriptor<StudentForElasticsearch>, QueryContainer>> queryContainers = new List<Func<QueryContainerDescriptor<StudentForElasticsearch>, QueryContainer>>();


               queryContainers.Add(queryContainerDescriptor =>

               {

                   return queryContainerDescriptor.Wildcard(c => c

                           .Field(ToJavaScriptPropertyName(nameof(StudentForElasticsearch.Name))).Value($"{name}*"));

               });

               boolQueryDescriptor.Must(x => x.Bool(b => b.Should(queryContainers)));

           }

           

           return boolQueryDescriptor;

       });

   })

   .From(0).Size(10);

});


 


这里采用Wildcard查询,并且只查询前10个匹配数据

调用http://localhost:5000/api/ElasticsearchTest/Get?indexAliasName=stuAliasName&name=Student2

返回结果


[{"id":2,"name":"Student2","email":"2222@qq.com"}]




删除数据


关键代码


BulkResponse bulkResponse = await elasticClient.BulkAsync(bulkDescriptor =>

{

   foreach (int id in deleteIds)

   {

       bulkDescriptor = bulkDescriptor.Delete<StudentForElasticsearch>(bulkIndexDescriptor =>

       {

           return bulkIndexDescriptor.Index(indexAliasName).Id(id);

       });

   }


   return bulkDescriptor;

});


 


调用http://localhost:5000/api/ElasticsearchTest/DeleteData?indexAliasName=stuAliasName

此时再查询数据,只剩下id为2的数据了

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:在C#中如何操作ElasticSearch的增册改查 | ·下一条:ElasticSearch学习增删改查、高亮、聚合、别名、重建索引

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有