elasticsearch数据库使用
elasticsearch的一个最为显著的优点:快速全文检索。关于elasticsearch 全文检索的原理,请看:https://blog.csdn.net/wolfcode_cn/article/details/81907195
一、es 安装
参考官网安装教程:https://www.elastic.co/guide/en/elasticsearch/reference/6.6/zip-targz.html
教程中介绍了多种操作系统多种安装方式:
本篇选择的是linux操作系统下,手动下载安装包的方式安装:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.tar.gz wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.tar.gz.sha512 shasum -a 512 -c elasticsearch-6.6.2.tar.gz.sha512 tar -xzf elasticsearch-6.6.2.tar.gz cd elasticsearch-6.6.2/
安装完毕之后,测试是否成功:
./bin/elasticsearch
此开启方式是前台开启,如果要后台运行
./bin/elasticsearch &
curl -X GET "localhost:9200/"
得到下面的信息:
下面设置在守护进程中运行,开机可以自启动es:
[program:elasticsearch] command=/home/shippingadmin/elasticsearch-6.6.2/bin/elasticsearch startsecs=10 autostart=true autorestart=true stderr_logfile=/var/log/applogs/elasticsearch.err.log stdout_logfile=/var/log/applogs/elasticsearch.out.log user=shippingadmin stopsignal=INT
二、es 数据库操作
引入 nuget 包
Install-Package NEST
连接es代码:
public static ElasticClient Client; /// <summary> /// 初始化es连接 /// </summary> public static void Init() { //单节点 var node = new Uri("http://192.168.1.23:8920"); //指定默认索引是可选的,但如果没有为给定请求推断索引,NEST可能会引发异常,这里的默认索引是skuindex var settings = new ConnectionSettings(node) .DefaultMappingFor<Sku>(m => m.IndexName("skuitem")); //使用与群集中的三个Elasticsearch节点的地址对接的SniffingConnectionPool,并且客户端将使用此类型的池来维护可以以循环方式发送请求的群集中的可用节点列表 //var uris = new[] //{ // new Uri("http://localhost:9200"), // new Uri("http://localhost:9201"), // new Uri("http://localhost:9202"), //}; //var connectionPool = new SniffingConnectionPool(uris); //var settings = new ConnectionSettings(connectionPool) // .DefaultMappingFor<Sku>(m => m.IndexName("SKU")); Client = new ElasticClient(settings);
创建索引:
/// <summary> /// 创建索引,并建立POCO映射关系,这在es第一次插入sku数据时,是必要的,重复插入并不会报错 /// </summary> /// <returns></returns> public static bool CreateIndex() { var createIndexResponse = Client.CreateIndex("skuitem", c => c .Mappings(ms => ms .Map<Sku>(m => m.AutoMap()) ) ); return createIndexResponse.IsValid; }
定义Entity:
[ElasticsearchType(IdProperty = "Id")] public class Sku { public System.Guid Id { get; set; } [Keyword] public string Code { get; set; } [Keyword] public string SerialNumber { get; set; } public List<ItemTitle> SearchTitles { get; set; } } public class ItemTitle { public Guid Id { get; set; } [Keyword] public string Title { get; set; } [Keyword] public string CultureCode { get; set; } }
插入数据,并做查询:
static void Main(string[] args) { SkuManager.Init(); SkuManager.CreateIndex(); List<Sku> skus = new List<Sku>() { new Sku() { Id=Guid.NewGuid(), Code="sku1", SerialNumber="abc1234", SearchTitles=new List<ItemTitle>() { new ItemTitle() { Id=Guid.NewGuid(), CultureCode="cn", Title="ddkdkdkdkdkdkdk", }, new ItemTitle() { Id=Guid.NewGuid(), CultureCode="en", Title="ddkdkdkdkdkdkdk", } } }, new Sku() { Id=Guid.NewGuid(), Code="sku2", SerialNumber="abc1dddfd234", SearchTitles=new List<ItemTitle>() { new ItemTitle() { Id=Guid.NewGuid(), CultureCode="cn", Title="ddkdkdkdkdkdksdfsdk", }, new ItemTitle() { Id=Guid.NewGuid(), CultureCode="en", Title="ddkdkdksdfdkdkdkdk", } } } }; var indexResult = SkuManager.Client.IndexMany<Sku>(skus); var searchResult1 = SkuManager.Client.Search<Sku>(s => s .From(0) .Size(5) .Query(q => q .Match(m => m .Field(f => f.SerialNumber) .Query("fd") ) ) ); var query = new List<Func<QueryContainerDescriptor<Sku>, QueryContainer>>(); query.Add(sku => sku.Wildcard(tm => tm.Field(f => f.SerialNumber).Value("*fd*"))); query.Add(sku => sku.Term(tm => tm.Field(f => f.Code).Value("sku2"))); var searchResult2 = SkuManager.Client.Search<Sku>(s => s .Query(q => q.Bool(b => b.Must(query)) ) ).Documents.ToList(); }
总结:1.es是用java代码编写的数据存储以及提供了丰富的数据操作API,所以在windows或者linux安装es之前,是需要先安装java sdk,并设置环境变量;关于环境变量的文章请参考:
https://www.cnblogs.com/jasonzeng/p/8302171.html
https://blog.csdn.net/u012498149/article/details/78771729
2.es相对应的客户端是 kibana,提供了强大的数据查询,图表,统计等功能,后面可能要针对 kibana作一次学习总结;
3.现在es最新版本,7.x已经出来,安装完之后,用elasticsearch.net,是不兼容的,因为elasticsearch.net,只支持到 6.x版本;
4.关于elasticsearch.net,更多的数据库操作,请看官网,其中有low lever client, high lever client, 本篇用的是high level client: https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/nest.html