Beispiel #1
0
        //Lookup by group string value
        private void LookupGroupsbyString(Directory directory)
        {
            Filter groupEndDocs = new CachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("groupEnd", "x"))));

            IndexReader indexReader = DirectoryReader.Open(directory);

            IndexSearcher indexSearcher = new IndexSearcher(indexReader);

            BlockGroupingCollector blockGroupingCollector = new BlockGroupingCollector(Sort.RELEVANCE, 10, true, groupEndDocs);

            indexSearcher.Search(new MatchAllDocsQuery(), null, blockGroupingCollector);

            var topGroups = blockGroupingCollector.GetTopGroups(Sort.RELEVANCE, 0, 0, 10, true);

            Console.WriteLine("Total group count: " + topGroups.TotalGroupCount);

            Console.WriteLine("Total group hit count: " + topGroups.TotalGroupedHitCount);

            foreach (var groupDocs in topGroups.Groups)
            {
                Console.WriteLine("Group: " + groupDocs.GroupValue);

                foreach (var scoreDoc in groupDocs.ScoreDocs)
                {
                    Document doc = indexSearcher.Doc(scoreDoc.Doc);

                    Console.WriteLine("Category: " + doc.GetField("Category").GetStringValue() + ", BookId: " + doc.GetField("BookId").GetStringValue());
                }
            }

            indexReader.Dispose();
        }
Beispiel #2
0
        public virtual void TestWithCachingFilter()
        {
            Directory         dir    = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, dir);
            IndexReader reader = writer.GetReader();

            writer.Dispose();

            IndexSearcher searcher = NewSearcher(reader);

            Query query = new TermQuery(new Term("none", "none"));

            QueryWrapperFilter   queryFilter   = new QueryWrapperFilter(query);
            CachingWrapperFilter cachingFilter = new CachingWrapperFilter(queryFilter);

            searcher.Search(query, cachingFilter, 1);

            CachingWrapperFilter cachingFilter2 = new CachingWrapperFilter(queryFilter);

            Filter[] chain = new Filter[2];
            chain[0] = cachingFilter;
            chain[1] = cachingFilter2;
            ChainedFilter cf = new ChainedFilter(chain);

            // throws java.lang.ClassCastException: org.apache.lucene.util.OpenBitSet cannot be cast to java.util.BitSet
            searcher.Search(new MatchAllDocsQuery(), cf, 1);
            reader.Dispose();
            dir.Dispose();
        }
        public Filter GetFilter(string tenantId, string[] types)
        {
            Filter visibilityFilter = GetVisibilityFilter(tenantId);
            Filter typeFilter       = new CachingWrapperFilter(new TypeFilter(types));

            return(new ChainedFilter(new Filter[] { visibilityFilter, typeFilter }, ChainedFilter.Logic.AND));
        }
