(NEST)c#中使用ElarticSearch的一些语句
2018-03-07 11:42
731 查看
//本文不是完整的一个示例,写的不够详细完整,不适合还没入门的人看, //需要添加nest.dll相关包 官网:https://www.elastic.co/cn/products 中文社区:https://elasticsearch.cn/ Chroem浏览器中有个可视化插件ElasticSearch Head //模型 public class TestModel { public string TagCode { get; set; } public string PN1 { get; set; } public string PN2 { get; set; } public string Tag { get; set; } public string TagPYSX { get; set; } public string TagPY { get; set; } public int SNum { get; set; } public int RNum { get; set; } public int TNum { get; set; } public string IsShow { get; set; } public DateTime LastTime { get; set; } public int OrderBy { get; set; } } //模型可加属性 [ElasticsearchType(Name= "CentaSearchTagMainModel")] public class CentaSearchTagMainModel { [Keyword(Name="tagcode",DocValues =false)] public string TagCode { get; set; } //[Keyword(Name = "pn1", DocValues = false)] public string PN1 { get; set; } [Keyword(Name = "pn2", DocValues = false)] public string PN2 { get; set; } [Text(Name="tag",Analyzer ="ik_max_word")] public string Tag { get; set; } [Keyword(Name = "tagcommon", DocValues = false)] public string TagCommon { get; set; } [Keyword(Name = "tchinese", DocValues = false)] public string TChinese { get; set; } [Keyword(Name = "tagPYSX", DocValues = false)] public string TagPYSX { get; set; } [Keyword(Name = "tagPY", DocValues = false)] public string TagPY { get; set; } [Keyword(Name = "TagCategory", DocValues = false)] public string TagCategory { get; set; } [Number(NumberType.Integer,Name = "SNum")] public int SNum { get; set; } [Number(NumberType.Integer, Name = "RNum")] public int RNum { get; set; } // [Number(NumberType.Integer, Name = "TNum")] public int TNum { get; set; } [Keyword(Name = "IsShow", DocValues = false)] public string IsShow { get; set; } [Keyword(Name = "LastTime", DocValues = false)] public string LastTime { get; set; } [Number(NumberType.Integer,Name ="orderby")] public int OrderBy { get; set; } } [WebMethod] public static List<TestModel> SearchEsfAll(string searchName) { Uri uri = new Uri("http://localhost:9200"); var settings = new ConnectionSettings(uri).DisableDirectStreaming();//.DefaultIndex("tagmain"); var client = new ElasticClient(settings); //返回所有的文件,Fluent API表示查询 var searchResponse = client.Search <TestModel>(s => s.Query(q => q.MatchAll()).Index("testindex2").Type("testIndex2Type3")); //searchResponse = client.Search <TestModel>(s => s .MatchAll()); //简写 var highlights = searchResponse.HitsMetaData.Hits.Select(h => h.Highlights); //Object Initializer语法组合查询 //var searchRequest = new SearchRequest<TestModel> //{ Query = new MatchAllQuery()}; //searchResponse = client.Search < TestModel >(searchRequest); var searchResponse3 = client.Search<TestModel>(s => s.Query(q => q.DateRange(r => r.Field(f => f.LastTime) .GreaterThanOrEquals(new DateTime(2017-01)).LessThan(new DateTime(2018-01)))) .Index("testindex2").Type("testIndex2Type3")); //查找包含改字的文档,默认分词 var searchResponse4 = client.Search < TestModel >(s => s .Query(q => q .Match(m => m .Field(f => f.Tag).Query("体馆"))).Index("testindex2").Type("testIndex2Type3")); //匹配Tag包含南的文档,和PN2包含镇 var searchResponse5 = client.Search<TestModel>(s => s .Query(q => q .Bool(b => b.Must(mu => mu .Match(m => m .Field(f => f.Tag).Query("南")), mu => mu.Match(m => m.Field(f => f.PN2) .Query("镇") )))).Index("testindex2").Type("testIndex2Type3")); //同上的简写,多个字默认分词器拆分后对比,如包含体馆or体育就匹配 var searchResponse6 = client.Search<TestModel>(s => s.Query(q => q.Match(m => m.Field(f => f.Tag) .Query("馆育")) && q.Match(m => m.Field(f => f.Tag).Query("体") )).Index("testindex2").Type("testIndex2Type3")); //结构化搜索,是关于查询具有固有结构的数据 //二进制运算符 //包含东或包含体则匹配,不分词,只能输入一个字 var searchResults = client.Search<TestModel>(s => s.Query(q => q.Bool(b => b .Should (bs => bs.Term(p => p.Tag, "东") ,bs => bs.Term(p => p.Tag, "体") ))).Index("testindex2").Type("testIndex2Type3")); //同上。显示带和字or体字的文档 var firstSearchResponse = client.Search <TestModel>(s => s.Query(q => q.Term(p => p.Tag, "体") || q.Term(p => p.Tag, "和")) .Index("testindex2").Type("testIndex2Type3")); //包含体并且包含中心,只能输入一个字 var firstSearchResponse7 = client.Search<TestModel>(s => s.Query(q => q .Term(p => p.Tag, "体") && q .Term(p => p.Tag, "和")) .Index("testindex2").Type("testIndex2Type3")); //Object Initializer语法。同上 //一元运算符 //不包含和字 var firstSearchResponse8 = client.Search < TestModel >(s => s .Query(q =>!q.Term(p => p.Tag, "和")).Index("testindex2").Type("testIndex2Type3")); //指定返回单个字段 var searchResponse9= client.Search<TestModel>(s => s.StoredFields(sf => sf .Fields( f => f.Tag, f => f.PN2 )) .Query(q => q .MatchAll()).Index("testindex2").Type("testIndex2Type3") ); //包含或排除字段 var searchResponse10 = client.Search<TestModel>(s => s .Source(sf => sf .Includes(i => i.Fields( f => f.Tag, f => f.PN2 )) //.Excludes(e => e //.Fields("PN*") //) ) .Query(q => q .MatchAll()).Index("testindex2").Type("testIndex2Type3")); return null; //List<CentaSearchTagMainModel> tagList = new List<CentaSearchTagMainModel>(); //// Uri uri = new Uri("http://localhost:9200"); //// ConnectionSettings settings = new ConnectionSettings(uri); ////settings.DisableDirectStreaming(); //// ElasticClient client = new ElasticClient(settings); ////查询该索引该类的所有文档 //var searchResults11= client.Search<CentaSearchTagMainModel>(s => s.Index("tagmain").Size(10000)); ////查询单个字 //var searchResults = client.Search<CentaSearchTagMainModel>(s => s.From(0).Query(q => q.Term(p => p.TagPYSX, "h")).Size(1000)); ////查该类型的所有文档 //var searchResults1 = client.Search<CentaSearchTagMainModel>(s => s.AllIndices().From(0).Size(10000)); ////查所有类型的文档 //var searchResults2 = client.Search<CentaSearchTagMainModel>(s => s.AllIndices().AllTypes().From(0).Size(10000)); //var result2 = client.Search<CentaSearchTagMainModel>(s => s.Query(q => q.MatchPhrase(m => m.Field(f => f.TagCode).Query("227"))).From(0).Size(15000)); ////取得该type所有的文档 //var searchResults3 = client.Search<CentaSearchTagMainModel>(s => s.From(0).Size(10000)); ////全文所有,匹配多个字段 //string keyword = String.Format("*{0}*", searchName); //var tagList1 = client.Search<CentaSearchTagMainModel>(s => s // .Index("tagmain") // .Query(q => q.QueryString(qs => qs.Query(keyword).DefaultOperator(Operator.Or))).From(0).Size(1000)); //; ////搜拼音时需要全匹配,如万科城市花园=wkcshy //var searchResults4 = client.Search<CentaSearchTagMainModel> // (s =>s.Query(q => q.QueryString(m => m.Fields(fd => fd.Field(fdd => fdd.TagPYSX).Field(fdd => fdd.Tag)).Query(searchName))).From(0).Size(1500));//.Field(fdd => fdd.carGearBox).From(0).Size(15) ////模糊匹配,不分字。='%name%' //var result3 = client.Search<CentaSearchTagMainModel>(s => s .Query(q => q.MatchPhrase(m => m.Field(f => f.Tag).Query("和"))).From(0) .Size(1500)); ////分字匹配 //var searchResponse = client.Search<CentaSearchTagMainModel> // (s => s.From(0).Size(100).Query(q => q.Match(m => m.Field(f => f.Tag).Query("*"+searchName+"*")))); ////根据id查询,精准匹配,不能分词包含等。 //var searchResults6 = client.Search<CentaSearchTagMainModel>(s => s.From(0) .Size(10).Query(q => q.Ids(r => r.Values("2270*")))); ////查询前10条,不能加数量 //var query = new Nest.SearchDescriptor<CentaSearchTagMainModel>(); //var result = client.Search<CentaSearchTagMainModel>(x => query); ////取得包含有科或和或平的,用分词器拆分的,单个字拆分,%科%,%和%,0%平% //var v1 = client.Search <CentaSearchTagMainModel> // (query.PostFilter(x => x.QueryString(t => t.Fields(f => f.Field(obj => obj.Tag)).Query("科和 平"))).From(0).Size(1000)); //tagList.AddRange(client.Search<CentaSearchTagMainModel>(s => s.Query(q => q.MatchPhrase(m => m.Field(f => f.Tag).Query("万"))).From(0).Size(11).Sort(d => d.Ascending(p => p.OrderBy).Descending(p => p.TNum))).Documents.ToList()); ////tagList.AddRange(client.Search<CentaSearchTagMainModel> //// (s => s.Query(q => q.QueryString(m => m.Fields(fd => fd.Field(fdd => fdd.Tag)). //// Query("*" + searchNames + "*"))).From(0).Size(11).Sort(d => d.Ascending(p => p.OrderBy).Descending(p => p.TNum))).Documents.ToList());//.Field(fdd => fdd.carGearBox).From(0).Size(15) // return null;//v1.Documents.ToList(); } protected void Page_Load(object sender, EventArgs e) { Uri uri = new Uri("http://localhost:9200"); ConnectionSettings settings = new ConnectionSettings(uri); settings.DisableDirectStreaming().DefaultIndex("customer"); ElasticClient client = new ElasticClient(settings); //var node = new Uri("http://localhost:9200/"); //如果没有指定使用哪个index,ElasticSearch会直接使用我们在setting中的defaultIndex,如果没有,则会自动创建 //var settings = new ConnectionSettings(node).DefaultIndex("geopoint-tests11"); //var client = new ElasticClient(settings); client.CreateIndex("geopoint-tests"); //添加索引 //dynamic response = client.GetIndex(indexName); //client.CreateIndex("customer1"); //if (!response.IsValid) //{ // response = client.CreateIndex("customer"); // Console.WriteLine(response); //} //创建文档并指定索引,和文档ID // var company = new Company { CompanyID = 2, Name = "IBM" }; //response = client.Index(company, i => i.Index(indexName).Type("sss").Id(company.CompanyID)); //创建文档并指定索引,文档类型,未指定文档ID,ElasticSearch会帮你生成一个随机的ID //var company1 = new Company { CompanyID = 2, Name = "IBM2" }; //response = client.Index(company, i => i.Index(indexName)); //这里没有显示指定索引,所以使用的客户端初始化默认索引,如果没有默认索引,就会发生错误 //var company2 = new Company { CompanyID = 1, Name = "IBM1" }; //response = client.Index(company); // 修改文档,创建式修改比较合适 //company = new Company { CompanyID = 2, Name = "联想" }; //response = client.Index(company, i => i.Id(company.CompanyID)); //修改指定版本的文档 //company = new Company { CompanyID = 2, Name = "联想" }; //response = client.Index(company, i => i.Id(company.CompanyID).Version(1)); //根据特定的条件来删除文档 //response = client.Delete<Company>(company.CompanyID, d => d.Index(indexName)); //删除文档 //client.Delete<Company>(company.CompanyID); //删除索引 //response = client.DeleteIndex(indexName); //这里制定了要删除的索引名称,后一个表示删除的索引名 //var descriptor = new DeleteIndexDescriptor("geopoint-tests1").Index("geopoint-tests1"); //删除指定索引所在节点下的所有索引(,当前索引同级的所有索引) //var descriptor = new DeleteIndexDescriptor("geopoint-tests1").AllIndices(); //这里是指定了相应的索引,如果没有显示指定索引,都是用的默认索引, //批量创建文档,同时也创建了索引 BulkDescriptor descriptor = new BulkDescriptor(); // descriptor.Index<Company>(op => op.Document(new Company { CompanyID = 1, Name = "IBM" })); //descriptor.Index<Company>(op => op.Document(new Company { CompanyID = 21221, Name = "IB1M" }).Index("b").Type("sss").Id("11")); //descriptor.Index<Company>(op => op.Document(new Company { CompanyID = 3, Name = "IBM" }).Index("c")); // response = client.DeleteIndex("c"); client.DeleteIndex("b"); client.Bulk(descriptor); //response = client.DeleteIndex(indexName); }
相关文章推荐
- 关于C#中用access做数据库,使用like语句的问题
- C,C++程序员使用C#应注意的一些小问题
- 对使用C#.Net的一些感想
- C#中对XML文件的一些基本操作(使用DOM Tree)
- 使用 SQL 语句从数据库一个表中随机获取一些数据
- 使用C#制做进程监视器 (一些基本的方法函数)
- 一个简单的动态编译器,支持C#和VB.NET。当你想测试一些简单的代码的时候可以使用。
- C#.Net Castle ActiveRecord学习实践(9):使用ActiveRecord的一些技巧
- c# 中一些方法使用的区别
- C#使用参数化和块语句来提高批处理SQL语句的执行效率
- 转载:使用飞信VMDotNet使C#程序脱离.NET FRAMEWORK也能运行的一些心得
- C#使用预处理SQL语句访问MySQL的方法(Parameter)
- c# 使用ChartDirector绘图的一些个人体会
- C#中使用语句和循环来控制程序流总程序....
- 使用 SQL 语句从一个表中随机获取一些数据
- 使用 SQL 语句从数据库一个表中随机获取一些数据
- 使用.net框架应用C#语言开发窗口程序的一些注意事项
- Excel对象模型的一些使用心得(C#)
- C#中使用含参数的SQL语句一定要注意参数长度的设置
- 使用飞信VMDotNet使C#程序脱离.NET FRAMEWORK也能运行的一些心得 ------(我复制别个的)