Esempio n. 1
0
        public ActionResult GetSearchArea(float minX, float minY, float maxX, float maxY, int page = 1, int category = 0, int price = 0)
        {
            var model = new QuerySource();

            var result = new List<LinkItem>();

            QueryTerm query = new QueryTerm();

            query.MinX = minX;
            query.MinY = minY;
            query.MaxX = maxX;
            query.MaxY = maxY;
            if (category != 0)
            {
                query.MediaCode = category;
                query.MediaMaxCode = Utilities.GetMaxCode(category);
            }
            if (price != 0)
            {
                query.Price = price;
            }

            var pageSize = 10;

            int totalHits = 0;

            SearchFilter sf = new SearchFilter();

            sf.PageSize = pageSize;

            sf.Skip = (page - 1) * pageSize;

            sf.Take = pageSize;

            sf.SortProperty = SortProperty.Published;

            sf.SortDirection = SortDirection.Descending;

            result = OutDoorLuceneService.Search(query, sf, out totalHits);

            model.Items = result;

            model.TotalCount = totalHits;

            model.CurrentPage = page;

            model.PageSize = pageSize;

            return Json(model, JsonRequestBehavior.AllowGet);
        }
Esempio n. 2
0
        private SearchFilter GetSearchFilter(string q, int sortOrder, int descending, int page, int pageSize)
        {
            var searchFilter = new SearchFilter
            {
                PageSize = pageSize,
                SearchTerm = q,
                Skip = (page - 1) * pageSize, // pages are 1-based.
                Take = pageSize
            };
            searchFilter.SortProperty = (SortProperty)sortOrder;

            searchFilter.SortDirection = (SortDirection)descending;

            return searchFilter;
        }
