Exemplo n.º 1
0
        /// <summary>
        /// 在title和content字段中查询数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List <MySearchUnit> Search(string keyword)
        {
            string[] fileds = { "title", "content" };//查询字段
            //Stopwatch st = new Stopwatch();
            //st.Start();
            QueryParser parser = null;                                     // new QueryParser(Lucene.Net.Util.Version.LUCENE_30, field, analyzer);//一个字段查询

            parser = new MultiFieldQueryParser(version, fileds, analyzer); //多个字段查询
            Query         query    = parser.Parse(keyword);
            int           n        = 1000;
            IndexSearcher searcher = new IndexSearcher(directory_luce, true);//true-表示只读
            TopDocs       docs     = searcher.Search(query, (Filter)null, n);

            if (docs == null || docs.TotalHits == 0)
            {
                return(null);
            }
            else
            {
                List <MySearchUnit> list = new List <MySearchUnit>();
                int counter = 1;
                foreach (ScoreDoc sd in docs.ScoreDocs)//遍历搜索到的结果
                {
                    try
                    {
                        Document doc        = searcher.Doc(sd.Doc);
                        string   id         = doc.Get("id");
                        string   title      = doc.Get("title");
                        string   content    = doc.Get("content");
                        string   flag       = doc.Get("flag");
                        string   imageurl   = doc.Get("imageurl");
                        string   updatetime = doc.Get("updatetime");

                        string createdate = doc.Get("createdate");
                        PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                        PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                        highlighter.FragmentSize = 50;
                        content = highlighter.GetBestFragment(keyword, content);
                        string titlehighlight = highlighter.GetBestFragment(keyword, title);
                        if (titlehighlight != "")
                        {
                            title = titlehighlight;
                        }
                        list.Add(new MySearchUnit(id, title, content, flag, imageurl, updatetime));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    counter++;
                }
                return(list);
            }
            //st.Stop();
            //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");
        }
Exemplo n.º 2
0
 public string CreateHightLight(string keywords, string content)
 {
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color='#dd4b39'>", "</font>");
     PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
     highlighter.FragmentSize = 150;
     return(highlighter.GetBestFragment(keywords, content));
 }
Exemplo n.º 3
0
        //需要添加PanGu.HighLight.dll的引用
        /// <summary>
        /// 搜索结果高亮显示
        /// </summary>
        /// <param name="keyword"> 关键字 </param>
        /// <param name="content"> 搜索结果 </param>
        /// <returns> 高亮后结果 </returns>
        public static string HightLight(string keyword, string content)
        {
            var preTag  = "<font style=\"font-style:normal;color:#cc0000;\"><b>";
            var postTag = "</b></font>";

            //创建HTMLFormatter,参数为高亮单词的前后缀
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                new PanGu.HighLight.SimpleHTMLFormatter(preTag, postTag);
            //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
            PanGu.HighLight.Highlighter highlighter =
                new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                                new Segment());
            //设置每个摘要段的字符数
            highlighter.FragmentSize = 256;

            var allFragment = highlighter.GetFragments(keyword, content, 50);
            var result      = GetBestMatchFragment(keyword, allFragment, preTag, postTag);

            if (result.IsNullOrEmpty())
            {
                result = highlighter.GetBestFragment(keyword, content);
            }

            if (!string.IsNullOrWhiteSpace(result))
            {
                return(result);
            }
            return(content.Left(200));
        }
Exemplo n.º 4
0
 /// <summary>
 /// 获取关键词高亮的摘要内容
 /// </summary>
 /// <param name="content">原始内容</param>
 /// <param name="keywords">关键词</param>
 /// <returns></returns>
 private string GetHightContent(string content, string keywords)
 {
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
         new PanGu.HighLight.SimpleHTMLFormatter("<span style='color:red'>", "</span>");
     PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
     highlighter.FragmentSize = 100;                         //设置每个摘要段的字符数
     return(highlighter.GetBestFragment(keywords, content)); //获取最匹配的摘要段
 }
