1. 程式人生 > >elasticsearch_.net_client_nest2.x_到_5.x常用方法屬性差異

elasticsearch_.net_client_nest2.x_到_5.x常用方法屬性差異

tel ofo 分片 ron ota 屬性 5% new 結果

變更1 字段類型string

  • 2.x中僅有String類型,可設置是否分詞、是否索引及分詞使用的分詞器。
  • 5.x中拆分為Keyword類型與Text類型,Keyword 不分詞,可設置是否索引;Text分詞,可設置是否索引及分詞使用的分詞器。
            [String(Name = "Name",Index = FieldIndexOption.NotAnalyzed)]
            public string Name { get; set; }
     
            [String(Name = "Dic", Index = FieldIndexOption.Analyzed,Analyzer = "
    ik_max_word")] /// <summary> /// keyword 不分詞 /// </summary> [Keyword(Name = "Name", Index = true)] public string Name { get; set; } /// <summary> /// text 分詞,Analyzer = "ik_max_word" /// </summary> [Text(Name = "Dic"
    , Index = true, Analyzer = "ik_max_word")] public string Dic { get; set; }

    註:5.x已將string標記為過時特性,下個版本將移除;

    變更2 滾動掃描獲取全量數據

    滾動掃描支持並行執行提高獲取數據效率;

    2.x

    public void SearchScanScroll(string indexName)
            {
                string scrollid = "";
                var result = _client.Search<TestModel2>(s => s.Index(indexName).Query(q => q.MatchAll())
                    .Size(
    10) //todo:2x中的SearchType.Scan,5.x移除 .SearchType(SearchType.Scan) .Scroll("1m")); scrollid = result.ScrollId; int count = 0; while (true) { //得到滾動掃描的id //執行滾動掃描得到數據 返回數據量是 result.Shards.Successful*size(查詢成功的分片數*size) var result1 = _client.Scroll<TestModel2>("1m", scrollid); if(result1.Documents==null || !result1.Documents.Any()) break; foreach (var info in result1.Documents) { count++; Console.WriteLine(info.Id+" - "+ count); } //得到新的id scrollid = result1.ScrollId; } }

    5.x

    Action<int> sc1 = (id) =>
                {
                    string scrollid = "";
                    //todo:5.x 多了Slice設置 移除SearchType.Scan
                    var result = _client.Search<TestModel5>(s => s.Index(indexName).Query(q => q.MatchAll())
                        .Size(15)
                        .Sort(st=>st.Descending(ds=>ds.Id))
                        .Scroll("1m")
                        //id從0開始 0,1,2...
                        //length=max
                        //例:max=3 id=0,id=1,id=2
                        .Slice(sl => sl.Id(id).Max(3))
                        );
     
                    //得到滾動掃描的id
                    scrollid = result.ScrollId;
                    foreach (var info in result.Documents)
                    {
                        Console.WriteLine(info.Id + " - " + " -批次count " + result.Documents.Count + " - 線程"+Thread.CurrentThread.ManagedThreadId);
                    }
                    while (true)
                    {
                        //執行滾動掃描得到數據 返回數據量是 result.Shards.Successful*size(查詢成功的分片數*size)
                        var result1 = _client.Scroll<TestModel5>("1m", scrollid);
                        if (result1.Documents == null || !result1.Documents.Any())
                            break;
                        foreach (var info in result1.Documents)
                        {
                            Console.WriteLine(info.Id + " - " +" -批次count "+ result1.Documents.Count+ " - 線程" + Thread.CurrentThread.ManagedThreadId);
                        }
                        //得到新的id
                        scrollid = result1.ScrollId;
                    }
                };
               var t1= Task.Factory.StartNew(() => { sc1(0); });
                var t2= Task.Factory.StartNew(() => { sc1(1); });
                var t3= Task.Factory.StartNew(() => { sc1(2); });
                t1.Wait();
                t2.Wait();
                t3.Wait();

    註:5.x 增加了Slice設置可並行拉取全量數據,移除SearchType.Scan,首次查詢Search時也返回結果,2.x中設置SearchType.Scan首次Search只返回ScrollId。

    註:5.x中新增深度分頁方案深度分頁

    分組結果對象 Nest.KeyedBucket

    • 2.x KeyedBucket
    • 5.x KeyedBucket<object>

    聚合結果解析

    2.x

               var vendorIdGroup = (BucketAggregate)result.Aggregations["Group_Group"];
                foreach (var bucket1 in vendorIdGroup.Items)
                {
                    //todo:2.x KeyedBucket 
                    var bucket = (KeyedBucket)bucket1;
                    var maxPrice = ((ValueAggregate)bucket.Aggregations["Dvalue_Max"]).Value;
                }

    5.x

                var vendorIdGroup = (BucketAggregate)result.Aggregations["Group_Group"];
                foreach (var bucket1 in vendorIdGroup.Items)
                {
                    //todo:5.x KeyedBucket<T> 
                    var bucket = (KeyedBucket<object>) bucket1;
                    var maxPrice = ((ValueAggregate)bucket.Aggregations["Dvalue_Max"]).Value;
                }

    其它

    一些新的特性或接口會持續補充。

elasticsearch_.net_client_nest2.x_到_5.x常用方法屬性差異