Esempio n. 3
0
        private static Query ParseQuery(QueryTerm queryTerm, SearchFilter searchFilter)
        {
            var combineQuery = new BooleanQuery();

            #region 关键字查询构建
            if (!String.IsNullOrWhiteSpace(searchFilter.SearchTerm))
            {
                var fields = new[] {
                    OutDoorIndexFields.Title,
                    OutDoorIndexFields.Description,
                    OutDoorIndexFields.AreaCate,
                    OutDoorIndexFields.IndustryCate,
                    OutDoorIndexFields.CrowdCate,
                    OutDoorIndexFields.PurposeCate,
                    OutDoorIndexFields.MediaCateName,
                    OutDoorIndexFields.CityCateName,
                    OutDoorIndexFields.FormatName,
                    OutDoorIndexFields.PeriodName,
                    OutDoorIndexFields.OwnerName
                };
                var analyzer = new PanGuAnalyzer();
                //var analyzer = new StandardAnalyzer(LuceneCommon.LuceneVersion);
                var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields, analyzer);

                var query = queryParser.Parse(searchFilter.SearchTerm);

                //conjuction 一起选择
                var conjuctionQuery = new BooleanQuery();
                conjuctionQuery.Boost = 2.0f;

                //disjunction 分离
                var disjunctionQuery = new BooleanQuery();
                disjunctionQuery.Boost = 0.1f;

                //wildCard 通配符
                var wildCardQuery = new BooleanQuery();
                wildCardQuery.Boost = 0.5f;

                var escapedSearchTerm = Escape(searchFilter.SearchTerm);

                var exactIdQuery = new TermQuery(new Term(OutDoorIndexFields.Title, escapedSearchTerm));

                exactIdQuery.Boost = 2.5f;

                var wildCardIdQuery = new WildcardQuery(new Term(OutDoorIndexFields.Title, "*" + escapedSearchTerm + "*"));

                foreach (var term in GetSearchTerms(searchFilter.SearchTerm))
                {
                    var termQuery = queryParser.Parse(term);
                    conjuctionQuery.Add(termQuery, Occur.MUST);
                    disjunctionQuery.Add(termQuery, Occur.SHOULD);

                    foreach (var field in fields)
                    {
                        var wildCardTermQuery = new WildcardQuery(new Term(field, term + "*"));
                        wildCardTermQuery.Boost = 0.7f;
                        wildCardQuery.Add(wildCardTermQuery, Occur.SHOULD);
                    }
                }
                //关键查询
                var keywordsQuery =
                    conjuctionQuery.Combine(new Query[] { exactIdQuery, wildCardIdQuery, conjuctionQuery, disjunctionQuery, wildCardQuery });

                combineQuery.Add(keywordsQuery, Occur.MUST);
            }
            #endregion

            #region 指定媒体ID查询
            if (queryTerm.MediaID != 0)
            {
                var mediaIdQuery = new TermQuery(new Term(OutDoorIndexFields.ID, queryTerm.MediaID.ToString()));
                combineQuery.Add(mediaIdQuery, Occur.MUST);
            }
            #endregion

            #region 用户状态
            var memberStatusQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.MemberStatus, (int)MemberStatus.CompanyAuth, 99, true, true);
            combineQuery.Add(memberStatusQuery, Occur.MUST);
            #endregion

            #region 审核状态查询构建
            var verifyStatus = NumericRangeQuery.NewIntRange(OutDoorIndexFields.Status, (int)OutDoorStatus.ShowOnline, 99, true, true);
            combineQuery.Add(verifyStatus, Occur.MUST);
            #endregion

            #region 指定用户ID查询
            if (queryTerm.MemberID != 0)
            {
                var memberIdQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.MemberID, queryTerm.MemberID, queryTerm.MemberID, true, true);
                combineQuery.Add(memberIdQuery, Occur.MUST);
            }
            #endregion

            #region 城市查询
            if (queryTerm.City != 0)
            {
                var cityQuery = NumericRangeQuery.NewLongRange(OutDoorIndexFields.CityCateCode, queryTerm.CityCateCode, queryTerm.CityMaxCode, true, true);
                combineQuery.Add(cityQuery, Occur.MUST);
            }
            #endregion

            #region 认证状态
            if (queryTerm.AuthStatus != 0)
            {
                var authStatusQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.AuthStatus, queryTerm.AuthStatus, queryTerm.AuthStatus, true, true);
                combineQuery.Add(authStatusQuery, Occur.MUST);
            }
            #endregion

            #region 经纬度搜索
            if (queryTerm.MinX != 0)
            {
                var latQuery = NumericRangeQuery.NewDoubleRange(OutDoorIndexFields.Lat, queryTerm.MinX, queryTerm.MaxX, true, true);
                var lngQuery = NumericRangeQuery.NewDoubleRange(OutDoorIndexFields.Lng, queryTerm.MinY, queryTerm.MaxY, true, true);
                combineQuery.Add(latQuery, Occur.MUST);
                combineQuery.Add(lngQuery, Occur.MUST);
            }
            #endregion

            #region 媒体类别查询
            if (queryTerm.MediaCode != 0)
            {
                var mediaCodeQuery = NumericRangeQuery.NewLongRange(OutDoorIndexFields.MediaCateCode,
                    queryTerm.MediaCateCode, queryTerm.MediaMaxCode, true, true);
                combineQuery.Add(mediaCodeQuery, Occur.MUST);
            }
            #endregion

            #region 媒体表现形式查询
            if (queryTerm.FormatCode != 0)
            {
                var FormatCodeCodeQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.FormatCode,
                    queryTerm.FormatCode, queryTerm.FormatCode, true, true);
                combineQuery.Add(FormatCodeCodeQuery, Occur.MUST);
            }
            #endregion

            #region 媒体所有权查询
            //if (queryTerm.OwnerCode != 0)
            //{
            //    var OwnerCodeCodeQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.OwnerCode,
            //        queryTerm.OwnerCode, queryTerm.OwnerCode, true, true);
            //    combineQuery.Add(OwnerCodeCodeQuery, Occur.MUST);
            //}
            #endregion

            #region 媒体购买周期查询
            if (queryTerm.PeriodCode != 0)
            {
                var PeriodCodeCodeQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.PeriodCode,
                    queryTerm.PeriodCode, queryTerm.PeriodCode, true, true);
                combineQuery.Add(PeriodCodeCodeQuery, Occur.MUST);
            }
            #endregion

            #region 媒体价格区间查询
            if (queryTerm.Price != 0)
            {
                var rangeValue = EnumHelper.GetPriceValue(queryTerm.Price);
                var PriceQuery = NumericRangeQuery.NewDoubleRange(OutDoorIndexFields.Price,
                    Convert.ToDouble(rangeValue.Min), Convert.ToDouble(rangeValue.Max), true, true);
                combineQuery.Add(PriceQuery, Occur.MUST);
            }
            #endregion

            #region 媒体档期查询
            if (queryTerm.DeadLine != 0)
            {
                var minValue = (DateTime.Now.AddYears(-10)).Ticks;
                var maxValue = (new DateTime(DateTime.Now.Year, queryTerm.DeadLine, 1)).Ticks;
                var DeadLineQuery = NumericRangeQuery.NewLongRange(OutDoorIndexFields.DeadLine,
                    Convert.ToInt64(minValue), Convert.ToInt64(maxValue), true, true);
                combineQuery.Add(DeadLineQuery, Occur.MUST);
            }
            #endregion

            #region 媒体档期时间查询
            if (!string.IsNullOrEmpty(queryTerm.Dq))
            {
                var minValue = (DateTime.Now.AddYears(-10)).Ticks;
                var maxValue = Convert.ToDateTime(queryTerm.Dq).Ticks;
                var DqQuery = NumericRangeQuery.NewLongRange(OutDoorIndexFields.DeadLine,
                    Convert.ToInt64(minValue), Convert.ToInt64(maxValue), true, true);
                combineQuery.Add(DqQuery, Occur.MUST);
            }
            #endregion

            return combineQuery;
        }
