Пример #1
0
 public static LuceneResultNode SearchEx(IndexBase index, SearchNode node)
 {
     try
     {
         var query = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, node.Queries, node.Fields, node.Occurs, new PanGuAnalyzer(true));
         foreach (NumberRangeNode n in node.NumberRangeFiters)
         {
             query = new FilteredQuery(query, NumericRangeFilter.NewIntRange(n.FieldName, n.MinValue, n.MaxValue, true, true));
         }
         foreach (LongRangeNode lr in node.LongRnageFilters)
         {
             query = new FilteredQuery(query, NumericRangeFilter.NewLongRange(lr.FieldName, lr.MinValue, lr.MaxValue, true, true));
         }
         foreach (ContainFilterNode cf in node.ContainFilters)
         {
             query = new FilteredQuery(query, new ContainFilter(new Term(cf.FieldName, cf.Text)));
         }
         return(ResponseSearch(index, query, node, 0));
     }
     catch (Lucene.Net.QueryParsers.ParseException)
     {
         return(new LuceneResultNode()
         {
             AllCount = 0, Result = new List <string>()
         });
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Пример #2
0
        /// <summary>
        ///根据关键字进行查询,根据int的范围进行搜索
        /// </summary>
        /// <param name="Title"></param>
        /// <returns></returns>
        public List <Bpo_JobEntity> SearchJobList3(string Title, int MinId, int MaxId)
        {
            List <Bpo_JobEntity> jobList = new List <Bpo_JobEntity>();
            string      path             = "F://LuceneIndexDir";
            FSDirectory dir = FSDirectory.Open(path);

            IndexSearcher searcher = new IndexSearcher(dir);//查询器

            /*
             * Title参数根据判断进行分词,可以支持分词查询,多个词之间是or的关系,搜索的时候中间加个空格,
             * 如:wcs 单子 这样就会把包含wcs 或 包含单子关键字的工单搜索出来
             */
            QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", new PanGuAnalyzer());

            Query query = parser.Parse(Title); //根据名称查询

            //根据id区间区间搜索
            NumericRangeFilter <int> intFilter = null;

            if (MinId > 0 && MaxId == 0)
            {
                intFilter = NumericRangeFilter.NewIntRange("Id", MinId, int.MaxValue, true, true);
            }
            else if (MaxId > 0 && MinId == 0)
            {
                intFilter = NumericRangeFilter.NewIntRange("Id", 0, MaxId, true, true);
            }
            else if (MaxId > 0 && MinId > 0)
            {
                intFilter = NumericRangeFilter.NewIntRange("Id", MinId, MaxId, true, true);
            }

            //定义排序
            SortField sortField  = new SortField("Id", SortField.INT, false);        //降序
            SortField sortField2 = new SortField("CompanyId", SortField.INT, false); //降序
            Sort      sort       = new Sort(sortField, sortField2);

            //取搜索结果方法1:
            TopDocs docs = searcher.Search(query, intFilter, 1000, sort); //找到的结果取100条数据

            foreach (ScoreDoc sd in docs.ScoreDocs)                       //从docs.ScoreDocs取数据
            {
                Document doc = searcher.Doc(sd.Doc);
                jobList.Add(new Bpo_JobEntity
                {
                    Id          = Convert.ToInt32(doc.Get("Id") ?? "-100"),
                    Title       = doc.Get("Title"),
                    UserId      = Convert.ToInt32(doc.Get("UserId") ?? "-100"),
                    UserName    = doc.Get("UserName"),
                    CompanyId   = Convert.ToInt32(doc.Get("CompanyId") ?? "-100"),
                    CompanyName = doc.Get("CompanyName"),
                    FullAddress = doc.Get("FullAddress"),
                    CreateDate  = Convert.ToDateTime(doc.Get("CreateDate"))
                });
            }
            return(jobList);
        }
Пример #3
0
        public virtual Filter GetFilter(XmlElement e)
        {
            string field          = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
            string lowerTerm      = DOMUtils.GetAttributeOrFail(e, "lowerTerm");
            string upperTerm      = DOMUtils.GetAttributeOrFail(e, "upperTerm");
            bool   lowerInclusive = DOMUtils.GetAttribute(e, "includeLower", true);
            bool   upperInclusive = DOMUtils.GetAttribute(e, "includeUpper", true);
            int    precisionStep  = DOMUtils.GetAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);

            string type = DOMUtils.GetAttribute(e, "type", "int");

            try
            {
                Filter filter;
                if (type.Equals("int", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewIntRange(field, precisionStep, Convert.ToInt32(lowerTerm), Convert.ToInt32(upperTerm), lowerInclusive,
                                                            upperInclusive);
                }
                else if (type.Equals("long", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewLongRange(field, precisionStep, Convert
                                                             .ToInt64(lowerTerm), Convert.ToInt64(upperTerm), lowerInclusive,
                                                             upperInclusive);
                }
                else if (type.Equals("double", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewDoubleRange(field, precisionStep, Convert
                                                               .ToDouble(lowerTerm), Convert.ToDouble(upperTerm), lowerInclusive,
                                                               upperInclusive);
                }
                else if (type.Equals("float", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewFloatRange(field, precisionStep, Convert
                                                              .ToSingle(lowerTerm), Convert.ToSingle(upperTerm), lowerInclusive,
                                                              upperInclusive);
                }
                else
                {
                    throw new ParserException("type attribute must be one of: [long, int, double, float]");
                }
                return(filter);
            }
            catch (FormatException nfe)
            {
                if (strictMode)
                {
                    throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
                }
                return(NO_MATCH_FILTER);
            }
        }
        public List <News> Search(string keywords)
        {
            Directory     dir    = FSDirectory.Open(new io.DirectoryInfo(HttpContext.Current.Server.MapPath("/Indexs/")), new SimpleFSLockFactory());
            IndexReader   reader = IndexReader.Open(dir, true);
            IndexSearcher search = new IndexSearcher(reader);

            MultiFieldQueryParser multifield = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "Title", "Content" }, new PanGuAnalyzer());

            multifield.PhraseSlop      = 3;
            multifield.DefaultOperator = QueryParser.Operator.AND;
            Query muqu = multifield.Parse(keywords);

            //MultiPhraseQuery multi = new MultiPhraseQuery();
            //multi.Add(new Term[] {new Term("Content","中国"), new Term("Content", "智慧"), new Term("Title", "中国"), new Term("Title", "智慧") });

            //PhraseQuery query = new PhraseQuery();
            //query.Add(new Term("Content", keywords));

            NumericRangeFilter <int> filter = NumericRangeFilter.NewIntRange("NewsId", 1, 10, true, true);

            Sort sort = new Sort();

            sort.SetSort(new SortField("OrderId", SortField.LONG, true));

            TopFieldDocs fields = search.Search(muqu, filter, 1000, sort);

            ScoreDoc[] docs = fields.ScoreDocs;

            List <News> newslist = new List <News>();

            for (int i = 0; i < docs.Length; i++)
            {
                News     news = new News();
                Document doc  = search.Doc(docs[i].Doc);
                news.NewsId = Convert.ToInt32(doc.Get("NewsId"));
                news.Title  = doc.Get("Title");

                SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span style=\"color:red\">", "</span>");
                Highlighter         high      = new Highlighter(formatter, new PanGu.Segment());
                high.FragmentSize = 120;
                news.Content      = high.GetBestFragment(keywords, doc.Get("Content"));



                news.AddTime = Convert.ToDateTime(doc.Get("Date"));
                news.OrderId = Convert.ToInt64(doc.Get("OrderId"));

                newslist.Add(news);
            }

            return(newslist);
        }
Пример #5
0
        public static void Show()
        {
            FSDirectory   dir      = FSDirectory.Open(TestIndexPath);
            IndexSearcher searcher = new IndexSearcher(dir);              //查找器
            {
                TermQuery query = new TermQuery(new Term("title", "英文")); //包含
                TopDocs   docs  = searcher.Search(query, null, 10000);    //找到的数据
                foreach (ScoreDoc sd in docs.ScoreDocs)
                {
                    Document doc = searcher.Doc(sd.Doc);
                    Console.WriteLine(string.Format("{0} {1}  {2} id={3}", doc.Get("title"), doc.Get("time"), doc.Get("price"), doc.Get("id")));
                }
                Console.WriteLine("1一共命中了{0}个", docs.TotalHits);
            }

            QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", new PanGuAnalyzer());//解析器
            {
                string keyword = "旧时光咖啡打几个给答复是加快了高科技打瞌睡了加工费拉的方式及规范了卡迪夫空间是梵蒂冈加工费";
                //string keyword = "旧时光 咖啡 打几个 给答复 是加快了 高科技 打瞌睡 了加工费 拉的方式及规范了卡迪夫空间是梵蒂冈加工费";
                Query query = parser.Parse(keyword);
                {
                    TopDocs docs = searcher.Search(query, null, 10000);//找到的数据
                    foreach (ScoreDoc sd in docs.ScoreDocs)
                    {
                        Document doc = searcher.Doc(sd.Doc);
                        Console.WriteLine(string.Format("{0} {1}  {2} id={3}", doc.Get("title"), doc.Get("time"), doc.Get("price"), doc.Get("id")));
                    }
                    Console.WriteLine("2一共命中了{0}个", docs.TotalHits);
                }
                {
                    NumericRangeFilter <int> timeFilter = NumericRangeFilter.NewIntRange("time", 20150000, 20151822, true, true); //过滤
                    SortField sortPrice = new SortField("price", SortField.DOUBLE, false);                                        //降序
                    SortField sortTime  = new SortField("time", SortField.INT, true);                                             //升序
                    Sort      sort      = new Sort(sortPrice, sortTime);                                                          //排序

                    TopDocs docs   = searcher.Search(query, timeFilter, 10000, sort);                                             //找到的数据
                    string  result = "";
                    foreach (ScoreDoc sd in docs.ScoreDocs)
                    {
                        Document doc = searcher.Doc(sd.Doc);
                        Console.WriteLine(string.Format("{0} {1} {2} id={3}", doc.Get("title"), doc.Get("time"), doc.Get("price"), doc.Get("id")));
                        if (doc.Get("title").Contains("金币卡"))
                        {
                            result += doc.Get("title");
                        }
                    }
                    Console.WriteLine("3一共命中了{0}个", docs.TotalHits);
                    //Console.WriteLine("result={0}", result);
                }
            }
        }
Пример #6
0
 /// <summary>
 /// 通过类型来获取
 /// </summary>
 /// <param name="property"></param>
 /// <param name="min"></param>
 /// <param name="max"></param>
 /// <returns></returns>
 public static Filter GetNumberFilter(PropertyInfo property, double min, double max)
 {
     if (property.PropertyType == typeof(double) || property.PropertyType == typeof(decimal))
     {
         return(NumericRangeFilter.NewDoubleRange(property.Name, min, max, true, true));
     }
     else if (property.PropertyType == typeof(float))
     {
         return(NumericRangeFilter.NewFloatRange(property.Name, (float)min, (float)max, true, true));
     }
     else if (property.PropertyType == typeof(int))
     {
         return(NumericRangeFilter.NewIntRange(property.Name, (int)min, (int)max, true, true));
     }
     else
     {
         return(null);
     }
 }
Пример #7
0
        public List <Product> Search(string keyWords)
        {
            List <Product> product = new List <Product>();

            Query query = new TermQuery(new Term("Content", keyWords));

            System.IO.DirectoryInfo IndexDir = new System.IO.DirectoryInfo(path);
            Directory dict = FSDirectory.Open(IndexDir, new SimpleFSLockFactory());

            //搜索器
            IndexSearcher search = new IndexSearcher(dict, true);

            //过滤器
            NumericRangeFilter <int> filter = NumericRangeFilter.NewIntRange("ProductId", 1, 6, true, true);

            //排序字段
            Sort sort = new Sort(new SortField("OrderId", SortField.LONG, true));

            //执行搜索
            TopFieldDocs docs = search.Search(query, null, 1000, sort);

            foreach (var p in docs.ScoreDocs)
            {
                Product  pro = new Product();
                Document doc = search.Doc(p.Doc);
                pro.ProductId   = Convert.ToInt32(doc.Get("ProductId"));
                pro.ProductName = $"{doc.Get("ProductName")}文档ID:{p.Doc},内部ID:{pro.ProductId}";
                SimpleHTMLFormatter html = new SimpleHTMLFormatter("<span style=\"color:red\">", "</span>");
                Highlighter         high = new Highlighter(html, new PanGu.Segment());
                high.FragmentSize = 120;
                pro.Detail        = high.GetBestFragment(keyWords, doc.Get("Content"));
                pro.Detail        = doc.Get("Content");
                pro.CreateTime    = doc.Get("CreateTime");
                pro.OrderId       = doc.Get("OrderId");
                product.Add(pro);
            }

            search.Dispose();
            dict.Dispose();

            return(product);
        }
        public NumericRangeFilterInstance <int> CreateIntRangeFilter(string fieldName, int precisionStep, object min, object max, bool minInclusive, bool maxInclusive)
        {
            int?intMin = null;

            if (min != null && min != Null.Value && min != Undefined.Value && min is int)
            {
                intMin = Convert.ToInt32(min);
            }

            int?intMax = null;

            if (max != null && max != Null.Value && max != Undefined.Value && max is int)
            {
                intMax = Convert.ToInt32(max);
            }

            var filter = NumericRangeFilter.NewIntRange(fieldName, precisionStep, intMin, intMax, minInclusive, maxInclusive);

            return(new NumericRangeFilterInstance <int>(this.Engine.Object.InstancePrototype, filter));
        }
Пример #9
0
        public List <News> Search(string Keywords)
        {
            List <News>   newsList = new List <News>();
            Directory     dir      = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("/Index")), new SimpleFSLockFactory());
            IndexSearcher search   = new IndexSearcher(dir);

            PhraseQuery query = new PhraseQuery();

            query.Add(new Term("Content", Keywords));
            //query.Slop = 8;

            NumericRangeFilter <int> range = NumericRangeFilter.NewIntRange("NewsId", 1, 10, true, true);

            Sort sort = new Sort(new SortField("OrderId", SortField.LONG, true));


            TopFieldDocs fileds = search.Search(query, range, 1000, sort);

            ScoreDoc[] docs = fileds.ScoreDocs;
            for (int i = 0; i < docs.Length; i++)
            {
                News     news  = new News();
                int      docid = docs[i].Doc;
                Document doc   = search.Doc(docid);
                news.NewsId = Convert.ToInt32(doc.Get("NewsId"));
                news.Title  = doc.Get("Title");

                SimpleHTMLFormatter html = new SimpleHTMLFormatter("<span style=\"color:red\" >", "</span>");
                Highlighter         high = new Highlighter(html, new PanGu.Segment());
                high.FragmentSize = 120;

                news.Content = high.GetBestFragment(Keywords, doc.Get("Content"));

                news.AddTime = Convert.ToDateTime(doc.Get("AddTime"));

                news.OrderId = Convert.ToInt64(doc.Get("OrderId"));

                newsList.Add(news);
            }
            return(newsList);
        }