Beispiel #4
0
        public virtual void TestWithCachingFilter()
        {
            Directory         dir    = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone);
            IndexReader       reader = writer.Reader;

            writer.Dispose();

            IndexSearcher searcher = NewSearcher(reader);

            Query query = new TermQuery(new Term("none", "none"));

            QueryWrapperFilter   queryFilter   = new QueryWrapperFilter(query);
            CachingWrapperFilter cachingFilter = new CachingWrapperFilter(queryFilter);

            searcher.Search(query, cachingFilter, 1);

            CachingWrapperFilter cachingFilter2 = new CachingWrapperFilter(queryFilter);

            Filter[] chain = new Filter[2];
            chain[0] = cachingFilter;
            chain[1] = cachingFilter2;
            ChainedFilter cf = new ChainedFilter(chain);

            // throws java.lang.ClassCastException: org.apache.lucene.util.OpenBitSet cannot be cast to java.util.BitSet
            searcher.Search(new MatchAllDocsQuery(), cf, 1);
            reader.Dispose();
            dir.Dispose();
        }
        public void TestWithCachingFilter()
        {
            Directory dir      = new RAMDirectory();
            Analyzer  analyzer = new WhitespaceAnalyzer();

            IndexWriter writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);

            writer.Close();

            Searcher searcher = new IndexSearcher(dir, true);

            Query query = new TermQuery(new Term("none", "none"));

            QueryWrapperFilter   queryFilter   = new QueryWrapperFilter(query);
            CachingWrapperFilter cachingFilter = new CachingWrapperFilter(queryFilter);

            searcher.Search(query, cachingFilter, 1);

            CachingWrapperFilter cachingFilter2 = new CachingWrapperFilter(queryFilter);

            Filter[] chain = new Filter[2];
            chain[0] = cachingFilter;
            chain[1] = cachingFilter2;
            ChainedFilter cf = new ChainedFilter(chain);

            // throws java.lang.ClassCastException: org.apache.lucene.util.OpenBitSet cannot be cast to java.util.BitSet
            searcher.Search(new MatchAllDocsQuery(), cf, 1);
        }
        public Filter GetFilter(string tenantId, string[] types, bool includePrerelease, bool includeUnlisted)
        {
            Filter visibilityFilter = GetVisibilityFilter(tenantId);
            Filter typeFilter       = new CachingWrapperFilter(new TypeFilter(types));
            Filter versionFilter    = GetVersionFilter(includePrerelease, includeUnlisted);

            return(new ChainedFilter(new Filter[] { visibilityFilter, versionFilter, typeFilter }, ChainedFilter.Logic.AND));
        }
        private IEnumerable <FacetMatch> FindMatchesInQuery(Query baseQueryWithoutFacetDrilldown, IList <FacetFieldInfo> allFacetFieldInfos, FacetFieldInfo facetFieldInfoToCalculateFor)
        {
            var calculations = 0;

            var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(CreateFacetedQuery(baseQueryWithoutFacetDrilldown, allFacetFieldInfos, facetFieldInfoToCalculateFor.FieldName)));
            var bitsQueryWithoutFacetDrilldown     = new OpenBitSetDISI(queryFilter.GetDocIdSet(IndexReader).Iterator(), IndexReader.MaxDoc);
            var baseQueryWithoutFacetDrilldownCopy = new OpenBitSetDISI(bitsQueryWithoutFacetDrilldown.Bits.Length)
            {
                Bits = new long[bitsQueryWithoutFacetDrilldown.Bits.Length]
            };

            var calculatedFacetCounts = new ResultCollection(facetFieldInfoToCalculateFor);

            foreach (var facetValueBitSet in GetOrCreateFacetBitSet(facetFieldInfoToCalculateFor.FieldName).FacetValueBitSetList)
            {
                var isSelected = calculatedFacetCounts.IsSelected(facetValueBitSet.Value);

                if (!isSelected && facetValueBitSet.Count < calculatedFacetCounts.MinCountForNonSelected) //Impossible to get a better result
                {
                    if (calculatedFacetCounts.HaveEnoughResults)
                    {
                        break;
                    }
                }

                bitsQueryWithoutFacetDrilldown.Bits.CopyTo(baseQueryWithoutFacetDrilldownCopy.Bits, 0);
                baseQueryWithoutFacetDrilldownCopy.NumWords = bitsQueryWithoutFacetDrilldown.NumWords;

                var bitset = facetValueBitSet.Bitset ?? CalculateOpenBitSetDisi(facetFieldInfoToCalculateFor.FieldName, facetValueBitSet.Value);
                baseQueryWithoutFacetDrilldownCopy.And(bitset);
                var count = baseQueryWithoutFacetDrilldownCopy.Cardinality();
                if (count == 0)
                {
                    continue;
                }
                var match = new FacetMatch
                {
                    Count          = count,
                    Value          = facetValueBitSet.Value,
                    FacetFieldName = facetFieldInfoToCalculateFor.FieldName
                };

                calculations++;
                if (isSelected)
                {
                    calculatedFacetCounts.AddToSelected(match);
                }
                else
                {
                    calculatedFacetCounts.AddToNonSelected(match);
                }
            }

            return(calculatedFacetCounts.GetList());
        }
