elasticsearch_.net_client_nest2.x_到_5.x常用方法属性差异
目录:
Elasticsearch.Net、NEST 交流群:523061899
变更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; }
其它
一些新的特性或接口会持续补充。
-