Nest查询示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elasticsearch.Net;
using Nest;

namespace ElasticSearchClinet_NEST
{
    class Program
    {
        static void Main(string[] args)
        {
            new TestElasticSearch().TestRequest();
        }
    }

    public class TestElasticSearch
    {
        /// <summary>
        /// 生成搜索客户端
        /// </summary>
        /// <returns></returns>
        private ElasticClient GetSearchClient()
        {
            //var connectString = ConfigurationManager.ConnectionStrings["ElasticSearch"].ConnectionString;
            var nodes = new Uri[]
            {
                new Uri("http://a.elk.qaem.cn:9200"),
                new Uri("http://b.elk.qaem.cn:9200"),
                new Uri("http://c.elk.qaem.cn:9200"),
                new Uri("http://d.elk.qaem.cn:9200"),
            };
            var pool = new StaticConnectionPool(nodes);
            var settings = new ConnectionSettings(pool);
            var client = new ElasticClient(settings);
            return client;
        }

        public string TestRequest()
        {
            var client = this.GetSearchClient();
            var person = new Person
            {
                Id = "1",
                Firstname = "Martijn",
                Lastname = "Laarman"
            };

            var index = client.Index(person, i => i
                .Index("personindex")
                .Type("persontype")
                .Id("1")
                .Refresh()
                .Ttl("1m")
            );

            var results = client.Search<Person>(s => s
                .From(0)
                .Size(10)
                .Query(q => q
                     .Term(p => p.Firstname, "martijn")
                )
            );

            foreach (var item in results.Hits)
            {
                var p = item.Source;
                Console.WriteLine(p.Id);
                Console.WriteLine(p.Firstname);
                Console.WriteLine(p.Lastname);
            }
            Func<MatchPhraseQueryDescriptor<Person>, IMatchQuery> selector = q =>
            {
                return q.Query(@"{
                      ""query"": {
                        ""match_phrase"": {
                                    ""ZuHeName"": ""基金""
                        }
                            }
                        }");
            };
            Func<SearchDescriptor<ZuHeEntity>, ISearchRequest> search = s => s
              .Query(
                q=>q.MatchPhrase(t=>t.Field("ZuHeName").Query("基金"))
                ).From(0)
                .Size(10);

            var zuheList = client.Search<ZuHeEntity>(search);

            foreach (var item in zuheList.Hits)
            {
                var p = item.Source;
                Console.WriteLine(p.ZuHeId);
                Console.WriteLine(p.ManagerName);
                Console.WriteLine(p.ZuHeName);
            }
            Console.ReadKey();
            return results.Hits.Count().ToString();
        }
    }


    public class Person
    {
        public string Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
    }

    public class ZuHeEntity
    {

        /// <summary>
        /// 组合Id
        /// </summary>
        public string ZuHeId
        {
            get;
            set;
        }

        /// <summary>
        /// 管理人Id
        /// </summary>
        public string ManagerId
        {
            get;
            set;
        }

        /// <summary>
        /// 组合创建时间
        /// </summary>
        public DateTime StartDate
        {
            get;
            set;
        }

        /// <summary>
        /// 组合名
        /// </summary>
        public string ZuHeName
        {
            get;
            set;
        }

        /// <summary>
        /// 组合管理人名
        /// </summary>
        public string ManagerName
        {
            get;
            set;
        }

        public string Permit
        {
            get; set;
        }
    }


}

http://nest.azurewebsites.net/nest/cluster/health.html

posted on 2016-03-23 21:05  Anthony.Zhao  阅读(1812)  评论(0编辑  收藏  举报