Beispiel #8
0
        public virtual Filter GetFilter(XmlElement e)
        {
            UninterruptableMonitor.Enter(this);
            try
            {
                XmlElement childElement = DOMUtils.GetFirstChildOrFail(e);

                if (filterCache is null)
                {
                    filterCache = new LurchTable <object, Filter>(LurchTableOrder.Access, cacheSize);
                }

                // Test to see if child Element is a query or filter that needs to be
                // cached
                IQueryBuilder qb       = queryFactory.GetQueryBuilder(childElement.Name);
                object        cacheKey = null;
                Query         q        = null;
                Filter        f        = null;
                if (qb != null)
                {
                    q        = qb.GetQuery(childElement);
                    cacheKey = q;
                }
                else
                {
                    f        = filterFactory.GetFilter(childElement);
                    cacheKey = f;
                }
                if (filterCache.TryGetValue(cacheKey, out Filter cachedFilter) && cachedFilter != null)
                {
                    return(cachedFilter); // cache hit
                }

                //cache miss
                if (qb != null)
                {
                    cachedFilter = new QueryWrapperFilter(q);
                }
                else
                {
                    cachedFilter = new CachingWrapperFilter(f);
                }

                filterCache[cacheKey] = cachedFilter;
                return(cachedFilter);
            }
            finally
            {
                UninterruptableMonitor.Exit(this);
            }
        }
Beispiel #9
0
        //Lookup by group int value (Alternative Syntax)
        private void LookupGroupsByIntAlt(Directory directory)
        {
            Filter groupEndDocs = new CachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("groupEnd", "x"))));

            IndexReader indexReader = DirectoryReader.Open(directory);

            IndexSearcher indexSearcher = new IndexSearcher(indexReader);

            GroupingSearch groupingSearch = new GroupingSearch(groupEndDocs);

            groupingSearch.SetGroupSort(new Sort());

            groupingSearch.SetIncludeScores(true);

            Query query = NumericRangeQuery.NewInt32Range("Repetition", 1, 2, true, false);

            var groupsResult = groupingSearch.Search(indexSearcher, query, 0, 10);             //search(indexSearcher, query, groupOffset, groupLimit);

            indexReader.Dispose();
        }
Beispiel #10
0
        //Lookup by group string value (Alternative Syntax)
        private void LookupGroupsByStringAlt(Directory directory)
        {
            Filter groupEndDocs = new CachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("groupEnd", "x"))));

            IndexReader indexReader = DirectoryReader.Open(directory);

            IndexSearcher indexSearcher = new IndexSearcher(indexReader);

            GroupingSearch groupingSearch = new GroupingSearch(groupEndDocs);

            groupingSearch.SetGroupSort(new Sort());

            groupingSearch.SetIncludeScores(true);

            TermQuery query = new TermQuery(new Term("Category", "Cat 1"));

            var groupsResult = groupingSearch.Search(indexSearcher, query, 0, 10);             //search(indexSearcher, query, groupOffset, groupLimit);

            indexReader.Dispose();
        }
Beispiel #11
0
        public static Filter GetFilterForRole(string role)
        {
            Filter roleFilter = RoleFilterCache.Get(role);

            if (roleFilter == null)
            {
                roleFilter =
                    // the caching wrapper filter makes it cache the BitSet per segmentreader
                    new CachingWrapperFilter(
                        // builds the filter from the index and not from iterating
                        // stored doc content which is much faster
                        new QueryWrapperFilter(
                            new TermQuery(
                                new Term("security", role)
                                )
                            )
                        );
                // put in cache
                RoleFilterCache.Put(role, roleFilter);
            }
            return(roleFilter);
        }
