private static void Main(string[] args) { /* * 作用就是类似mysql的limit,控制返回多少个命中数,做分页的时候挺好用 */ var client = new SphinxClient("192.168.52.135", 9312); client.Mode = MatchMode.SPH_MATCH_ANY; //设置Limit数目(跟MYSQL的limit关键字作用差不多) client.Limit = 5; var res1 = client.Query("ios"); Console.WriteLine("第一次查询,Limit值为{0},共返回{1}个记录", client.Limit, res1[0].Matches.Count()); //设置Limit数目 client.Limit = 1; var res2 = client.Query("ios"); Console.WriteLine("第二次查询,Limit值为{0},共返回{1}个记录", client.Limit, res2[0].Matches.Count()); Console.ReadKey(); }
/* * 本案例展示构建摘录(也就是类似百度那种命中加红加粗之类的) * 这里需要注意的,index不能传入‘*’因为需要使用具体索引配置作为分词组件的执行配置 */ private static void Main(string[] args) { var client = new SphinxClient("192.168.1.102", 9312); var keystr = "苹果生产ios"; var queryRes = client.Query(keystr); var ids = from m in queryRes[0].Matches select m.DocID; var selectstr = string.Format("select title from post where id in ({0})", string.Join(",", ids)); var docs = new List<string>(); using (var conn = new MySqlConnection()) { conn.ConnectionString = "Server=192.168.1.102;Database=blog; User=root;Password=root;Charset=utf8;"; conn.Open(); using (var reader = MySqlHelper.ExecuteReader(conn, selectstr)) { while (reader.Read()) { docs.Add(reader["title"].ToString()); } } } var buildRes = client.BuildExcerpts(docs, "main", keystr, new Jhong.SphinxClient.Model.SphinxBuildExceptsOptions(beforeMatch: "<font color='red'>", afterMatch: "</font>")); Console.WriteLine("传入文档内容:"); foreach (var d in docs) { Console.WriteLine(d); } Console.WriteLine(""); Console.WriteLine("返回结果:"); foreach (var r in buildRes) { Console.WriteLine(r); } Console.ReadKey(); }