Пример #10
0
        static void Main(string[] args)
        {
            Directory dir      = new RAMDirectory();
            Analyzer  analyzer = new SimpleAnalyzer();

            var indexWriter = new IndexWriter(dir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);

            foreach (var document in CreateDocuments())
            {
                indexWriter.AddDocument(document);
            }
            indexWriter.Commit();
            indexWriter.Dispose();



            var   parser = new QueryParser(Version, TextFieldName, analyzer);
            Query q      = parser.Parse("Anders");

            var fromFilter = DateTime2Int(new DateTime(2010, 3, 1));
            var toFilter   = DateTime2Int(new DateTime(2010, 4, 1));

            Filter filter = NumericRangeFilter.NewIntRange(DateFieldName, fromFilter, toFilter, true, true);
            //Filter filter = NumericRangeFilter.NewIntRange(DateFieldName, fromFilter, null, true, true); // openended

            var searcher = new IndexSearcher(dir, true);

            TopDocs hits = searcher.Search(q, filter, 5, Sort.RELEVANCE);

            Console.WriteLine("Found {0} document(s) that matched query '{1}' with filter {2}:", hits.TotalHits, q, filter);
            foreach (ScoreDoc match in hits.ScoreDocs)
            {
                Document doc = searcher.Doc(match.Doc);

                Console.WriteLine("Matched = {0} with date {1}", doc.Get(TextFieldName), doc.Get(DateFieldName));
            }
            searcher.Dispose();
        }
