/// <summary>
        /// Calculates the result count.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="baseDocIdSet">The base doc id set.</param>
        /// <param name="facetGroup">The facet group.</param>
        /// <param name="filter">The filter.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        private int CalculateResultCount(IndexReader reader, DocIdSet baseDocIdSet, FacetGroup facetGroup, ISearchFilter filter, ISearchCriteria criteria)
        {
            var count = 0;

            var values = LuceneQueryHelper.GetFilterValues(filter);

            if (values == null)
            {
                return(0);
            }

            BooleanFilter ffilter = null;

            foreach (var f in criteria.CurrentFilters)
            {
                if (!f.Key.Equals(facetGroup.FieldName))
                {
                    if (ffilter == null)
                    {
                        ffilter = new BooleanFilter();
                    }

                    var q = LuceneQueryHelper.CreateQuery(criteria, f, Occur.SHOULD);
                    ffilter.Add(new FilterClause(q, Occur.MUST));
                }
            }

            foreach (var value in values)
            {
                var queryFilter = new BooleanFilter();

                var valueFilter = LuceneQueryHelper.CreateQueryForValue(Results.SearchCriteria, filter, value);

                if (valueFilter == null)
                {
                    continue;
                }

                queryFilter.Add(new FilterClause(valueFilter, Occur.MUST));
                if (ffilter != null)
                {
                    queryFilter.Add(new FilterClause(ffilter, Occur.MUST));
                }

                var filterArray = queryFilter.GetDocIdSet(reader);
                var newCount    = (int)CalculateFacetCount(baseDocIdSet, filterArray);
                if (newCount == 0)
                {
                    continue;
                }

                var newFacet = new Facet(facetGroup, value.Id, GetDescription(value, Results.SearchCriteria.Locale), newCount);
                facetGroup.Facets.Add(newFacet);
                count += newCount;
            }

            return(count);
        }
        public static Filter CreateQueryForValue(ISearchCriteria criteria, ISearchFilter filter, ISearchFilterValue value)
        {
            Filter q          = null;
            var    priceQuery = filter is PriceRangeFilter;

            if (value is RangeFilterValue && priceQuery)
            {
                q = LuceneQueryHelper.CreateQuery(
                    criteria, filter.Key, value as RangeFilterValue);
            }
            else if (value is CategoryFilterValue)
            {
                q = CreateQuery(filter.Key, value as CategoryFilterValue);
            }
            else
            {
                q = CreateQuery(filter.Key, value);
            }
            return(q);
        }
Beispiel #3
0
        /// <summary>
        ///     Builds the query.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public virtual object BuildQuery(ISearchCriteria criteria)
        {
            var queryBuilder = new QueryBuilder();
            var queryFilter  = new BooleanFilter();
            var query        = new BooleanQuery();

            queryBuilder.Query  = query;
            queryBuilder.Filter = queryFilter;

            if (criteria.CurrentFilters != null)
            {
                foreach (var filter in criteria.CurrentFilters)
                {
                    // Skip currencies that are not part of the filter
                    if (filter.GetType() == typeof(PriceRangeFilter)) // special filtering
                    {
                        var currency = (filter as PriceRangeFilter).Currency;
                        if (!currency.Equals(criteria.Currency, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                    }

                    var filterQuery = LuceneQueryHelper.CreateQuery(criteria, filter, Occur.SHOULD);

                    // now add other values that should also be counted?

                    if (filterQuery != null)
                    {
                        var clause = new FilterClause(filterQuery, Occur.MUST);
                        queryFilter.Add(clause);
                    }
                }
            }

            return(queryBuilder);
        }