Beispiel #12
0
        /// <summary>
        /// Creates facets.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="query">The query.</param>
        private void CreateFacets(IndexReader reader, Query query)
        {
            var groups          = new List <FacetGroup>();
            var baseQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query));
            var baseDocIdSet    = baseQueryFilter.GetDocIdSet(reader);

            #region Subcategory filters


            /*
             * var catalogCriteria = Results.SearchCriteria as CatalogItemSearchCriteria;
             * if (catalogCriteria != null && catalogCriteria.ChildCategoryFilters.Any())
             * {
             *  var group = new FacetGroup("Subcategory");
             *  var groupCount = 0;
             *
             *  foreach (var value in catalogCriteria.ChildCategoryFilters)
             *  {
             *      var q = LuceneQueryHelper.CreateQuery(catalogCriteria.OutlineField, value);
             *
             *      if (q == null) continue;
             *
             *      var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(q));
             *      var filterArray = queryFilter.GetDocIdSet(reader);
             *      var newCount = (int)CalculateFacetCount(baseDocIdSet, filterArray);
             *      if (newCount == 0) continue;
             *
             *      var newFacet = new Facet(group, value.Code, value.Name, newCount);
             *      group.Facets.Add(newFacet);
             *      groupCount += newCount;
             *  }
             *
             *  // Add only if items exist under
             *  if (groupCount > 0)
             *  {
             *      groups.Add(group);
             *  }
             * }
             * */

            #endregion

            if (Results.SearchCriteria.Filters != null && Results.SearchCriteria.Filters.Length > 0)
            {
                foreach (var filter in Results.SearchCriteria.Filters)
                {
                    if (!string.IsNullOrEmpty(Results.SearchCriteria.Currency) && filter is PriceRangeFilter)
                    {
                        var valCurrency = ((PriceRangeFilter)filter).Currency;
                        if (!valCurrency.Equals(Results.SearchCriteria.Currency, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                    }

                    var facetGroup = CalculateResultCount(reader, baseDocIdSet, filter, Results.SearchCriteria);
                    if (facetGroup != null)
                    {
                        groups.Add(facetGroup);
                    }
                }
            }

            Results.FacetGroups = groups.ToArray();
        }
        /// <summary>
        /// Creates facets.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="query">The query.</param>
        private void CreateFacets(IndexReader reader, Query query)
        {
            var groups = new List<FacetGroup>();
            var baseQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query));
            var baseDocIdSet = baseQueryFilter.GetDocIdSet(reader);

            #region Subcategory filters

           
            /* 
            var catalogCriteria = Results.SearchCriteria as CatalogItemSearchCriteria;
            if (catalogCriteria != null && catalogCriteria.ChildCategoryFilters.Any())
            {
                var group = new FacetGroup("Subcategory");
                var groupCount = 0;

                foreach (var value in catalogCriteria.ChildCategoryFilters)
                {
                    var q = LuceneQueryHelper.CreateQuery(catalogCriteria.OutlineField, value);

                    if (q == null) continue;

                    var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(q));
                    var filterArray = queryFilter.GetDocIdSet(reader);
                    var newCount = (int)CalculateFacetCount(baseDocIdSet, filterArray);
                    if (newCount == 0) continue;

                    var newFacet = new Facet(group, value.Code, value.Name, newCount);
                    group.Facets.Add(newFacet);
                    groupCount += newCount;
                }

                // Add only if items exist under
                if (groupCount > 0)
                {
                    groups.Add(group);
                }
            }
             * */

            #endregion

            if (Results.SearchCriteria.Filters != null && Results.SearchCriteria.Filters.Length > 0)
            {
                foreach (var filter in Results.SearchCriteria.Filters)
                {
                    var group = new FacetGroup(filter.Key);
                    var groupCount = 0;

                    if (!String.IsNullOrEmpty(Results.SearchCriteria.Currency) && filter is s.PriceRangeFilter)
                    {
                        var valCurrency = ((s.PriceRangeFilter) filter).Currency;
                        if (
                            !valCurrency.Equals(
                                Results.SearchCriteria.Currency, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                    }

                    groupCount += CalculateResultCount(reader, baseDocIdSet, group, filter,
                        Results.SearchCriteria);

                    // Add only if items exist under
                    if (groupCount > 0)
                    {
                        groups.Add(group);
                    }
                }
            }

            Results.FacetGroups = groups.ToArray();
        }
Beispiel #14
0
        public virtual void TestBasic()
        {
            string groupField = "author";

            FieldType customType = new FieldType();

            customType.IsStored = (true);

            Directory         dir = NewDirectory();
            RandomIndexWriter w   = new RandomIndexWriter(
                Random,
                dir,
                NewIndexWriterConfig(TEST_VERSION_CURRENT,
                                     new MockAnalyzer(Random)).SetMergePolicy(NewLogMergePolicy()));
            bool canUseIDV = !"Lucene3x".Equals(w.IndexWriter.Config.Codec.Name, StringComparison.Ordinal);

            JCG.List <Document> documents = new JCG.List <Document>();
            // 0
            Document doc = new Document();

            AddGroupField(doc, groupField, "author1", canUseIDV);
            doc.Add(new TextField("content", "random text", Field.Store.YES));
            doc.Add(new Field("id", "1", customType));
            documents.Add(doc);

            // 1
            doc = new Document();
            AddGroupField(doc, groupField, "author1", canUseIDV);
            doc.Add(new TextField("content", "some more random text", Field.Store.YES));
            doc.Add(new Field("id", "2", customType));
            documents.Add(doc);

            // 2
            doc = new Document();
            AddGroupField(doc, groupField, "author1", canUseIDV);
            doc.Add(new TextField("content", "some more random textual data", Field.Store.YES));
            doc.Add(new Field("id", "3", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));
            documents.Add(doc);
            w.AddDocuments(documents);
            documents.Clear();

            // 3
            doc = new Document();
            AddGroupField(doc, groupField, "author2", canUseIDV);
            doc.Add(new TextField("content", "some random text", Field.Store.YES));
            doc.Add(new Field("id", "4", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));
            w.AddDocument(doc);

            // 4
            doc = new Document();
            AddGroupField(doc, groupField, "author3", canUseIDV);
            doc.Add(new TextField("content", "some more random text", Field.Store.YES));
            doc.Add(new Field("id", "5", customType));
            documents.Add(doc);

            // 5
            doc = new Document();
            AddGroupField(doc, groupField, "author3", canUseIDV);
            doc.Add(new TextField("content", "random", Field.Store.YES));
            doc.Add(new Field("id", "6", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));
            documents.Add(doc);
            w.AddDocuments(documents);
            documents.Clear();

            // 6 -- no author field
            doc = new Document();
            doc.Add(new TextField("content", "random word stuck in alot of other text", Field.Store.YES));
            doc.Add(new Field("id", "6", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));

            w.AddDocument(doc);

            IndexSearcher indexSearcher = NewSearcher(w.GetReader());

            w.Dispose();

            Sort           groupSort      = Sort.RELEVANCE;
            GroupingSearch groupingSearch = CreateRandomGroupingSearch(groupField, groupSort, 5, canUseIDV);

            ITopGroups <object> groups = groupingSearch.Search(indexSearcher, (Filter)null, new TermQuery(new Index.Term("content", "random")), 0, 10);

            assertEquals(7, groups.TotalHitCount);
            assertEquals(7, groups.TotalGroupedHitCount);
            assertEquals(4, groups.Groups.Length);

            // relevance order: 5, 0, 3, 4, 1, 2, 6

            // the later a document is added the higher this docId
            // value
            IGroupDocs <object> group = groups.Groups[0];

            CompareGroupValue("author3", group);
            assertEquals(2, group.ScoreDocs.Length);
            assertEquals(5, group.ScoreDocs[0].Doc);
            assertEquals(4, group.ScoreDocs[1].Doc);
            assertTrue(group.ScoreDocs[0].Score > group.ScoreDocs[1].Score);

            group = groups.Groups[1];
            CompareGroupValue("author1", group);
            assertEquals(3, group.ScoreDocs.Length);
            assertEquals(0, group.ScoreDocs[0].Doc);
            assertEquals(1, group.ScoreDocs[1].Doc);
            assertEquals(2, group.ScoreDocs[2].Doc);
            assertTrue(group.ScoreDocs[0].Score > group.ScoreDocs[1].Score);
            assertTrue(group.ScoreDocs[1].Score > group.ScoreDocs[2].Score);

            group = groups.Groups[2];
            CompareGroupValue("author2", group);
            assertEquals(1, group.ScoreDocs.Length);
            assertEquals(3, group.ScoreDocs[0].Doc);

            group = groups.Groups[3];
            CompareGroupValue(null, group);
            assertEquals(1, group.ScoreDocs.Length);
            assertEquals(6, group.ScoreDocs[0].Doc);

            Filter lastDocInBlock = new CachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Index.Term("groupend", "x"))));

            groupingSearch = new GroupingSearch(lastDocInBlock);
            groups         = groupingSearch.Search(indexSearcher, null, new TermQuery(new Index.Term("content", "random")), 0, 10);

            assertEquals(7, groups.TotalHitCount);
            assertEquals(7, groups.TotalGroupedHitCount);
            assertEquals(4, groups.TotalGroupCount.GetValueOrDefault());
            assertEquals(4, groups.Groups.Length);

            indexSearcher.IndexReader.Dispose();
            dir.Dispose();
        }
        /// <summary>
        /// Creates facets.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="query">The query.</param>
        private void CreateFacets(IndexReader reader, Query query)
        {
            var groups = new List<FacetGroup>();

            if (this.Results.SearchCriteria.Filters != null && this.Results.SearchCriteria.Filters.Length > 0)
            {
                var baseQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query));

                var baseBitArray = baseQueryFilter.GetDocIdSet(reader);
                foreach (var filter in this.Results.SearchCriteria.Filters)
                {
                    var group = new FacetGroup(filter.Key);

                    var groupCount = 0;

                    if (!String.IsNullOrEmpty(this.Results.SearchCriteria.Currency) && filter is s.PriceRangeFilter)
                    {
                        var valCurrency = ((s.PriceRangeFilter)filter).Currency;
                        if (
                            !valCurrency.Equals(
                                this.Results.SearchCriteria.Currency, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                    }

                    groupCount += this.CalculateResultCount(
                        reader, baseBitArray, group, filter, this.Results.SearchCriteria.Currency);

                    // Add only if items exist under
                    if (groupCount > 0)
                    {
                        groups.Add(group);
                    }
                }
            }

            this.Results.FacetGroups = groups.ToArray();
        }