Пример #11
0
        public static void Show()
        {
            FSDirectory   dir      = FSDirectory.Open(StaticConstant.TestIndexPath);
            IndexSearcher searcher = new IndexSearcher(dir);               //查找器
            {
                TermQuery query = new TermQuery(new Term("title", "图书馆")); //包含
                TopDocs   docs  = searcher.Search(query, null, 10000);     //找到的数据
                foreach (ScoreDoc sd in docs.ScoreDocs)
                {
                    Document doc = searcher.Doc(sd.Doc);
                    Console.WriteLine("***************************************");
                    Console.WriteLine(string.Format("id={0}", doc.Get("id")));
                    Console.WriteLine(string.Format("title={0}", doc.Get("title")));
                    Console.WriteLine(string.Format("time={0}", doc.Get("time")));
                    Console.WriteLine(string.Format("price={0}", doc.Get("price")));
                    Console.WriteLine(string.Format("content={0}", doc.Get("content")));
                }
                Console.WriteLine("1一共命中了{0}个", docs.TotalHits);
            }

            QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", new PanGuAnalyzer());//解析器
            {
                //string keyword = "高中政治人教新课标选修生活中的法律常识";
                string keyword = "高中政治 人 教 新课 标 选修 生活 中的 法律常识";
                {
                    Query   query = parser.Parse(keyword);
                    TopDocs docs  = searcher.Search(query, null, 10000);//找到的数据

                    int i = 0;
                    foreach (ScoreDoc sd in docs.ScoreDocs)
                    {
                        if (i++ < 1000)
                        {
                            Document doc = searcher.Doc(sd.Doc);
                            Console.WriteLine("***************************************");
                            Console.WriteLine(string.Format("id={0}", doc.Get("id")));
                            Console.WriteLine(string.Format("title={0}", doc.Get("title")));
                            Console.WriteLine(string.Format("time={0}", doc.Get("time")));
                            Console.WriteLine(string.Format("price={0}", doc.Get("price")));
                        }
                    }
                    Console.WriteLine($"一共命中{docs.TotalHits}");
                }
                {
                    Query query = parser.Parse(keyword);
                    NumericRangeFilter <int> timeFilter = NumericRangeFilter.NewIntRange("time", 20190101, 20191231, true, true); //过滤
                    SortField sortPrice = new SortField("price", SortField.DOUBLE, false);                                        //降序
                    SortField sortTime  = new SortField("time", SortField.INT, true);                                             //升序
                    Sort      sort      = new Sort(sortTime, sortPrice);                                                          //排序 哪个前哪个后

                    TopDocs docs = searcher.Search(query, timeFilter, 10000, sort);                                               //找到的数据
                    int     i    = 0;
                    foreach (ScoreDoc sd in docs.ScoreDocs)
                    {
                        if (i++ < 1000)
                        {
                            Document doc = searcher.Doc(sd.Doc);
                            Console.WriteLine("***************************************");
                            Console.WriteLine(string.Format("id={0}", doc.Get("id")));
                            Console.WriteLine(string.Format("title={0}", doc.Get("title")));
                            Console.WriteLine(string.Format("time={0}", doc.Get("time")));
                            Console.WriteLine(string.Format("price={0}", doc.Get("price")));
                        }
                    }
                    Console.WriteLine("3一共命中了{0}个", docs.TotalHits);
                }
            }
        }
