Detailed explanation of getting started with .net Elasticsearch examples

零下一度
Release: 2018-05-29 11:16:31
Original
4635 people have browsed it

1. es installation related
1.elasticsearch installation
Run http://localhost:9200/
2.head plug-in
3.bigdesk plug-in installation
(Installation details Baidu: windows elasticsearch installation, details are available)

2. es plug-in related
www.searchtech.pro/elasticsearch-plugins (es plug-in collection)
(ik project)
(ES plug-in monitors node status and can also debug your ES query)
(2.1.1 + 1.6ik There is also Pinyin integrated with other tokenizers)
(plug-in similar to head)
www.elastic.co/downloads/marvel (monitoring ES health status)
konf plug-in (it is said that it can monitor the cluster Load)

三.es C# client example

1. Package download elasticsearch.net, nest component.
The nest component will depend on downloading the elasticsearch component.
Usage documentation:
Remarks: Relatively few people use es in .net, and the es version is updated very quickly. Many uses are based on their own guessing. Communicate more and share more.
2. Create a connection client

   public ElasticClient GetClient()
        {
            var node = new Uri("http://192.168.17.54:9200");
            var settings = new ConnectionSettings(
                node,
                defaultIndex: "my-application"
            );
            return new ElasticClient(settings);
        }
Copy after login

3. Create an index model (index structure)

[ElasticType(IdProperty = "Id", Name = "Person")]
        public class Person
        {
           [ElasticProperty(Name = "Id", Type = FieldType.String, Index = FieldIndexOption.NotAnalyzed)]
            public string Id { get; set; }
            public string Firstname { get; set; }
            public string Lastname { get; set; }
            public string[] Chains { get; set; }
           [ElasticProperty(Name = "content", Type = FieldType.String, Index = FieldIndexOption.Analyzed, Analyzer = "ik_max_word")]
            public string Content { get; set; }
        }
Copy after login

Note: For more detailed model examples provided by others, you can download the attachment.
4. Index content (create index)

private void btnIndex_Click(object sender, EventArgs e)
        {
            //var client = new ElasticsearchClient();
            ////index a document under /myindex/mytype/1
            //var indexResponse = client.Index("myindex", "mytype", "1", new { Hello = "World" });
            var client2 = GetClient();
            //client2.CreateIndex("test");
            //client2.Map<Person>(c => c.MapFromAttributes());
            IEnumerable<Person> persons = new List<Person>
            {
                new Person()
                {
                    Id = "4",
                    Firstname = "aaa",//Boterhuis-040
                    Lastname = "Gusto-040",
                    Chains = new string[]{ "a","b","c" },
                },
                new Person()
                {
                    Id = "5",
                    Firstname = "sales@historichousehotels.com",
                    Lastname = "t Boterhuis 1",
                    Chains = new string[]{ "a","b","c" },
                },
                new Person()
                {
                    Id = "6",
                    Firstname = "Aberdeen #110",
                    Lastname = "sales@historichousehotels.com",
                    Chains = new string[]{ "a","b","c" },
                },
                new Person()
                {
                    Id = "7",
                    Firstname = "Aberdeen #110",
                    Lastname = "t Boterhuis 2",
                    Chains = new string[]{ "a","b","c" },
                },
                 new Person()
                {
                    Id = "8",
                    Firstname = "Aberdeen #110",
                    Lastname = "t Boterhuis 2",
                    Chains = new string[]{ "a","b","c" },
                },
            };
            //foreach(var p in persons)
            client2.IndexMany<Person>(persons,"test");
        }
Copy after login

5. Simple search example

var client = GetClient();
            var rs = client.Search<Person>(s => s.Index("test").QueryString(this.textBox1.Text));
            this.richTextBox1.Text = JsonConvert.SerializeObject(rs.Documents);
Copy after login