Exemplo n.º 5
0
        private string Preview(string body, string keyword)
        {
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"Red\">", "</font>");
            PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
            highlighter.FragmentSize = 100;
            string bodyPreview = highlighter.GetBestFragment(keyword, body);

            return(bodyPreview);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 在传递的字段数组中查询数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns>返回</returns>
        public List <T> Search <T>(string keyword, string[] fields, T objType)
        {
            //fields = new string[]{ "title", "content" };//查询字段
            //Stopwatch st = new Stopwatch();
            //st.Start();
            QueryParser parser = null;                                     // new QueryParser(Lucene.Net.Util.Version.LUCENE_30, field, analyzer);//一个字段查询

            parser = new MultiFieldQueryParser(version, fields, analyzer); //多个字段查询
            Query         query    = parser.Parse(keyword);
            int           n        = 1000;
            IndexSearcher searcher = new IndexSearcher(directory_luce, true);//true-表示只读
            List <T>      list     = new List <T>();
            TopDocs       docs     = searcher.Search(query, (Filter)null, n);

            if (docs == null || docs.totalHits == 0)
            {
                return(list);
            }
            else
            {
                PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font style='color:red'>", "</font>");
                PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                highlighter.FragmentSize = 50;
                foreach (ScoreDoc sd in docs.scoreDocs)//遍历搜索到的结果
                {
                    try
                    {
                        //获取对象
                        Document       doc          = searcher.Doc(sd.doc);
                        Type           t            = objType.GetType();
                        T              obj          = (T)Activator.CreateInstance(t, true);
                        PropertyInfo[] propertyList = t.GetProperties();
                        //设置对象,并填充搜索到的数据至对象
                        foreach (PropertyInfo property in propertyList)
                        {
                            string strPName = property.Name;
                            string strValue = doc.Get(strPName);
                            if (fields != null && fields.Contains(strPName))
                            {
                                string strHighterValue = highlighter.GetBestFragment(keyword, strValue);
                                strValue = strHighterValue == "" ? strValue : strHighterValue;
                            }
                            property.SetValue(obj, strValue, null);
                        }
                        list.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                return(list);
            }
            //st.Stop();
            //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");
        }
Exemplo n.º 7
0
 public static string ChangeStringToHighLight(string keyWord, string content)
 {
     //创建HTMLFormatter,参数为高亮单词的前后缀 
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");  //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent 
     PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
     //设置每个摘要段的字符数 
     highlighter.FragmentSize = 50;
     //获取最匹配的摘要段 
     return highlighter.GetBestFragment(keyWord, content);
 }
Exemplo n.º 8
0
        /// <summary>
        /// search
        /// </summary>
        /// <param name="indexDir"></param>
        /// <param name="q">keyword</param>
        /// <param name="pageLen">every page's length</param>
        /// <param name="pageNo">page number</param>
        /// <param name="recCount">result number</param>
        /// <returns></returns>
        public List <TDocs> Search(String q, int pageLen, int pageNo, out int recCount)
        {
            string        keywords = q;
            IndexSearcher search   = new IndexSearcher(INDEX_DIR);

            q = GetKeyWordsSplitBySpace(q, new PanGuTokenizer());
            QueryParser queryParser = new QueryParser("content", new PanGuAnalyzer(true));

            Query query = queryParser.Parse(q);

            Hits hits = search.Search(query, Sort.RELEVANCE);

            List <TDocs> result = new List <TDocs>();

            recCount = hits.Length();
            int i = (pageNo - 1) * pageLen;

            while (i < recCount && result.Count < pageLen)
            {
                TDocs docs = null;

                try
                {
                    docs           = new TDocs();
                    docs.Path      = hits.Doc(i).Get("path");
                    docs.Name      = hits.Doc(i).Get("name");
                    docs.Title     = hits.Doc(i).Get("title");
                    docs.Extension = hits.Doc(i).Get("ext");
                    //rem this item in case the search result will be too large & consume too much memory,
                    //   takes loading time, abstract is enough
                    //docs.Content = hits.Doc(i).Get("content");

                    PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                        new PanGu.HighLight.SimpleHTMLFormatter("<color=red>", "</color>");

                    PanGu.HighLight.Highlighter highlighter =
                        new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                                        new Segment());

                    highlighter.FragmentSize = 100;
                    docs.Abstract            = highlighter.GetBestFragment(keywords, hits.Doc(i).Get("content"));
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
                finally
                {
                    result.Add(docs);
                    i++;
                }
            }
            search.Close();
            return(result);
        }
Exemplo n.º 9
0
 public static string CreateHighLight(string keywords, string content)
 {
     //创建HTMLFormatter,参数为高亮单词的前后缀
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
         new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");//同过改变字体颜色,实现高亮显示
     //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
     PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
     //设置每个摘要段的字符数
     highlighter.FragmentSize = 50;
     //获取最匹配的摘要段
     return(highlighter.GetBestFragment(keywords, content));
 }
Exemplo n.º 10
0
 /// <summary>
 /// 创建HTMLFormatter,参数为高亮单词的前后缀
 /// </summary>
 /// <param name="keywords"></param>
 /// <param name="Content"></param>
 /// <returns></returns>
 public static string CreateHightLight(string keywords, string Content)
 {
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
      new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
     //创建Highlighter ,输入HTMLFormatter 和盘古分词对象Semgent
     PanGu.HighLight.Highlighter highlighter =
     new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
     new Segment());
     //设置每个摘要段的字符数
     highlighter.FragmentSize = 150;
     //获取最匹配的摘要段
     return highlighter.GetBestFragment(keywords, Content);
 }
Exemplo n.º 11
0
 //需要添加PanGu.HighLight.dll的引用
 /// <summary>
 /// 搜索结果高亮显示
 /// </summary>
 /// <param name="keyword"> 关键字 </param>
 /// <param name="content"> 搜索结果 </param>
 /// <returns> 高亮后结果 </returns>
 public static string HightLight(string keyword, string content)
 {
     //创建HTMLFormatter,参数为高亮单词的前后缀
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
         new PanGu.HighLight.SimpleHTMLFormatter("<font style=\"font-style:normal;color:#cc0000;\"><b>", "</b></font>");
     //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
     PanGu.HighLight.Highlighter highlighter =
                     new PanGu.HighLight.Highlighter(simpleHTMLFormatter,new Segment());
     //设置每个摘要段的字符数
     highlighter.FragmentSize = 1000;
     //获取最匹配的摘要段
     return highlighter.GetBestFragment(keyword, content);
 }
Exemplo n.º 12
0
 /// <summary>
 /// 创建HTMLFormatter,参数为高亮单词的前后缀
 /// </summary>
 /// <param name="msg">原始搜索内容</param>
 /// <param name="Content">字段里的值</param>
 /// <returns></returns>
 public static string CreateHightLight(string msg, string Content)
 {
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
         new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
     //创建Highlighter ,输入HTMLFormatter 和盘古分词对象Semgent
     PanGu.HighLight.Highlighter highlighter =
         new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                         new Segment());
     //设置每个摘要段的字符数
     highlighter.FragmentSize = 150;
     //获取最匹配的摘要段
     return(highlighter.GetBestFragment(msg, Content));
 }
Exemplo n.º 13
0
        /// <summary>
        /// 搜索结果中关键词高亮显示
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        static string HightLight(string keyword, string content)
        {
            //创建HTMLFormatter,参数为高亮单词的前后缀
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<em>", "<em>");

            //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
            PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());

            //设置每个摘要段的字符数
            highlighter.FragmentSize = 1000;

            //获取最匹配的摘要段
            return(highlighter.GetBestFragment(keyword, content));
        }
Exemplo n.º 14
0
 public static string HightLight(string keyword, string content, int iFragmentSize)
 {
     //创建HTMLFormatter,参数为高亮单词的前后缀
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
         new PanGu.HighLight.SimpleHTMLFormatter("<font style=\"font-style:normal;color:#cc0000;\"><b>", "</b></font>");
     //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
     PanGu.HighLight.Highlighter highlighter =
         new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                         new Segment());
     //设置每个摘要段的字符数
     highlighter.FragmentSize = iFragmentSize;
     //获取最匹配的摘要段
     return(highlighter.GetBestFragment(keyword, content));
 }
Exemplo n.º 15
0
 /// <summary>
 /// 进行高亮显示搜索词
 /// </summary>
 /// <param name="keywords"></param>
 /// <param name="Content"></param>
 /// <returns></returns>
 public static string CreateHightLight(string keywords, string Content)
 {
     // 注意:在后台加 HTML标签会导致前台原样输出,可以通过
     // @MvcHtmlString.Create(book.Content)来解决
     PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
         new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
     //创建Highlighter ,输入HTMLFormatter 和盘古分词对象Semgent
     PanGu.HighLight.Highlighter highlighter =
         new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                         new Segment());
     //设置每个摘要段的字符数
     highlighter.FragmentSize = 150;
     //获取最匹配的摘要段
     return(highlighter.GetBestFragment(keywords, Content));
 }
Exemplo n.º 16
0
        //高亮显示
        public static string CreateHightLight(string keywords, string Content)
        {
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            //创建Highlighter ,输入HTMLFormatter 和盘古分词对象Semgent
            PanGu.HighLight.Highlighter highlighter =
                new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                                new Segment());
            //设置每个摘要段的字符数
            highlighter.FragmentSize = 150;
            //获取最匹配的摘要段
            var result = highlighter.GetBestFragment(keywords, Content);

            return(string.IsNullOrEmpty(result) ? Content : result);
        }