Пример #12
0
 static Filter GetFilter()
 {
     return(NumericRangeFilter.NewIntRange("ID", 1, 50, true, false)); // [2; 5) range
 }
Пример #13
0
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        public List <News> search(string keyWord)
        {
            //定义分词器
            Analyzer analyzer = new PanGuAnalyzer();

            //定义索引目录路径
            string path = Server.MapPath("/Indexs");

            //定义索引用到的目录
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(path), new NoLockFactory());

            //返回读取给定目录中索引的IndexReader。
            //您应该传递readOnly =true,因为它提供了更好的并发性能,除非您打算对读取器执行写操作(删除文档或更改规范)。
            IndexReader reader = IndexReader.Open(directory, true);

            IndexSearcher searcher = new IndexSearcher(reader);

            TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true);

            //============完整设置查询条件、过滤器、结果排序的使用方法==============
            //设置查询条件
            PhraseQuery query = new PhraseQuery();

            query.Slop = 8;
            query.Add(new Term("Content", keyWord));


            //设置过滤器
            NumericRangeFilter <int> IdRange = NumericRangeFilter.NewIntRange("Id", 1, 6, true, true);

            //结果排序
            Sort sort = new Sort(new SortField("OrderId", SortField.LONG, true));

            // 使用query这个查询条件进行搜索,搜索结果放入collector
            TopFieldDocs tt = searcher.Search(query, null, 1000, sort);

            //按排序来取
            ScoreDoc[] docs = tt.ScoreDocs;
            //============ 完整设置查询条件、过滤器、结果排序的使用方法 ==============

            //  从查询结果中取出第m条到第n条的数据
            //collector.GetTotalHits()表示总的结果条数
            //searcher.Search(query, null, collector);
            //ScoreDoc[] docs = collector.TopDocs(0, collector.TotalHits).ScoreDocs;

            // 遍历查询结果
            List <News> resultList = new List <News>();

            for (int i = 0; i < docs.Length; i++)
            {
                // 拿到文档的id,因为Document可能非常占内存(DataSet和DataReader的区别)
                int docId = docs[i].Doc;
                // 所以查询结果中只有id,具体内容需要二次查询
                // 根据id查询内容:放进去的是Document,查出来的还是Document
                Document doc    = searcher.Doc(docId);
                News     result = new News();
                result.Id = Convert.ToInt32(doc.Get("Id"));

                result.Title = doc.Get("Title");

                result.AddTime = Convert.ToDateTime(doc.Get("AddTime"));

                SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");
                //构造一个高亮对象,它将应用改革才创建的格式化
                Highlighter highter = new Highlighter(formatter, new PanGu.Segment());

                //设置片段的长度,应该是格式化搜索词后带html标签的长度
                highter.FragmentSize = 120;

                //调用方法,替换数据title中的关键词,也就是高亮此关键词
                result.Content = highter.GetBestFragment(keyWord, doc.Get("Content"));

                resultList.Add(result);
            }

            //reader.Close();
            reader.Dispose();

            return(resultList);
        }