Ejemplo n.º 1
0
        private static void Main(string[] args)
        {
            /*
             * 本案例展示两种常见的命中模式设置
             */

            var client = new SphinxClient("192.168.192.132", 9312);
            //SPH_MATCH_ALL
            //此模式需要全部分词都命中才算命中
            var keystr = "苹果生产Mac";
            client.Mode = Jhong.SphinxClient.Enum.MatchMode.SPH_MATCH_ALL;
            var result1 = client.Query(keystr);
            Console.WriteLine("MatchAll模式:");
            foreach (var m in result1[0].Matches) Console.WriteLine("文档ID:{0},权重:{1}", m.DocID, m.Weight);

            Console.WriteLine("-----------------------------");

            //SPH_MATCH_ANY
            //此模式仅要求命中一个或以上即符合命中
            client.Mode = Jhong.SphinxClient.Enum.MatchMode.SPH_MATCH_ANY;
            var result2 = client.Query(keystr);
            Console.WriteLine("MatchAny模式:");
            foreach (var m in result2[0].Matches) Console.WriteLine("文档ID:{0},权重:{1}", m.DocID, m.Weight);
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public static void Test()
        {
            int          limit        = 10;
            SphinxClient sphinxClient = new SphinxClient("127.0.0.1", 9036);

            sphinxClient.MaxMatches = 20000;
            sphinxClient.Offset     = 0;
            sphinxClient.Limit      = limit;
            sphinxClient.SelectList = "NewsID,NewsTitle,NewsContent,CoverUrl";
            string keyword = "";
            string filter  = "";

            keyword             = "\"比特币\"";
            filter              = keyword + " @ChannelID \"" + 1 + "\"";
            sphinxClient.Mode   = MatchModes.SPH_MATCH_EXTENDED2;
            sphinxClient.Sort   = SortModes.SPH_SORT_RELEVANCE | SortModes.SPH_SORT_EXTENDED;
            sphinxClient.SortBy = "IssueTime desc ";
            string index = "index_datacenter_news";

            sphinxClient.AddQuery(filter, index, "");
            sphinxClient.FieldsWeights.Add("NewsTitle", 10);
            List <Result> list         = sphinxClient.RunQueries();
            List <string> list_title   = new List <string>();
            List <string> list_content = new List <string>();

            foreach (Result current in list)
            {
                if (current.Status == SeachdStatusCodes.SEARCHD_OK)
                {
                    foreach (Match current2 in current.Matches)
                    {
                        list_title.Add(current2.Attributes["newstitle"].AsString);
                        list_content.Add(current2.Attributes["newscontent"].AsString);
                    }
                    int totalFound = current.TotalFound;
                }
            }
            if (list_title.Count > 0)
            {
                ExcerptOptions excerptOptions = new ExcerptOptions();
                excerptOptions.Flags       = ExcerptFlags.SINGLE_PASSAGE;
                excerptOptions.Limit       = 60;
                excerptOptions.Around      = 25;
                excerptOptions.BeforeMatch = "<span class=\"red\">";
                excerptOptions.AfterMatch  = "</span>";
                List <string> list4 = sphinxClient.BuildExcerpts(list_title, index, keyword, excerptOptions);
                List <string> list5 = sphinxClient.BuildExcerpts(list_content, index, keyword, excerptOptions);
            }
        }
Ejemplo n.º 3
0
 public SphinxSearch(string host, int port)
 {
     sc = new SphinxClient(host, port);
 }