Esempio n. 4
0
 private static SortField GetSortField(SearchFilter searchFilter)
 {
     switch (searchFilter.SortProperty)
     {
         case SortProperty.Hit:
             return new SortField(OutDoorIndexFields.Hit, SortField.INT, reverse: searchFilter.SortDirection.Equals(SortDirection.Descending));
         case SortProperty.Price:
             return new SortField(OutDoorIndexFields.Price, SortField.DOUBLE, reverse: searchFilter.SortDirection.Equals(SortDirection.Descending));
         case SortProperty.Published:
             return new SortField(OutDoorIndexFields.Published, SortField.LONG, reverse: searchFilter.SortDirection.Equals(SortDirection.Descending));
     }
     return SortField.FIELD_SCORE;
 }
Esempio n. 5
0
        public List<LinkItem> Search(QueryTerm queryTerm, SearchFilter searchFilter, out int totalHits)
        {
            if (!Directory.Exists(LuceneCommon.IndexOutDoorDirectory))
            {
                totalHits = 0;
                return new List<LinkItem>();
            }
            SortField sortField = GetSortField(searchFilter);

            var sortFieldArry = new List<SortField>(){
                new SortField(OutDoorIndexFields.SuggestStatus, SortField.INT, reverse: true)
            };
            sortFieldArry.Add(sortField);

            int numRecords = searchFilter.Skip + searchFilter.Take;

            using (var directory = new SimpleFSDirectory(new DirectoryInfo(LuceneCommon.IndexOutDoorDirectory)))
            {
                var searcher = new IndexSearcher(directory, readOnly: true);

                var query = ParseQuery(queryTerm, searchFilter);

                //var termQuery = new TermQuery(new Term(OutDoorIndexFields.ID, "1"));

                var results = searcher.Search(query, filter: null, n: numRecords, sort: new Sort(sortFieldArry.ToArray()));

                var keys = results.ScoreDocs.Skip(searchFilter.Skip)
                    .Select(c => GetMediaItem(searcher.Doc(c.Doc)))
                    .ToList();

                totalHits = results.TotalHits;

                searcher.Dispose();

                return keys;
            }
        }
