Esempio n. 1
0
        public virtual void TestSetAllGroups()
        {
            Directory         dir = NewDirectory();
            RandomIndexWriter w   = new RandomIndexWriter(
                Random,
                dir,
                NewIndexWriterConfig(TEST_VERSION_CURRENT,
                                     new MockAnalyzer(Random)).SetMergePolicy(NewLogMergePolicy()));
            Document doc = new Document();

            doc.Add(NewField("group", "foo", StringField.TYPE_NOT_STORED));
            w.AddDocument(doc);

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

            w.Dispose();

            GroupingSearch gs = new GroupingSearch("group");

            gs.SetAllGroups(true);
            ITopGroups <object> groups = gs.Search(indexSearcher, null, new TermQuery(new Index.Term("group", "foo")), 0, 10);

            assertEquals(1, groups.TotalHitCount);
            //assertEquals(1, groups.totalGroupCount.intValue());
            assertEquals(1, groups.TotalGroupedHitCount);
            assertEquals(1, gs.GetAllMatchingGroups().Count);
            indexSearcher.IndexReader.Dispose();
            dir.Dispose();
        }
Esempio n. 2
0
 public TopGroups(ITopGroups <TGroupValue> oldTopGroups, int?totalGroupCount)
 {
     GroupSort            = oldTopGroups.GroupSort;
     WithinGroupSort      = oldTopGroups.WithinGroupSort;
     TotalHitCount        = oldTopGroups.TotalHitCount;
     TotalGroupedHitCount = oldTopGroups.TotalGroupedHitCount;
     Groups          = oldTopGroups.Groups;
     MaxScore        = oldTopGroups.MaxScore;
     TotalGroupCount = totalGroupCount;
 }
Esempio n. 3
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();
        }
Esempio n. 4
0
        private IEnumerable <KeyValuePair <string, int> > _GroupBy(int skip, int pageSize, string fieldName)
        {
            GroupingSearch groupingSearch = new GroupingSearch(fieldName);

            groupingSearch.SetGroupSort(Sort.RELEVANCE);
            groupingSearch.SetFillSortFields(false);
            groupingSearch.SetCachingInMB(40.0, true);
            groupingSearch.SetAllGroups(true);
            // Render groupsResult...
            try
            {
                var reader = DirectoryReader.Open(_directory);

                var searcher = new Lucene.Net.Search.IndexSearcher(reader);

                Sort groupSort   = Sort.RELEVANCE;
                int  groupOffset = 0;
                int  groupLimit  = 10000000;

                string rawQuery = _queryProvider.GetBooleanQuery().ToString();

                if (!rawQuery.Contains("isdeleted"))
                {
                    rawQuery += "+isdeleted:0";
                }
                var queryParser = new QueryParser(LuceneVersion.LUCENE_48, "isdeleted", analyzer);

                queryParser.AllowLeadingWildcard = _queryProvider.GetContainsWildCard();

                var query = queryParser.Parse(rawQuery);

                ITopGroups <object> result = groupingSearch.Search(searcher, query, groupOffset, groupLimit);

                if (result.Groups == null || result.Groups.Count() <= 0)
                {
                    return(new List <KeyValuePair <string, int> >());
                }

                var d = result.Groups.OrderByDescending(p => p.TotalHits).ToList();

                if (d.FirstOrDefault().GroupValue == null)
                {
                    d.RemoveAt(0);
                }

                _groupCount = d.Count;

                if (pageSize > d.Count)
                {
                    pageSize = d.Count;
                }
                d = d.Skip(skip).Take(pageSize).ToList();

                if (d.Count > 0)
                {
                    var rs = d.Select(p => new KeyValuePair <string, int>(((BytesRef)p.GroupValue)?.Utf8ToString(), p.TotalHits)).ToList();

                    return(rs);
                }
                else
                {
                    return(new List <KeyValuePair <string, int> >());
                }
            }

            catch
            {
                throw;
            }

            finally
            {
            }
        }