Exemplo n.º 17
0
        private static VM.SearchModel SetHighlighter(Dictionary <string, string> dicKeywords, VM.SearchModel model)
        {
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
            highlighter.FragmentSize = 100;
            string strContent = string.Empty;

            dicKeywords.TryGetValue("Content", out strContent);
            if (!string.IsNullOrEmpty(strContent))
            {
                model.Contents = highlighter.GetBestFragment(strContent, model.Contents) + "...";
                //model.Content = string.Join("...", (highlighter.GetFragments(strContent, model.Content,3).ToList()));
            }

            return(model);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 对搜索的关键词高亮显示
        /// </summary>
        /// <param name="keyword">搜索的关键词项</param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Highlight(string keyword, string content)
        {
            //创建HTMLFormatter,参数为高亮单词的前后缀
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                new PanGu.HighLight.SimpleHTMLFormatter("<a href='#' class='highlightshow'>", "</a>");

            //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
            PanGu.HighLight.Highlighter highlighter =
                new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());

            //设置每个摘要段的字符数,暂时取300个字符吧
            highlighter.FragmentSize = 300;

            //获取最匹配的摘要段
            return(highlighter.GetBestFragment(keyword, content));
        }
Exemplo n.º 19
0
        /// <summary>
        /// 将内容中的某些部分高亮(可以针对所有的字符执行)
        /// </summary>
        /// <param name="keyword">要高亮显示的关键词</param>
        /// <param name="content">含有关键词的内容</param>
        /// <returns>如果内容中含有关键词将其高亮返回,如果没有,则返回内容</returns>
        public static String highLight(string keyword, String content)
        {
            PanGu.HighLight.SimpleHTMLFormatter formatter   = new PanGu.HighLight.SimpleHTMLFormatter("<font color='red'>", "</font>"); //高亮部分的表现方式,即前后要加的标签
            PanGu.HighLight.Highlighter         highlighter = new PanGu.HighLight.Highlighter(formatter, new Segment());                //两个参数的意义1、高亮的表现形式;2、要高亮显示的关键词,并进行分词
            highlighter.FragmentSize = 800;                                                                                             //要显示的长度
            string msg = highlighter.GetBestFragment(keyword, content);                                                                 //要上面要求返回的字符

            if (string.IsNullOrEmpty(msg))
            {
                return(content);
            }
            else
            {
                return(msg);
            }
        }
Exemplo n.º 20
0
        //需要添加PanGu.HighLight.dll的引用
        /// <summary>
        /// 搜索结果高亮显示
        /// </summary>
        /// <param name="keyword"> 关键字 </param>
        /// <param name="content"> 搜索结果 </param>
        /// <returns> 高亮后结果 </returns>
        public static string HightLight(string keyword, string content)
        {
            //创建HTMLFormatter,参数为高亮单词的前后缀
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                new PanGu.HighLight.SimpleHTMLFormatter("<span class='hightlight'>", "</span>");
            //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent
            PanGu.HighLight.Highlighter highlighter =
                new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                                new Segment());
            //设置每个摘要段的字符数
            highlighter.FragmentSize = 1000;
            //获取最匹配的摘要段
            var result = highlighter.GetBestFragment(keyword, content);

            if (string.IsNullOrEmpty(result))
            {
                return(content);
            }

            return(result);
        }
Exemplo n.º 21
0
        /// <summary>
        /// 设置高亮
        /// </summary>
        /// <param name="body"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        private string Preview(string body, string keyword)
        {
            int FragmentSize = __SearchAppGaoLiangMaxLength;

            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<em class='DataKey'>", "</em>");
            PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
            highlighter.FragmentSize = FragmentSize;
            string bodyPreview = highlighter.GetBestFragment(keyword, body);

            if (string.IsNullOrEmpty(bodyPreview))
            {
                if (body.Length > FragmentSize)
                {
                    bodyPreview = body.Substring(0, FragmentSize);
                }
                else
                {
                    bodyPreview = body;
                }
            }
            return(bodyPreview);
        }
Exemplo n.º 22
0
        private void button2_Click(object sender, EventArgs e)
        {
            PanGu.Segment.Init();
            PanGu.Segment segment = new PanGu.Segment();
            ICollection <PanGu.WordInfo> words = segment.DoSegment("山东落花生花落东山,长春市长春花店");

            foreach (var word in words)
            {
                Console.WriteLine(word.Word);
            }

            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
            highlighter.FragmentSize = 100; // 设置每个摘要段的字符数
            string keywords = "信号/道路/开通";
            string content  = @"高德完胜百度。我专门花了几个星期,在我所在的城市测试两个地图,高德数据不准确在少数,而百度就是家常便饭了,表现为:
已经管制一年的道路(双向变单向),百度仍然提示双向皆可走。
已经封闭数年的道路,百度仍然说是通的。
新修道路,还没有开通,百度居然让走。
有时候规划路线时明明是正确的,但是导航过程中,就出乱子,信号没问题、路线不复杂,明明是要左转,百度却叫右转。";
            string abs      = highlighter.GetBestFragment(keywords, content);

            Console.WriteLine(abs);
        }