Esempio n. 6
0
        private static Query ParseQuery(QueryTerm queryTerm, SearchFilter searchFilter)
        {
            var combineQuery = new BooleanQuery();

            #region 关键字查询构建
            if (!String.IsNullOrWhiteSpace(searchFilter.SearchTerm))
            {
                var fields = new[] {
                    OutDoorIndexFields.Title,
                    OutDoorIndexFields.Description,
                    OutDoorIndexFields.AreaCate,
                    OutDoorIndexFields.IndustryCate,
                    OutDoorIndexFields.CrowdCate,
                    OutDoorIndexFields.PurposeCate,
                    OutDoorIndexFields.MediaCateName,
                    OutDoorIndexFields.CityCateName,
                    OutDoorIndexFields.FormatName,
                    OutDoorIndexFields.PeriodName,
                    OutDoorIndexFields.OwnerName
                };
                var analyzer = new PanGuAnalyzer();
                //var analyzer = new StandardAnalyzer(LuceneCommon.LuceneVersion);
                var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields, analyzer);

                var query = queryParser.Parse(searchFilter.SearchTerm);

                //conjuction 一起选择
                var conjuctionQuery = new BooleanQuery();
                conjuctionQuery.Boost = 2.0f;

                //disjunction 分离
                var disjunctionQuery = new BooleanQuery();
                disjunctionQuery.Boost = 0.1f;

                //wildCard 通配符
                var wildCardQuery = new BooleanQuery();
                wildCardQuery.Boost = 0.5f;

                var escapedSearchTerm = Escape(searchFilter.SearchTerm);

                var exactIdQuery = new TermQuery(new Term(OutDoorIndexFields.Title, escapedSearchTerm));

                exactIdQuery.Boost = 2.5f;

                var wildCardIdQuery = new WildcardQuery(new Term(OutDoorIndexFields.Title, "*" + escapedSearchTerm + "*"));

                foreach (var term in GetSearchTerms(searchFilter.SearchTerm))
                {
                    var termQuery = queryParser.Parse(term);
                    conjuctionQuery.Add(termQuery, Occur.MUST);
                    disjunctionQuery.Add(termQuery, Occur.SHOULD);

                    foreach (var field in fields)
                    {
                        var wildCardTermQuery = new WildcardQuery(new Term(field, term + "*"));
                        wildCardTermQuery.Boost = 0.7f;
                        wildCardQuery.Add(wildCardTermQuery, Occur.SHOULD);
                    }
                }
                //关键查询
                var keywordsQuery =
                    conjuctionQuery.Combine(new Query[] { exactIdQuery, wildCardIdQuery, conjuctionQuery, disjunctionQuery, wildCardQuery });

                combineQuery.Add(keywordsQuery, Occur.MUST);
            }
            #endregion

            #region 指定媒体ID查询
            if (queryTerm.MediaID != 0)
            {
                var mediaIdQuery = new TermQuery(new Term(OutDoorIndexFields.ID, queryTerm.MediaID.ToString()));
                combineQuery.Add(mediaIdQuery, Occur.MUST);
            }
            #endregion


            #region 用户状态
            var memberStatusQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.MemberStatus, (int)MemberStatus.CompanyAuth, 99, true, true);
            combineQuery.Add(memberStatusQuery, Occur.MUST);
            #endregion

            #region 审核状态查询构建
            var verifyStatus = NumericRangeQuery.NewIntRange(OutDoorIndexFields.Status, (int)OutDoorStatus.ShowOnline, 99, true, true);
            combineQuery.Add(verifyStatus, Occur.MUST);
            #endregion

            #region 指定用户ID查询
            if (queryTerm.MemberID != 0)
            {
                var memberIdQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.MemberID, queryTerm.MemberID, queryTerm.MemberID, true, true);
                combineQuery.Add(memberIdQuery, Occur.MUST);
            }
            #endregion

            #region 城市查询
            if (queryTerm.City != 0)
            {
                var cityQuery = NumericRangeQuery.NewLongRange(OutDoorIndexFields.CityCateCode, queryTerm.CityCateCode, queryTerm.CityMaxCode, true, true);
                combineQuery.Add(cityQuery, Occur.MUST);
            }
            #endregion

            #region 认证状态
            if (queryTerm.AuthStatus != 0)
            {
                var authStatusQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.AuthStatus, queryTerm.AuthStatus, queryTerm.AuthStatus, true, true);
                combineQuery.Add(authStatusQuery, Occur.MUST);
            }
            #endregion

            #region 经纬度搜索
            if (queryTerm.MinX != 0)
            {
                var latQuery = NumericRangeQuery.NewDoubleRange(OutDoorIndexFields.Lat, queryTerm.MinX, queryTerm.MaxX, true, true);
                var lngQuery = NumericRangeQuery.NewDoubleRange(OutDoorIndexFields.Lng, queryTerm.MinY, queryTerm.MaxY, true, true);
                combineQuery.Add(latQuery, Occur.MUST);
                combineQuery.Add(lngQuery, Occur.MUST);
            }
            #endregion

            #region 媒体类别查询
            if (queryTerm.MediaCode != 0)
            {
                var mediaCodeQuery = NumericRangeQuery.NewLongRange(OutDoorIndexFields.MediaCateCode,
                                                                    queryTerm.MediaCateCode, queryTerm.MediaMaxCode, true, true);
                combineQuery.Add(mediaCodeQuery, Occur.MUST);
            }
            #endregion

            #region 媒体表现形式查询
            if (queryTerm.FormatCode != 0)
            {
                var FormatCodeCodeQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.FormatCode,
                                                                        queryTerm.FormatCode, queryTerm.FormatCode, true, true);
                combineQuery.Add(FormatCodeCodeQuery, Occur.MUST);
            }
            #endregion

            #region 媒体所有权查询
            //if (queryTerm.OwnerCode != 0)
            //{
            //    var OwnerCodeCodeQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.OwnerCode,
            //        queryTerm.OwnerCode, queryTerm.OwnerCode, true, true);
            //    combineQuery.Add(OwnerCodeCodeQuery, Occur.MUST);
            //}
            #endregion

            #region 媒体购买周期查询
            if (queryTerm.PeriodCode != 0)
            {
                var PeriodCodeCodeQuery = NumericRangeQuery.NewIntRange(OutDoorIndexFields.PeriodCode,
                                                                        queryTerm.PeriodCode, queryTerm.PeriodCode, true, true);
                combineQuery.Add(PeriodCodeCodeQuery, Occur.MUST);
            }
            #endregion

            #region 媒体价格区间查询
            if (queryTerm.Price != 0)
            {
                var rangeValue = EnumHelper.GetPriceValue(queryTerm.Price);
                var PriceQuery = NumericRangeQuery.NewDoubleRange(OutDoorIndexFields.Price,
                                                                  Convert.ToDouble(rangeValue.Min), Convert.ToDouble(rangeValue.Max), true, true);
                combineQuery.Add(PriceQuery, Occur.MUST);
            }
            #endregion

            #region 媒体档期查询
            if (queryTerm.DeadLine != 0)
            {
                var minValue      = (DateTime.Now.AddYears(-10)).Ticks;
                var maxValue      = (new DateTime(DateTime.Now.Year, queryTerm.DeadLine, 1)).Ticks;
                var DeadLineQuery = NumericRangeQuery.NewLongRange(OutDoorIndexFields.DeadLine,
                                                                   Convert.ToInt64(minValue), Convert.ToInt64(maxValue), true, true);
                combineQuery.Add(DeadLineQuery, Occur.MUST);
            }
            #endregion

            #region 媒体档期时间查询
            if (!string.IsNullOrEmpty(queryTerm.Dq))
            {
                var minValue = (DateTime.Now.AddYears(-10)).Ticks;
                var maxValue = Convert.ToDateTime(queryTerm.Dq).Ticks;
                var DqQuery  = NumericRangeQuery.NewLongRange(OutDoorIndexFields.DeadLine,
                                                              Convert.ToInt64(minValue), Convert.ToInt64(maxValue), true, true);
                combineQuery.Add(DqQuery, Occur.MUST);
            }
            #endregion

            return(combineQuery);
        }