Esempio n. 1
0
        public void TestEmptyChildFilter()
        {
            Directory dir = NewDirectory();
            IndexWriterConfig config = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
            config.SetMergePolicy(NoMergePolicy.NO_COMPOUND_FILES);
            // we don't want to merge - since we rely on certain segment setup
            IndexWriter w = new IndexWriter(dir, config);

            IList<Document> docs = new List<Document>();

            docs.Add(MakeJob("java", 2007));
            docs.Add(MakeJob("python", 2010));
            docs.Add(MakeResume("Lisa", "United Kingdom"));
            w.AddDocuments(docs);

            docs.Clear();
            docs.Add(MakeJob("ruby", 2005));
            docs.Add(MakeJob("java", 2006));
            docs.Add(MakeResume("Frank", "United States"));
            w.AddDocuments(docs);
            w.Commit();
            int num = AtLeast(10); // produce a segment that doesn't have a value in the docType field
            for (int i = 0; i < num; i++)
            {
                docs.Clear();
                docs.Add(MakeJob("java", 2007));
                w.AddDocuments(docs);
            }

            IndexReader r = DirectoryReader.Open(w, Random().NextBoolean());
            w.Dispose();
            assertTrue(r.Leaves.size() > 1);
            IndexSearcher s = new IndexSearcher(r);
            Filter parentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("docType", "resume"))));

            BooleanQuery childQuery = new BooleanQuery();
            childQuery.Add(new BooleanClause(new TermQuery(new Term("skill", "java")), BooleanClause.Occur.MUST));
            childQuery.Add(new BooleanClause(NumericRangeQuery.NewIntRange("year", 2006, 2011, true, true), BooleanClause.Occur.MUST));

            ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg);

            BooleanQuery fullQuery = new BooleanQuery();
            fullQuery.Add(new BooleanClause(childJoinQuery, BooleanClause.Occur.MUST));
            fullQuery.Add(new BooleanClause(new MatchAllDocsQuery(), BooleanClause.Occur.MUST));
            ToParentBlockJoinCollector c = new ToParentBlockJoinCollector(Sort.RELEVANCE, 1, true, true);
            s.Search(fullQuery, c);
            TopGroups<int> results = c.GetTopGroups(childJoinQuery, null, 0, 10, 0, true);
            assertFalse(float.IsNaN(results.MaxScore));
            assertEquals(1, results.TotalGroupedHitCount);
            assertEquals(1, results.Groups.Length);
            IGroupDocs<int> group = results.Groups[0];
            Document childDoc = s.Doc(group.ScoreDocs[0].Doc);
            assertEquals("java", childDoc.Get("skill"));
            assertNotNull(group.GroupValue);
            Document parentDoc = s.Doc(group.GroupValue);
            assertEquals("Lisa", parentDoc.Get("name"));

            r.Dispose();
            dir.Dispose();
        }
Esempio n. 2
0
        /// <summary>
        /// Adds the specified object to the given IndexWriter.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the object to add.
        /// </typeparam>
        /// <param name="writer">
        /// The IndexWriter to use.
        /// </param>
        /// <param name="objList">
        /// The object list to write.
        /// </param>
        /// <param name="settings">
        /// The MappingSettings to use when creating the Document to add to the index.
        /// </param>
        /// <param name="analyzer">
        /// The Analyzer to use.
        /// </param>
        public static void Add <T>(this IndexWriter writer, List <T> objList, MappingSettings settings, Analyzer analyzer)
        {
            if (null == writer)
            {
                throw new ArgumentNullException("writer");
            }
            else if (null == objList)
            {
                throw new ArgumentNullException("objList");
            }
            else if (null == settings)
            {
                throw new ArgumentNullException("settings");
            }
            else if (null == analyzer)
            {
                throw new ArgumentNullException("analyzer");
            }

            var docList = new List <Document>();

            foreach (var obj in objList)
            {
                docList.Add(obj.ToDocument <T>(settings));
            }

            writer.AddDocuments(docList, analyzer);
        }
Esempio n. 3
0
        public virtual void AddDocument(IEnumerable <IIndexableField> doc, Analyzer a)
        {
            if (r.Next(5) == 3)
            {
                // TODO: maybe, we should simply buffer up added docs
                // (but we need to clone them), and only when
                // getReader, commit, etc. are called, we do an
                // addDocuments?  Would be better testing.
                IndexWriter.AddDocuments(new IterableAnonymousInnerClassHelper <IIndexableField>(this, doc), a);
            }
            else
            {
                IndexWriter.AddDocument(doc, a);
            }

            MaybeCommit();
        }
 public override void SetUp()
 {
     Directory = NewDirectory();
     IndexWriterConfig config = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
     IndexWriter indexWriter = new IndexWriter(Directory, config);
     for (int i = 0; i < AMOUNT_OF_SEGMENTS; i++)
     {
         IList<Document> segmentDocs = CreateDocsForSegment(i);
         indexWriter.AddDocuments(segmentDocs);
         indexWriter.Commit();
     }
     IndexReader = DirectoryReader.Open(indexWriter, Random().NextBoolean());
     indexWriter.Dispose();
     IndexSearcher = new IndexSearcher(IndexReader);
     ParentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new WildcardQuery(new Term("parent", "*"))));
 }
Esempio n. 5
0
 protected internal virtual void AddDocuments(Term id, IEnumerable <IEnumerable <IndexableField> > docs)
 {
     Writer.AddDocuments(docs);
 }
Esempio n. 6
0
 /// <summary>
 /// Calls
 /// <see cref="IndexWriter.AddDocuments(IEnumerable{IEnumerable{IIndexableField}}, Analyzer)"/> and
 /// returns the generation that reflects this change.
 /// </summary>
 public virtual long AddDocuments(IEnumerable <IEnumerable <IIndexableField> > docs, Analyzer a)
 {
     writer.AddDocuments(docs, a);
     // Return gen as of when indexing finished:
     return(indexingGen.Get());
 }
Esempio n. 7
0
        internal static void IndexDocuments(string path, List<IndexOperation> batch)
        {
            var uniques = new Dictionary<string, Document>();

            var deletes = new List<Term>();
            var writes = new List<LDocument>();

            for (var i = batch.Count - 1; i >= 0; i--)
            {
                var op = batch[i];
                var doc = batch[i].Document;
                var schema = doc.Schema;
                var uniqueFields = doc.GetUniqueFields(schema);
                bool indexDoc = op.Type == IndexOperationType.Save;
                foreach (var field in uniqueFields)
                {
                    var sig = field.GetSignature(schema);
                    if (uniques.ContainsKey(sig))
                    {
                        indexDoc = false;
                        break;
                    }
                }
                if (indexDoc)
                {
                    foreach (var field in uniqueFields)
                    {
                        uniques.Add(field.GetSignature(schema), doc);
                        var t = new Term(field.Name, field.GetValue());
                        deletes.Add(t);
                    }
                    writes.Add(Convert(doc, schema));
                }
            }

            var wrtr = new IndexWriter(path, new StandardAnalyzer());
            wrtr.DeleteDocuments(deletes.ToArray());
            wrtr.AddDocuments(writes.ToArray());
            if (deletes.Count + writes.Count > 99)
            {
                wrtr.Optimize();
            }
            wrtr.Close();
        }
Esempio n. 8
0
 protected virtual void AddDocuments(Term id, IEnumerable <IEnumerable <IIndexableField> > docs)
 {
     m_writer.AddDocuments(docs);
 }