Beispiel #16
0
        public IEnumerable <IHit> Query(int pageIndex, int pageSize, out int totalCount, out IEnumerable <FacetGroup> facetedResults)
        {
            totalCount     = 0;
            facetedResults = null;

            if (searchPaths == null || searchPaths.Count <= 0)
            {
                searchPaths.AddRange(indexPaths.Values.Select(o => o.Path));
            }

            List <LuceneHit> results = new List <LuceneHit>();

            List <IndexSearcher> subSearchs = new List <IndexSearcher>();

            searchPaths.ForEach(o => subSearchs.Add(new IndexSearcher(FSDirectory.Open(o))));

            if (facetFields != null && facetFields.Count > 0)
            {
                var         facetGroups     = new List <FacetGroup>();
                var         mainQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query));
                MultiReader readers         = new MultiReader(subSearchs.Select(o => o.IndexReader).ToArray());

                foreach (var facetField in facetFields)
                {
                    FacetGroup fg = new FacetGroup();
                    fg.FieldName = facetFieldNameProvider.GetMapName(TypeName, facetField);
                    var items = new List <FacetItem>();

                    var allDistinctField = FieldCache_Fields.DEFAULT.GetStrings(readers, facetField).Distinct().ToArray();
                    int totalHits        = 0;

                    Parallel.ForEach(allDistinctField, fieldValue =>
                    {
                        //foreach (var fieldValue in allDistinctField)
                        //{
                        var facetQuery       = new TermQuery(new Term(facetField, fieldValue));
                        var facetQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(facetQuery));

                        var bs = new OpenBitSetDISI(facetQueryFilter.GetDocIdSet(readers).Iterator(), readers.MaxDoc);
                        bs.InPlaceAnd(mainQueryFilter.GetDocIdSet(readers).Iterator());
                        int count = (Int32)bs.Cardinality();

                        FacetItem item  = new FacetItem();
                        item.GroupValue = fieldValue;
                        item.Count      = count;

                        items.Add(item);
                        totalHits += count;
                    }
                                     );

                    fg.FacetItems = items.OrderByDescending(o => o.Count);
                    fg.TotalHits  = totalHits;

                    facetGroups.Add(fg);
                }

                facetedResults = facetGroups.OrderBy(o => o.FieldName);
            }
            ParallelMultiSearcher searcher = new ParallelMultiSearcher(subSearchs.ToArray());
            Sort sort = null;

            if (sortFields != null && sortFields.Count > 0)
            {
                sort = new Sort(sortFields.ToArray());
            }

            int maxDoc     = searcher.MaxDoc;
            int startIndex = 0;

            if (pageIndex >= 0 && pageSize > 0)
            {
                startIndex = pageIndex * pageSize;
                maxDoc     = pageSize * (pageIndex + 1);
            }
            var docs = sort == null?searcher.Search(query, null, maxDoc) : searcher.Search(query, null, maxDoc, sort);

            totalCount = docs.TotalHits;
            int endIndex = docs.TotalHits - startIndex;

            for (int i = startIndex; i < endIndex; i++)
            {
                LuceneHit h = new LuceneHit(TypeName, DocumentBuilder, searcher.Doc(docs.ScoreDocs[i].Doc));
                results.Add(h);
            }
            return(results);
        }
        /// <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="currency">The currency.</param>
        /// <returns></returns>
        private int CalculateResultCount(
            IndexReader reader, DocIdSet baseDocIdSet, FacetGroup facetGroup, ISearchFilter filter, string currency)
        {
            var count = 0;

            ISearchFilterValue[] values = null;
            var priceQuery = false;
            if (filter is s.AttributeFilter)
            {
                values = ((s.AttributeFilter)filter).Values;
            }
            else if (filter is s.RangeFilter)
            {
                values = ((s.RangeFilter)filter).Values;
            }
            else if (filter is s.PriceRangeFilter)
            {
                values = ((s.PriceRangeFilter)filter).Values;
                priceQuery = true;
            }

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

            foreach (var value in values)
            {
                Query q = null;
                if (priceQuery)
                {
                    q = LuceneQueryHelper.CreateQuery(
                        this.Results.SearchCriteria, filter.Key, value as s.RangeFilterValue);
                }
                else
                {
                    q = LuceneQueryHelper.CreateQuery(filter.Key, value);
                }

                if (q == null) continue;

                var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(q));
                var filterArray = queryFilter.GetDocIdSet(reader);
                var newCount = (int)this.CalculateFacetCount(baseDocIdSet, filterArray);
                if (newCount == 0) continue;

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

            return count;
        }
