记录一点 Nest 的新的
1 var client = ElasticSearchHelper.GetClient(); 2 //查询 3 var data = client.Search<A>(s => s.Index("testy").Size(500).Query(q => q.Term(t => t.authorid, "3"))); 4 var result = client.Search<A>(s => s.Index("testy").Size(500).Query(q => q.Term(t => t.authorid, "3") && q.Range(r => r 5 .Field(f => f.procount) 6 .GreaterThanOrEquals(1) 7 .LessThanOrEquals(5) 8 ))); 9 10 //多条件查询 11 var mustQuerys = new List<Func<QueryContainerDescriptor<AuthorEs>, QueryContainer>>(); 12 // 模糊查询 13 mustQuerys.Add(q => q.MatchPhrase(t => t.Field(x => x.nickname).Query("qq"))); 14 mustQuerys.Add(q => q.Term(t => t.morestarid, 1)); 15 mustQuerys.Add(mt => mt.Term(tm => tm.Field(fd => fd.moreageid).Value(1))); 16 mustQuerys.Add(q => q.Range(t => t.Field(f => f.birthday).GreaterThanOrEquals(1).LessThanOrEquals(2))); 17 //排序 18 Func<SortDescriptor<AuthorEs>, IPromise<IList<ISort>>> sortDesc = sd => 19 { 20 //根据分值排序 21 // sd.Descending(SortSpecialField.Score); 22 sd.Descending(d => d.totalfans); 23 return sd; 24 }; 25 //TrackTotalHits 符合条件的返回总数 es 的分页从0开始(不确定) 26 var docmsg = client.Search<AuthorEs>(b => b.Index("author").TrackTotalHits().Query(q => q.Bool(b => b.Must(mustQuerys))).Sort(sortDesc).From(0).Size(10)); 27 //总数 28 int count = (int)docmsg.Total; 29 30 List<A> la = new List<A>(); 31 la.Add(new A { procount = 888, moreageid = 888, id = "3" }); 32 la.Add(new A { procount = 222, moreageid = 222, id = "6" }); 33 //批量添加存在会更新,不存在会添加是全部字段 34 client.IndexMany(la, "testy"); 35 //单个更新某个字段 docasUpset=true 表示不存在也添加 false=不存在不添加 36 client.Update<object>(6, u => u.Index("testy").Doc(new { gender = 121 }).DocAsUpsert(true)); 37 // client.Update<object>(6, u => u.Index("testy").Doc(new { gender = 121 }).DocAsUpsert(false)); 38 //批量更新多个字段(将) 39 client.Bulk(b => b.Update<object>(u => u.Index("testy").Id("3").Doc(new { procount = 999, morageid = 444 })).Update<object>(u => u.Index("testy").Id("5").Doc(new { procount = 565, moreageid = 22 }))); 40 //批量更新list类型 41 client.Bulk(b => b.UpdateMany(la, (b, u) => b.Id(u.id).Index("testy").Doc(new A { procount = u.procount, moreageid = u.moreageid })));