Exemplo n.º 23
0
        /// <summary>
        /// 在不同的类型下再根据title和content字段中查询数据(分页)
        /// </summary>
        /// <param name="_flag">分类,传空值查询全部</param>
        /// <param name="keyword"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public List <MySearchUnit> Search(string _flag, string keyword, int PageIndex, int PageSize, out int TotalCount)
        {
            if (PageIndex < 1)
            {
                PageIndex = 1;
            }
            //Stopwatch st = new Stopwatch();
            //st.Start();
            BooleanQuery bq = new BooleanQuery();

            if (_flag != "")
            {
                QueryParser qpflag = new QueryParser(version, "flag", analyzer);
                Query       qflag  = qpflag.Parse(_flag);
                bq.Add(qflag, Occur.MUST);//与运算
            }
            if (keyword != "")
            {
                string[]    fileds = { "title", "content" };                   //查询字段
                QueryParser parser = null;                                     // new QueryParser(version, field, analyzer);//一个字段查询
                parser = new MultiFieldQueryParser(version, fileds, analyzer); //多个字段查询
                Query queryKeyword = parser.Parse(keyword);
                bq.Add(queryKeyword, Occur.MUST);                              //与运算
            }

            TopScoreDocCollector collector = TopScoreDocCollector.Create(PageIndex * PageSize, false);
            IndexSearcher        searcher  = new IndexSearcher(directory_luce, true);//true-表示只读

            searcher.Search(bq, collector);
            if (collector == null || collector.TotalHits == 0)
            {
                TotalCount = 0;
                return(null);
            }
            else
            {
                int start = PageSize * (PageIndex - 1);
                //结束数
                int                 limit   = PageSize;
                ScoreDoc[]          hits    = collector.TopDocs(start, limit).ScoreDocs;
                List <MySearchUnit> list    = new List <MySearchUnit>();
                int                 counter = 1;
                TotalCount = collector.TotalHits;
                foreach (ScoreDoc sd in hits)//遍历搜索到的结果
                {
                    try
                    {
                        Document doc        = searcher.Doc(sd.Doc);
                        string   id         = doc.Get("id");
                        string   title      = doc.Get("title");
                        string   content    = doc.Get("content");
                        string   flag       = doc.Get("flag");
                        string   imageurl   = doc.Get("imageurl");
                        string   updatetime = doc.Get("updatetime");

                        PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                        PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                        highlighter.FragmentSize = 50;
                        content = highlighter.GetBestFragment(keyword, content);
                        string titlehighlight = highlighter.GetBestFragment(keyword, title);
                        if (titlehighlight != "")
                        {
                            title = titlehighlight;
                        }
                        list.Add(new MySearchUnit(id, title, content, flag, imageurl, updatetime));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    counter++;
                }
                return(list);
            }
            //st.Stop();
            //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");
        }
Exemplo n.º 24
0
        /// <summary>
        /// 在不同的类型下再根据title和content字段中查询数据(分页)
        /// </summary>
        /// <param name="flag">分类,传空值查询全部</param>
        /// <param name="keyWord"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="isLike"></param>
        /// <returns></returns>
        public List <SearchResult> Search(string flag, string keyWord, int pageIndex, int pageSize, out int totalCount, bool isLike = false)
        {
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            BooleanQuery bq = new BooleanQuery();

            if (!string.IsNullOrEmpty(flag))
            {
                QueryParser qpflag = new QueryParser(version, "flag", analyzer);
                Query       qflag  = qpflag.Parse(flag);
                bq.Add(qflag, Occur.MUST);//与运算
            }
            if (!string.IsNullOrEmpty(keyWord))
            {
                string[]    fileds = { "title", "content" };                   //查询字段
                QueryParser parser = null;                                     // new QueryParser(version, field, analyzer);//一个字段查询
                parser = new MultiFieldQueryParser(version, fileds, analyzer); //多个字段查询
                Query queryKeyword = parser.Parse(keyWord);
                bq.Add(queryKeyword, Occur.MUST);                              //与运算

                if (isLike)
                {
                    Query prefixQuery_title   = null;
                    Query prefixQuery_body    = null;
                    Query fuzzyQuery_Title    = null;
                    Query fuzzyQuery_body     = null;
                    Query wildcardQuery_title = null;
                    Query wildcardQuery_body  = null;
                    //以什么开头,输入“ja”就可以搜到包含java和javascript两项结果了
                    prefixQuery_title = new PrefixQuery(new Term("title", keyWord));
                    prefixQuery_body  = new PrefixQuery(new Term("content", keyWord));
                    //直接模糊匹配,假设你想搜索跟‘wuzza’相似的词语,你可能得到‘fuzzy’和‘wuzzy’。
                    fuzzyQuery_Title = new FuzzyQuery(new Term("title", keyWord));
                    fuzzyQuery_body  = new FuzzyQuery(new Term("content", keyWord));
                    //通配符搜索
                    wildcardQuery_title = new WildcardQuery(new Term("title", keyWord));
                    wildcardQuery_body  = new WildcardQuery(new Term("content", keyWord));

                    bq.Add(prefixQuery_title, Occur.SHOULD);
                    bq.Add(prefixQuery_body, Occur.SHOULD);
                    bq.Add(fuzzyQuery_Title, Occur.SHOULD);
                    bq.Add(fuzzyQuery_body, Occur.SHOULD);
                    bq.Add(wildcardQuery_title, Occur.SHOULD);
                    bq.Add(wildcardQuery_body, Occur.SHOULD);
                }
            }
            TopScoreDocCollector collector = TopScoreDocCollector.Create(pageIndex * pageSize, false);
            IndexSearcher        searcher  = new IndexSearcher(directory_luce, true);//true-表示只读

            searcher.Search(bq, collector);
            if (collector == null || collector.TotalHits == 0)
            {
                totalCount = 0;
                return(null);
            }
            else
            {
                int start = pageSize * (pageIndex - 1);
                //结束数
                int                 limit   = pageSize;
                ScoreDoc[]          hits    = collector.TopDocs(start, limit).ScoreDocs;
                List <SearchResult> list    = new List <SearchResult>();
                int                 counter = 1;
                totalCount = collector.TotalHits;
                foreach (ScoreDoc sd in hits)//遍历搜索到的结果
                {
                    try
                    {
                        Document     doc          = searcher.Doc(sd.Doc);
                        SearchResult searchResult = new SearchResult();
                        searchResult.Id = Convert.ToInt64(doc.Get("id"));
                        string title   = doc.Get("title");
                        string content = doc.Get("content");

                        PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                        PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                        highlighter.FragmentSize = 50;
                        content = highlighter.GetBestFragment(keyWord, content);
                        string titlehighlight = highlighter.GetBestFragment(keyWord, title);
                        if (titlehighlight != "")
                        {
                            title = titlehighlight;
                        }
                        searchResult.Title   = title;
                        searchResult.Content = content;
                        list.Add(searchResult);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    counter++;
                }
                return(list);
            }
        }
Exemplo n.º 25
0
        public static List<TNews> Search(String indexDir, String q, int pageLen, int pageNo, out int recCount)
        {
            string keywords = q;

            IndexSearcher search = new IndexSearcher(indexDir);
            q = GetKeyWordsSplitBySpace(q, new PanGuTokenizer());
            QueryParser queryParser = new QueryParser("contents", new PanGuAnalyzer(true));

            Query query = queryParser.Parse(q);

            Hits hits = search.Search(query);

            List<TNews> result = new List<TNews>();

            recCount = hits.Length();
            int i = (pageNo - 1) * pageLen;

            while (i < recCount && result.Count < pageLen)
            {
                TNews news = null;

                try
                {
                    news = new TNews();
                    news.Title = hits.Doc(i).Get("title");
                    news.Content = hits.Doc(i).Get("contents");
                    news.Url = hits.Doc(i).Get("url");
                    String strTime = hits.Doc(i).Get("time");
                    news.Time = DateTime.ParseExact(strTime, "yyyyMMdd", null);

                    PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                        new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");

                    PanGu.HighLight.Highlighter highlighter =
                        new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                        new Segment());

                    highlighter.FragmentSize = 50;

                    news.Abstract = highlighter.GetBestFragment(keywords, news.Content);

                    //// ������ʾ����
                    ////TermQuery tQuery = new TermQuery(new Term("contents", q));

                    //SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                    //Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
                    ////�ؼ�������ʾ��С����
                    //highlighter.SetTextFragmenter(new SimpleFragmenter(50));
                    ////ȡ��������ʾ����
                    //Lucene.Net.Analysis.KTDictSeg.KTDictSegAnalyzer analyzer = new Lucene.Net.Analysis.KTDictSeg.KTDictSegAnalyzer();
                    //TokenStream tokenStream = analyzer.TokenStream("contents", new StringReader(news.Content));
                    //news.Abstract = highlighter.GetBestFragment(tokenStream, news.Content);

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    result.Add(news);
                    i++;
                }
            }

            search.Close();
            return result;
        }
Exemplo n.º 26
0
        /// <summary>
        /// 在不同的类型下再根据检索字段中查询数据(分页)
        /// </summary>
        /// <param name="dType">分类,传空值查询全部,用于精确查找,例如类型等,key为字段名,value为值</param>
        /// <param name="iTypeClause">分类之间的运算关系,三个值:0:must,且;1:must not,非;2:should,或;默认为0</param>
        /// <param name="keyword">用于模糊查找的关键词</param>
        /// <param name="objType">需要的对象</param>
        /// <param name="PageIndex">页数</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="TotalCount">返回的数据总数</param>
        /// <returns></returns>
        public List <T> Search <T>(Dictionary <string, string> dType, string keyword, string[] fields, T objType, int PageIndex, int PageSize, out int TotalCount, int iTypeClause)
        {
            if (PageIndex < 1)
            {
                PageIndex = 1;
            }
            //Stopwatch st = new Stopwatch();
            //st.Start();
            BooleanQuery bq = new BooleanQuery();

            //对类型进行与运算
            if (dType != null && dType.Count > 0)
            {
                foreach (KeyValuePair <string, string> kvp in dType)
                {
                    //使用术语查询
                    Query tq = new TermQuery(new Term(kvp.Key, kvp.Value));
                    BooleanClause.Occur occur = BooleanClause.Occur.MUST;
                    switch (iTypeClause)
                    {
                    case 0: occur = BooleanClause.Occur.MUST;
                        break;

                    case 1: occur = BooleanClause.Occur.MUST_NOT;
                        break;

                    case 2: occur = BooleanClause.Occur.SHOULD;
                        break;

                    default: occur = BooleanClause.Occur.MUST;
                        break;
                    }

                    bq.Add(tq, occur);//与运算
                }
            }
            //对关键字进行或运算
            if (keyword != "")
            {
                QueryParser parser = null;                                     // new QueryParser(version, field, analyzer);//一个字段查询
                parser = new MultiFieldQueryParser(version, fields, analyzer); //多个字段查询
                Query queryKeyword = parser.Parse(keyword);
                bq.Add(queryKeyword, BooleanClause.Occur.SHOULD);              //或运算
            }
            List <T> list = new List <T>();

            TopScoreDocCollector collector = TopScoreDocCollector.create(PageIndex * PageSize, true);
            IndexSearcher        searcher  = new IndexSearcher(directory_luce, true);//true-表示只读

            searcher.Search(bq, collector);
            if (collector == null || collector.GetTotalHits() == 0)
            {
                TotalCount = 0;
                return(list);
            }
            else
            {
                int start = PageSize * (PageIndex - 1);
                //结束数
                int        limit = PageSize;
                ScoreDoc[] hits  = collector.TopDocs(start, limit).scoreDocs;
                TotalCount = collector.GetTotalHits();

                PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font style='color:red'>", "</font>");
                PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                highlighter.FragmentSize = 50;
                //遍历搜索到的结果
                foreach (ScoreDoc sd in hits)
                {
                    try
                    {
                        //获取对象
                        Document       doc          = searcher.Doc(sd.doc);
                        Type           t            = objType.GetType();
                        T              obj          = (T)Activator.CreateInstance(t, true);
                        PropertyInfo[] propertyList = t.GetProperties();
                        //设置对象,并填充搜索到的数据至对象
                        foreach (PropertyInfo property in propertyList)
                        {
                            string strPName = property.Name;
                            string strValue = doc.Get(strPName);
                            if (fields != null && fields.Contains(strPName))
                            {
                                string strHighterValue = highlighter.GetBestFragment(keyword, strValue);
                                strValue = strHighterValue == "" ? strValue : strHighterValue;
                            }
                            property.SetValue(obj, strValue, null);
                        }
                        list.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                return(list);
            }
            //st.Stop();
            //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");
        }
Exemplo n.º 27
0
        public static List <IndexItem> Search(String indexDir, String q, int pageLen, int pageNo, out int recCount)
        {
            string keywords = q;

            IndexSearcher search = new IndexSearcher(indexDir);

            q = GetKeyWordsSplitBySpace(q, new PanGuTokenizer());
            QueryParser queryParser = new QueryParser("contents", new PanGuAnalyzer(true));

            Query query = queryParser.Parse(q);

            Hits hits = search.Search(query);

            List <IndexItem> result = new List <IndexItem>();

            recCount = hits.Length();
            int i = (pageNo - 1) * pageLen;

            while (i < recCount && result.Count < pageLen)
            {
                IndexItem news = null;

                try
                {
                    news         = new IndexItem();
                    news.Title   = hits.Doc(i).Get("title");
                    news.Content = hits.Doc(i).Get("contents");
                    news.Url     = hits.Doc(i).Get("url");
                    String strTime = hits.Doc(i).Get("time");
                    news.Time = DateTime.ParseExact(strTime, "yyyyMMdd", null);


                    PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                        new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");

                    PanGu.HighLight.Highlighter highlighter =
                        new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
                                                        new Segment());

                    highlighter.FragmentSize = 50;

                    news.Abstract = highlighter.GetBestFragment(keywords, news.Content);

                    //// 高亮显示设置
                    ////TermQuery tQuery = new TermQuery(new Term("contents", q));

                    //SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                    //Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
                    ////关键内容显示大小设置
                    //highlighter.SetTextFragmenter(new SimpleFragmenter(50));
                    ////取出高亮显示内容
                    //Lucene.Net.Analysis.KTDictSeg.KTDictSegAnalyzer analyzer = new Lucene.Net.Analysis.KTDictSeg.KTDictSegAnalyzer();
                    //TokenStream tokenStream = analyzer.TokenStream("contents", new StringReader(news.Content));
                    //news.Abstract = highlighter.GetBestFragment(tokenStream, news.Content);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    result.Add(news);
                    i++;
                }
            }

            search.Close();
            return(result);
        }
Exemplo n.º 28
0
        /// <summary>
        /// 索引查询
        /// </summary>
        /// <param name="indexDir">索引文件路径</param>
        /// <param name="top">数量</param>
        /// <param name="site_id">网站ID</param>
        /// <param name="channel_id">频道ID</param>
        /// <param name="category_id">类别ID</param>
        /// <param name="keywords">关键词</param>
        /// <param name="is_high">是否高量显示</param>
        /// <returns></returns>
        public static DataTable Search(string indexDir, int top, int site_id, int channel_id, int category_id, String keywords)
        {
            IndexSearcher search = new IndexSearcher(indexDir);

            if (keywords.Length > 1)
            {
                keywords = GetKeyWordsSplitBySpace(keywords, new PanGuTokenizer());
            }
            //查询条件
            BooleanQuery bq = new BooleanQuery();

            if (site_id > 0)
            {
                Term  t     = new Term("sid", site_id.ToString());
                Query query = new TermQuery(t);
                bq.Add(query, BooleanClause.Occur.MUST);
            }
            if (channel_id > 0)
            {
                Term  t     = new Term("hid", channel_id.ToString());
                Query query = new TermQuery(t);
                bq.Add(query, BooleanClause.Occur.MUST);
            }
            if (category_id > 0)
            {
                Term  t     = new Term("cid", category_id.ToString());
                Query query = new TermQuery(t);
                bq.Add(query, BooleanClause.Occur.MUST);
            }
            if (null != keywords && keywords.Length > 0)
            {
                string[] fields = { "title", "tags", "zhaiyao" };
                MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new PanGuAnalyzer(true));
                Query query = parser.Parse(keywords);
                bq.Add(query, BooleanClause.Occur.MUST);
            }
            //搜索
            //Sort sort = new Sort(new SortField(null, SortField.DOC, true)); //排序
            Sort sort = Sort.RELEVANCE;
            Hits hits = search.Search(bq, sort);

            DataTable dt = new DataTable();

            dt.Columns.Add("id", Type.GetType("System.Int32"));
            dt.Columns.Add("site_id", Type.GetType("System.Int32"));
            dt.Columns.Add("channel_id", Type.GetType("System.Int32"));
            dt.Columns.Add("category_id", Type.GetType("System.Int32"));
            dt.Columns.Add("title", Type.GetType("System.String"));
            dt.Columns.Add("call_index", Type.GetType("System.String"));
            dt.Columns.Add("img_url", Type.GetType("System.String"));
            dt.Columns.Add("tags", Type.GetType("System.String"));
            dt.Columns.Add("zhaiyao", Type.GetType("System.String"));
            dt.Columns.Add("add_time", Type.GetType("System.DateTime"));
            dt.Columns.Add("update_time", Type.GetType("System.DateTime"));
            dt.Columns.Add("titlehighlight", Type.GetType("System.String"));
            dt.Columns.Add("zhaiyaohighlight", Type.GetType("System.String"));

            int    i = 0;
            int    recordCount = hits.Length();
            string title, zhaiyao;

            while (i < top && i < recordCount && dt.Rows.Count < top)
            {
                title   = hits.Doc(i).Get("title");
                zhaiyao = hits.Doc(i).Get("zhaiyao");

                DataRow row = dt.NewRow();
                row["id"]          = Utils.StrToInt(hits.Doc(i).Get("id"), 0);
                row["site_id"]     = Utils.StrToInt(hits.Doc(i).Get("sid"), 0);
                row["channel_id"]  = Utils.StrToInt(hits.Doc(i).Get("hid"), 0);
                row["category_id"] = Utils.StrToInt(hits.Doc(i).Get("cid"), 0);
                row["title"]       = title;
                row["call_index"]  = hits.Doc(i).Get("cidx");
                row["tags"]        = hits.Doc(i).Get("tags");
                row["zhaiyao"]     = zhaiyao;
                row["img_url"]     = hits.Doc(i).Get("img");
                row["add_time"]    = Utils.StrToDateTime(hits.Doc(i).Get("atime"));
                row["update_time"] = Utils.StrToDateTime(hits.Doc(i).Get("utime"));
                //高亮
                PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                highlighter.FragmentSize = 50;
                title = highlighter.GetBestFragment(keywords, title);
                if (string.IsNullOrEmpty(title))
                {
                    row["titlehighlight"] = row["title"];
                }
                else
                {
                    row["titlehighlight"] = title;
                }
                zhaiyao = highlighter.GetBestFragment(keywords, zhaiyao);
                if (string.IsNullOrEmpty(zhaiyao))
                {
                    row["zhaiyaohighlight"] = row["zhaiyao"];
                }
                else
                {
                    row["zhaiyaohighlight"] = zhaiyao;
                }
                //填充
                dt.Rows.Add(row);
                //自增
                i++;
            }
            search.Close();
            return(dt);
        }
Exemplo n.º 29
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="hits"></param>
        /// <param name="i"></param>
        /// <param name="pageSize"></param>
        /// <param name="keywords"></param>
        /// <returns></returns>
        private static DataTable HitsToDataTable(Hits hits, int i, int pageSize, string keywords)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("id", Type.GetType("System.Int32"));
            dt.Columns.Add("site_id", Type.GetType("System.Int32"));
            dt.Columns.Add("channel_id", Type.GetType("System.Int32"));
            dt.Columns.Add("category_id", Type.GetType("System.Int32"));
            dt.Columns.Add("title", Type.GetType("System.String"));
            //dt.Columns.Add("sub_title", Type.GetType("System.String"));
            dt.Columns.Add("call_index", Type.GetType("System.String"));
            dt.Columns.Add("img_url", Type.GetType("System.String"));
            dt.Columns.Add("tags", Type.GetType("System.String"));
            dt.Columns.Add("zhaiyao", Type.GetType("System.String"));
            //dt.Columns.Add("is_top", Type.GetType("System.Int32"));
            //dt.Columns.Add("is_red", Type.GetType("System.Int32"));
            //dt.Columns.Add("is_hot", Type.GetType("System.Int32"));
            //dt.Columns.Add("market_price", Type.GetType("System.Decimal"));
            //dt.Columns.Add("sell_price", Type.GetType("System.Decimal"));
            dt.Columns.Add("add_time", Type.GetType("System.DateTime"));
            dt.Columns.Add("update_time", Type.GetType("System.DateTime"));
            dt.Columns.Add("titlehighlight", Type.GetType("System.String"));
            dt.Columns.Add("zhaiyaohighlight", Type.GetType("System.String"));

            int    total = hits.Length();
            string title, zhaiyao;

            while (i < total && dt.Rows.Count < pageSize)
            {
                title   = hits.Doc(i).Get("title");
                zhaiyao = hits.Doc(i).Get("zhaiyao");

                DataRow row = dt.NewRow();
                row["id"]          = Utils.StrToInt(hits.Doc(i).Get("id"), 0);
                row["site_id"]     = Utils.StrToInt(hits.Doc(i).Get("sid"), 0);
                row["channel_id"]  = Utils.StrToInt(hits.Doc(i).Get("hid"), 0);
                row["category_id"] = Utils.StrToInt(hits.Doc(i).Get("cid"), 0);
                row["title"]       = title;
                //row["sub_title"] = hits.Doc(i).Get("sub");
                row["call_index"] = hits.Doc(i).Get("cidx");
                row["tags"]       = hits.Doc(i).Get("tags");
                row["zhaiyao"]    = zhaiyao;
                //row["is_top"] = Utils.StrToInt(hits.Doc(i).Get("top"), 0);
                //row["is_red"] = Utils.StrToInt(hits.Doc(i).Get("red"), 0);
                //row["is_hot"] = Utils.StrToInt(hits.Doc(i).Get("hot"), 0);
                row["img_url"] = hits.Doc(i).Get("img");
                //row["market_price"] = Utils.StrToDecimal(hits.Doc(i).Get("market"), 0);
                //row["sell_price"] = Utils.StrToDecimal(hits.Doc(i).Get("sell"), 0);
                row["add_time"]    = Utils.StrToDateTime(hits.Doc(i).Get("atime"));
                row["update_time"] = Utils.StrToDateTime(hits.Doc(i).Get("utime"));
                //高亮
                PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                PanGu.HighLight.Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
                highlighter.FragmentSize = 100;
                title = highlighter.GetBestFragment(keywords, title);
                if (string.IsNullOrEmpty(title))
                {
                    row["titlehighlight"] = row["title"];
                }
                else
                {
                    row["titlehighlight"] = title;
                }
                zhaiyao = highlighter.GetBestFragment(keywords, zhaiyao);
                if (string.IsNullOrEmpty(zhaiyao))
                {
                    row["zhaiyaohighlight"] = Utils.CutString(row["zhaiyao"].ToString(), 100);
                }
                else
                {
                    row["zhaiyaohighlight"] = zhaiyao;
                }
                //填充
                dt.Rows.Add(row);
                //自增
                i++;
            }
            return(dt);
        }