6. Index update

 private void btnUpdate_Click(object sender, EventArgs e)
        {
            var client2 = GetClient();
            client2.Update<Person, object>(u => u
             .Index("test")
            .Id(4)
            .Doc(new { Id="4", Firstname = "United States" })
            .RetryOnConflict(3)
            .Refresh()
           );
            //var u1 =  new Person()
            //    {
            //        Id = "4",
            //        Firstname = "Boterhuis-040",
            //        Lastname = "Gusto-040",
            //        Chains = new string[]{ "a","b","c" },
            //    };
            //var u2 = new Person()
            //    {
            //        Id = "4",
            //        Firstname = "United States",
            //        Lastname = "Gusto-040",
            //        Chains = new string[] { "a", "b", "c" },
            //    };
            //client2.Update<Person,Person>(u1,u2).
        }
Copy after login

7. Index deletion

private void btnDelete_Click(object sender, EventArgs e)
        {
            var client2 = GetClient();
            client2.DeleteIndex("test");
            client2.DeleteIndex("my-application");
        }
Copy after login

8. 总结
以上示例代码,简单的应用已经足够用。其他的就是高亮和分组。可以看文档。
四. es 集群
 es 默认是一个集群,相对solr云来说配置更简单,搭建更方便些。但是更多还是要根据业务进行自己的集群设计还是好费很多时间,很多精力。(上手容易,用好难)
五. es 与solr 对比
个人目前了解的:
原来solr资料比较多,现在貌似es的资料更多一点。solr是官方英文pdf,es也是英文的。
原来solr还有中文书籍,现在貌似没有了。es 目前还有几本书籍,但是讲的es版本略有老旧。
solr上手相对es略微难些。
(n年前,我用的是solr,那时候还没有solrcloud;es还没有出来,那时候solr资料反而多。个人也实现了自己的solr集群方案。其他的功能上的对比,还是百度,不重复。)
六. 如果es客户端调试请求
建议下载HTTPAnalyzer之类的tcp拦截工具。这样可以拦截验证sdk出来的请求连接,对比资料和书籍看下哪些参数写错了,对调试很有帮助。
七. es 附录
es术语介绍:
cluster:
代 表一个集群,集群中有多个节点,其中有一个为主节点。这个主节点是可以通过选举产生的。注意,主从节点是对于集群内部来说的。es的一个概念就是去中心 化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看es集群,在逻辑上是个整体,你与任何一个节点的通信和与整个es集群通信是等价 的。
shards
代表索引分片。es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。
replicas
代表索引副本,es可以设置多个索引的副本。副本的作用,一是提高系统的容错性,当某个节点的某个分片损坏或丢失时可以从副本中恢复,二是提高es的查询效率,es会自动对搜索请求进行负载均衡。
recovery
代表数据恢复或叫数据重新分布,es在有节点加入或退出时会根据机器的负载对索引分片进行重新分配,挂掉的节点重新启动时也会进行数据恢复。
river
代表es的一个数据源,也是其他存储方式(如:数据库)同步数据到es的一个方法。它是以插件方式存在的一个es服务,通过读取river中的数据并把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。
gateway
代 表es索引快照的存储方式。es默认是先把索引存放到内存中,当内存满了时再持久化到本地硬盘。gateway对索引快照进行存储,当这个es集群关闭再 重新启动时,就会从gateway中读取索引备份数据。es支持多种类型的gateway,有本地文件系统(默认),分布式文件系统,Hadoop的 HDFS和amazon的s3云存储服务。
discovery.zen
代表es的自动发现节点机制。es是一个基于p2p的系统,它先通过广播寻找存在的节点,再通过多播协议来进行节点之间的通信,同时也支持点对点的交互。
Transport
代表es内部节点或集群与客户端的交互方式。默认内部是使用tcp协议进行交互,同时它支持http协议(json格式)、thrift、servlet、memcached、zeroMQ等的传输协议(通过插件方式集成)。

The above is the detailed content of Detailed explanation of getting started with .net Elasticsearch examples. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template