Beispiel #18
0
        public virtual void TestBasic()
        {

            string groupField = "author";

            FieldType customType = new FieldType();
            customType.Stored = (true);

            Directory dir = NewDirectory();
            RandomIndexWriter w = new RandomIndexWriter(
                Random(),
                dir,
                NewIndexWriterConfig(TEST_VERSION_CURRENT,
                    new MockAnalyzer(Random())).SetMergePolicy(NewLogMergePolicy()));
            bool canUseIDV = !"Lucene3x".Equals(w.w.Config.Codec.Name, StringComparison.Ordinal);
            List<Document> documents = new List<Document>();
            // 0
            Document doc = new Document();
            AddGroupField(doc, groupField, "author1", canUseIDV);
            doc.Add(new TextField("content", "random text", Field.Store.YES));
            doc.Add(new Field("id", "1", customType));
            documents.Add(doc);

            // 1
            doc = new Document();
            AddGroupField(doc, groupField, "author1", canUseIDV);
            doc.Add(new TextField("content", "some more random text", Field.Store.YES));
            doc.Add(new Field("id", "2", customType));
            documents.Add(doc);

            // 2
            doc = new Document();
            AddGroupField(doc, groupField, "author1", canUseIDV);
            doc.Add(new TextField("content", "some more random textual data", Field.Store.YES));
            doc.Add(new Field("id", "3", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));
            documents.Add(doc);
            w.AddDocuments(documents);
            documents.Clear();

            // 3
            doc = new Document();
            AddGroupField(doc, groupField, "author2", canUseIDV);
            doc.Add(new TextField("content", "some random text", Field.Store.YES));
            doc.Add(new Field("id", "4", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));
            w.AddDocument(doc);

            // 4
            doc = new Document();
            AddGroupField(doc, groupField, "author3", canUseIDV);
            doc.Add(new TextField("content", "some more random text", Field.Store.YES));
            doc.Add(new Field("id", "5", customType));
            documents.Add(doc);

            // 5
            doc = new Document();
            AddGroupField(doc, groupField, "author3", canUseIDV);
            doc.Add(new TextField("content", "random", Field.Store.YES));
            doc.Add(new Field("id", "6", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));
            documents.Add(doc);
            w.AddDocuments(documents);
            documents.Clear();

            // 6 -- no author field
            doc = new Document();
            doc.Add(new TextField("content", "random word stuck in alot of other text", Field.Store.YES));
            doc.Add(new Field("id", "6", customType));
            doc.Add(new StringField("groupend", "x", Field.Store.NO));

            w.AddDocument(doc);

            IndexSearcher indexSearcher = NewSearcher(w.Reader);
            w.Dispose();

            Sort groupSort = Sort.RELEVANCE;
            GroupingSearch groupingSearch = CreateRandomGroupingSearch(groupField, groupSort, 5, canUseIDV);

            ITopGroups<object> groups = groupingSearch.Search(indexSearcher, (Filter)null, new TermQuery(new Index.Term("content", "random")), 0, 10);

            assertEquals(7, groups.TotalHitCount);
            assertEquals(7, groups.TotalGroupedHitCount);
            assertEquals(4, groups.Groups.Length);

            // relevance order: 5, 0, 3, 4, 1, 2, 6

            // the later a document is added the higher this docId
            // value
            IGroupDocs<object> group = groups.Groups[0];
            CompareGroupValue("author3", group);
            assertEquals(2, group.ScoreDocs.Length);
            assertEquals(5, group.ScoreDocs[0].Doc);
            assertEquals(4, group.ScoreDocs[1].Doc);
            assertTrue(group.ScoreDocs[0].Score > group.ScoreDocs[1].Score);

            group = groups.Groups[1];
            CompareGroupValue("author1", group);
            assertEquals(3, group.ScoreDocs.Length);
            assertEquals(0, group.ScoreDocs[0].Doc);
            assertEquals(1, group.ScoreDocs[1].Doc);
            assertEquals(2, group.ScoreDocs[2].Doc);
            assertTrue(group.ScoreDocs[0].Score > group.ScoreDocs[1].Score);
            assertTrue(group.ScoreDocs[1].Score > group.ScoreDocs[2].Score);

            group = groups.Groups[2];
            CompareGroupValue("author2", group);
            assertEquals(1, group.ScoreDocs.Length);
            assertEquals(3, group.ScoreDocs[0].Doc);

            group = groups.Groups[3];
            CompareGroupValue(null, group);
            assertEquals(1, group.ScoreDocs.Length);
            assertEquals(6, group.ScoreDocs[0].Doc);

            Filter lastDocInBlock = new CachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Index.Term("groupend", "x"))));
            groupingSearch = new GroupingSearch(lastDocInBlock);
            groups = groupingSearch.Search(indexSearcher, null, new TermQuery(new Index.Term("content", "random")), 0, 10);

            assertEquals(7, groups.TotalHitCount);
            assertEquals(7, groups.TotalGroupedHitCount);
            assertEquals(4, groups.TotalGroupCount.GetValueOrDefault());
            assertEquals(4, groups.Groups.Length);

            indexSearcher.IndexReader.Dispose();
            dir.Dispose();
        }