Exemplo n.º 30
0
        /// <summary>
        /// 产品搜索
        /// 从索引
        /// </summary>
        /// <param name="key">搜索关键字</param>
        /// <param name="categorySysNo">分类编号</param>
        /// <param name="attributes">属性列表</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">分页总数</param>
        /// <param name="recCount">总记录数</param>
        /// <param name="highLight">是否高亮关键字</param>
        /// <param name="sort">排序(0:相关度 1:销量 2:价格 3:评分 4:上架时间)</param>
        /// <param name="isDescending">true 为降序 false为升序</param>
        /// <param name="productSysNo">商品编号</param>
        /// <param name="priceSource">产品价格来源</param>
        /// <param name="priceSourceSysNo">产品价格来源编号(会员等级编号)</param>
        /// <param name="showNotFrontEndOrder">搜索前台不能下单的商品(2014-2-14 黄波 添加 物流APP需要)</param>
        /// <returns>商品列表</returns>
        /// <remarks>2013-08-08 黄波 创建</remarks>
        /// <remarks>2013-11-12 邵斌 修改 添加搜索商品系统编号:该方法暂时闲置</remarks>
        /// <remarks>2013-12-23 邵斌 添加前台是否下单字段</remarks>
        /// <remarks>2014-02-14 黄波 添加是否搜索前台不能下单的商品(物流APP需要)</remarks>
        public IList <PdProductIndex> Search(string key
                                             , int?categorySysNo
                                             , List <int> attributes
                                             , int pageSize
                                             , ref int pageIndex
                                             , ref int pageCount
                                             , ref int recCount
                                             , bool highLight    = false
                                             , int sort          = 1
                                             , bool isDescending = false
                                             , int productSysNo  = 0
                                             , ProductStatus.产品价格来源 priceSource = ProductStatus.产品价格来源.会员等级价
                                             , int priceSourceSysNo             = CustomerLevel.初级
                                             , bool showNotFrontEndOrder        = false)
        {
            var returnValue = new List <PdProductIndex>();
            var indexSearch = Hyt.Infrastructure.Lucene.ProductIndex.Searcher;

            try
            {
                pageIndex = pageIndex == 0 ? 1 : pageIndex;
                key       = key ?? ""; //查询设置初始值

                #region 搜索条件
                BooleanQuery query = new BooleanQuery();
                BooleanQuery childQuery;
                BooleanQuery esenQuery;

                #region 关键字搜索
                string keywords = key.Trim();
                if (!string.IsNullOrWhiteSpace(keywords))
                {
                    childQuery = new BooleanQuery();
                    esenQuery  = new BooleanQuery();
                    if (!IsErpCode(keywords))
                    {
                        ////全词去空格
                        //esenQuery.Add(new TermQuery(new Term("ProductName", Regex.Replace(keywords, @"\s", ""))),
                        //        BooleanClause.Occur.SHOULD);
                        //esenQuery.SetBoost(3.0F);
                        //esenQuery.Add(new TermQuery(new Term("ProductSubName", Regex.Replace(keywords, @"\s", ""))),
                        //        BooleanClause.Occur.SHOULD);
                        //esenQuery.SetBoost(3.0F);
                        //childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                        esenQuery = new BooleanQuery();
                        //分词 盘古分词
                        var keyWordsSplitBySpace = GetKeyWordsSplitBySpace(keywords);

                        if (string.IsNullOrWhiteSpace(keyWordsSplitBySpace))
                        {
                            return(null);
                        }

                        keyWordsSplitBySpace = string.Format("{0}^{1}.0", keywords, (int)Math.Pow(3, 5));

                        QueryParser productNameQueryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "ProductName", new PanGuAnalyzer(true));
                        Query       productNameQuery       = productNameQueryParser.Parse(keyWordsSplitBySpace);
                        childQuery.Add(productNameQuery, BooleanClause.Occur.SHOULD);

                        //以什么开头,输入“ja”就可以搜到包含java和javascript两项结果了
                        Query prefixQuery_productName = new PrefixQuery(new Term("ProductName", key));
                        //直接模糊匹配,假设你想搜索跟‘wuzza’相似的词语,你可能得到‘fuzzy’和‘wuzzy’。
                        Query fuzzyQuery_productName = new FuzzyQuery(new Term("ProductName", key));
                        //通配符搜索
                        Query wildcardQuery_productName = new WildcardQuery(new Term("ProductName", string.Format("*{0}*", key.Trim())));


                        childQuery.Add(prefixQuery_productName, BooleanClause.Occur.SHOULD);
                        childQuery.Add(fuzzyQuery_productName, BooleanClause.Occur.SHOULD);
                        childQuery.Add(wildcardQuery_productName, BooleanClause.Occur.SHOULD);
                        //esenQuery.Add(new QueryParser("ProductName", new PanGuAnalyzer(true)).Parse(keyWordsSplitBySpace), BooleanClause.Occur.SHOULD);
                        //esenQuery.Add(new QueryParser("ProductSubName", new PanGuAnalyzer(true)).Parse(keyWordsSplitBySpace), BooleanClause.Occur.SHOULD);

                        ////分词  按空格
                        //var keyColl = keywords.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        //foreach (var item in keyColl)
                        //{
                        //    esenQuery.Add(new TermQuery(new Term("ProductName", item)),
                        //        BooleanClause.Occur.SHOULD);
                        //    esenQuery.Add(new TermQuery(new Term("ProductSubName", item)),
                        //        BooleanClause.Occur.SHOULD);
                        //}
                        //esenQuery.SetBoost(2.9F);
                        //childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    }
                    else
                    {
                        esenQuery.Add(new TermQuery(new Term("ErpCode", Regex.Replace(keywords, @"\s", ""))),
                                      BooleanClause.Occur.SHOULD);
                        esenQuery.SetBoost(3.0F);
                        childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    }

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                #endregion

                #region 分类搜索
                if (categorySysNo.HasValue && categorySysNo.Value != 0)
                {
                    childQuery = new BooleanQuery();

                    esenQuery = new BooleanQuery();
                    esenQuery.Add(new TermQuery(new Term("Category", categorySysNo.Value.ToString())),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(3.0F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    esenQuery = new BooleanQuery();
                    esenQuery.Add(new WildcardQuery(new Term("AssociationCategory", string.Format("*,{0},*", categorySysNo.Value.ToString()))),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(2.8F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    //所有子分类
                    var childCategoryList = Hyt.BLL.Web.PdCategoryBo.Instance.GetChildAllCategory(categorySysNo.Value);
                    foreach (var item in childCategoryList)
                    {
                        esenQuery = new BooleanQuery();
                        esenQuery.Add(new TermQuery(new Term("Category", item.SysNo.ToString())),
                                      BooleanClause.Occur.SHOULD);
                        esenQuery.SetBoost(3.0F);
                        childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                        esenQuery = new BooleanQuery();
                        esenQuery.Add(new WildcardQuery(new Term("AssociationCategory", string.Format("*,{0},*", item.SysNo.ToString()))),
                                      BooleanClause.Occur.SHOULD);
                        childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    }

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                #endregion

                #region 属性搜索
                if (attributes != null)
                {
                    childQuery = new BooleanQuery();
                    esenQuery  = new BooleanQuery();

                    foreach (var item in attributes)
                    {
                        esenQuery.Add(new WildcardQuery(new Term("Attributes", string.Format("*,*:{0},*", item.ToString()))),
                                      BooleanClause.Occur.MUST);
                    }
                    childQuery.Add(esenQuery, BooleanClause.Occur.MUST);

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                #endregion

                #region 品牌搜索
                //if (brandSysNo.Value != 0)
                //{
                //    childQuery = new BooleanQuery();
                //    childQuery.Add(new TermQuery(new Term("BrandSysNo", brandSysNo.Value.ToString())),
                //               BooleanClause.Occur.SHOULD);
                //    childQuery.SetBoost(3.0F);
                //    query.Add(childQuery, BooleanClause.Occur.MUST);
                //}
                #endregion

                #region 仅搜索有效的商品

                childQuery = new BooleanQuery();
                childQuery.Add(new TermQuery(new Term("Status", ((int)Hyt.Model.WorkflowStatus.ProductStatus.产品上线状态.效).ToString())),
                               BooleanClause.Occur.SHOULD);
                query.Add(childQuery, BooleanClause.Occur.MUST);

                //2013-12-23 邵斌 添加前台是否下单字段
                if (!showNotFrontEndOrder)
                {
                    childQuery = new BooleanQuery();
                    childQuery.Add(new TermQuery(new Term("CanFrontEndOrder", ((int)Hyt.Model.WorkflowStatus.ProductStatus.商品是否前台下单.是).ToString())),
                                   BooleanClause.Occur.SHOULD);
                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }

                #endregion

                #region 排序

                //isDescending true为降序 false为升序
                SortField sf = null;
                switch (Math.Abs(sort))
                {
                case 1:    //销量
                    sf = new SortField("SalesCount", SortField.INT, isDescending);
                    break;

                case 2:    //价格
                    sf = new SortField("BasicPrice", SortField.FLOAT, isDescending);
                    break;

                case 3:    //评分
                    sf = new SortField("CommentCount", SortField.INT, isDescending);
                    break;

                case 4:    //上架时间
                    sf = new SortField("CreatedDate", SortField.STRING, isDescending);
                    break;

                default:
                    sf = new SortField(null, SortField.SCORE, false);
                    break;
                }

                Sort luceneSort;                            //排序对象

                //默认匹配度,表明是对固定信息进行搜索,所以就要进行先安匹配度来排序。这样用户搜索的商品将排在前面,方便用户筛选
                if (Math.Abs(sort) != (int)CommonEnum.LuceneProductSortType.默认匹配度)
                {
                    //无搜索关键字的时候就按模式设置的进行排序
                    luceneSort = new Sort();
                    luceneSort.SetSort(sf);
                }
                else
                {
                    //收搜索关键字时,就要先安匹配度进行排序,然后才是设置排序。
                    luceneSort = new Sort(new SortField[] { SortField.FIELD_SCORE, sf });
                }

                #endregion

                #region 商品系统编号搜索

                if (productSysNo > 0)
                {
                    childQuery = new BooleanQuery();

                    esenQuery = new BooleanQuery();
                    esenQuery.Add(new TermQuery(new Term("SysNo", productSysNo.ToString())),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(3.0F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }

                #endregion

                #endregion

                Hits hits = indexSearch.Search(query, luceneSort);

                recCount  = hits.Length();
                pageCount = (int)Math.Ceiling((double)recCount / (double)pageSize);
                if (pageIndex <= 0)
                {
                    pageIndex = 1;
                }
                if (pageIndex > pageCount)
                {
                    pageIndex = pageCount;
                }

                var recordIndex = Math.Max((pageIndex - 1) * pageSize, 0);

                PdProductIndex pdProductIndex;
                var            simpleHtmlFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                var            T_ProductName       = "";
                while (recordIndex < recCount && returnValue.Count < pageSize)
                {
                    try
                    {
                        pdProductIndex = Hyt.Infrastructure.Lucene.ProductIndex.Instance.DocumentToModel(hits.Doc(recordIndex));
                        string productName = pdProductIndex.ProductName;
                        if (highLight && !string.IsNullOrEmpty(key))
                        {
                            var highlighter = new PanGu.HighLight.Highlighter(simpleHtmlFormatter, new PanGu.Segment())
                            {
                                FragmentSize = 50
                            };
                            T_ProductName = highlighter.GetBestFragment(key.Trim(), pdProductIndex.ProductName);
                            if (!string.IsNullOrWhiteSpace(T_ProductName))
                            {
                                pdProductIndex.ProductName = T_ProductName;
                            }
                        }
                        pdProductIndex.RankPrice = GetProductRankPrice(pdProductIndex.Prices, pdProductIndex.BasicPrice, priceSource, priceSourceSysNo);

                        returnValue.Add(pdProductIndex);
                    }
                    catch (Exception ex)
                    {
                        BLL.Log.SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, ex);
                    }
                    finally
                    {
                        recordIndex++;
                    }
                }
            }
            catch (Exception ex)
            {
                BLL.Log.SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, ex);
            }
            finally
            {
                // indexSearch.Close();
            }
            return(returnValue);
        }