Exemple #1
0
        public virtual void  TestPrefixQuery()
        {
            RAMDirectory directory = new RAMDirectory();

            System.String[] categories = new System.String[] { "/Computers", "/Computers/Mac", "/Computers/Windows" };
            IndexWriter     writer     = new IndexWriter(directory, new WhitespaceAnalyzer(), true);

            for (int i = 0; i < categories.Length; i++)
            {
                Document doc = new Document();
                doc.Add(Field.Keyword("category", categories[i]));
                writer.AddDocument(doc);
            }
            writer.Close();

            PrefixQuery   query    = new PrefixQuery(new Term("category", "/Computers"));
            IndexSearcher searcher = new IndexSearcher(directory);
            Hits          hits     = searcher.Search(query);

            Assert.AreEqual(3, hits.Length(), "All documents in /Computers category and below");

            query = new PrefixQuery(new Term("category", "/Computers/Mac"));
            hits  = searcher.Search(query);
            Assert.AreEqual(1, hits.Length(), "One in /Computers/Mac");
        }
Exemple #2
0
        private void  InsertDoc(IndexWriter writer, System.String content)
        {
            Document doc = new Document();

            doc.Add(Field.Keyword("id", "id" + docCount));
            doc.Add(Field.UnStored("content", content));

            writer.AddDocument(doc);
            docCount++;
        }
Exemple #3
0
        private void  AddDocumentWithDifferentFields(IndexWriter writer)
        {
            Document doc = new Document();

            doc.Add(Field.Keyword("keyword2", "test1"));
            doc.Add(Field.Text("text2", "test1"));
            doc.Add(Field.UnIndexed("unindexed2", "test1"));
            doc.Add(Field.UnStored("unstored2", "test1"));
            writer.AddDocument(doc);
        }
        public virtual void  TestMethod()
        {
            RAMDirectory directory = new RAMDirectory();

            System.String[] categories = new System.String[] { "food", "foodanddrink", "foodanddrinkandgoodtimes", "food and drink" };

            Query rw1 = null;
            Query rw2 = null;

            try
            {
                IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
                for (int i = 0; i < categories.Length; i++)
                {
                    Document doc = new Document();
                    doc.Add(Field.Keyword("category", categories[i]));
                    writer.AddDocument(doc);
                }
                writer.Close();

                IndexReader reader = IndexReader.Open(directory);
                PrefixQuery query  = new PrefixQuery(new Term("category", "foo"));

                rw1 = query.Rewrite(reader);

                BooleanQuery bq = new BooleanQuery();
                bq.Add(query, true, false);

                rw2 = bq.Rewrite(reader);
            }
            catch (System.IO.IOException e)
            {
                Assert.Fail(e.Message);
            }

            BooleanQuery bq1 = null;

            if (rw1 is BooleanQuery)
            {
                bq1 = (BooleanQuery)rw1;
            }

            BooleanQuery bq2 = null;

            if (rw2 is BooleanQuery)
            {
                bq2 = (BooleanQuery)rw2;
            }
            else
            {
                Assert.Fail("Rewrite");
            }

            Assert.AreEqual(bq1.GetClauses().Length, bq2.GetClauses().Length, "Number of Clauses Mismatch");
        }
Exemple #5
0
        public virtual void  TestAfter()
        {
            // create an index
            RAMDirectory indexStore = new RAMDirectory();
            IndexWriter  writer     = new IndexWriter(indexStore, new SimpleAnalyzer(), true);

            long now = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            Document doc = new Document();

            // add time that is in the future
            doc.Add(Field.Keyword("datefield", DateField.TimeToString(now + 888888)));
            doc.Add(Field.Text("body", "Today is a very sunny day in New York City"));
            writer.AddDocument(doc);
            writer.Optimize();
            writer.Close();

            IndexSearcher searcher = new IndexSearcher(indexStore);

            // filter that should preserve matches
            DateFilter df1 = DateFilter.After("datefield", now);

            // filter that should discard matches
            DateFilter df2 = DateFilter.After("datefield", now + 999999);

            // search something that doesn't exist with DateFilter
            Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));

            // search for something that does exists
            Query query2 = new TermQuery(new Term("body", "sunny"));

            Hits result;

            // ensure that queries return expected results without DateFilter first
            result = searcher.Search(query1);
            Assert.AreEqual(0, result.Length());

            result = searcher.Search(query2);
            Assert.AreEqual(1, result.Length());


            // run queries with DateFilter
            result = searcher.Search(query1, df1);
            Assert.AreEqual(0, result.Length());

            result = searcher.Search(query1, df2);
            Assert.AreEqual(0, result.Length());

            result = searcher.Search(query2, df1);
            Assert.AreEqual(1, result.Length());

            result = searcher.Search(query2, df2);
            Assert.AreEqual(0, result.Length());
        }
Exemple #6
0
        private Document MakeDocumentWithFields()
        {
            Document doc = new Document();

            doc.Add(Field.Keyword("keyword", "test1"));
            doc.Add(Field.Keyword("keyword", "test2"));
            doc.Add(Field.Text("text", "test1"));
            doc.Add(Field.Text("text", "test2"));
            doc.Add(Field.UnIndexed("unindexed", "test1"));
            doc.Add(Field.UnIndexed("unindexed", "test2"));
            doc.Add(Field.UnStored("unstored", "test1"));
            doc.Add(Field.UnStored("unstored", "test2"));
            return(doc);
        }
Exemple #7
0
 static DocHelper()
 {
     textField1     = Field.Text(TEXT_FIELD_1_KEY, FIELD_1_TEXT, false);
     textField2     = Field.Text(TEXT_FIELD_2_KEY, FIELD_2_TEXT, true);
     keyField       = Field.Keyword(KEYWORD_FIELD_KEY, KEYWORD_TEXT);
     unIndField     = Field.UnIndexed(UNINDEXED_FIELD_KEY, UNINDEXED_FIELD_TEXT);
     unStoredField1 = Field.UnStored(UNSTORED_FIELD_1_KEY, UNSTORED_1_FIELD_TEXT, false);
     unStoredField2 = Field.UnStored(UNSTORED_FIELD_2_KEY, UNSTORED_2_FIELD_TEXT, true);
     {
         nameValues = new System.Collections.Hashtable();
         nameValues[TEXT_FIELD_1_KEY]     = FIELD_1_TEXT;
         nameValues[TEXT_FIELD_2_KEY]     = FIELD_2_TEXT;
         nameValues[KEYWORD_FIELD_KEY]    = KEYWORD_TEXT;
         nameValues[UNINDEXED_FIELD_KEY]  = UNINDEXED_FIELD_TEXT;
         nameValues[UNSTORED_FIELD_1_KEY] = UNSTORED_1_FIELD_TEXT;
         nameValues[UNSTORED_FIELD_2_KEY] = UNSTORED_2_FIELD_TEXT;
     }
 }
Exemple #8
0
        /// <summary>Makes a document for a File.
        /// <p>
        /// The document has three fields:
        /// <ul>
        /// <li><code>path</code>--containing the pathname of the file, as a stored,
        /// tokenized Field;
        /// <li><code>modified</code>--containing the last modified date of the file as
        /// a keyword Field as encoded by <a
        /// href="lucene.document.DateField.html">DateField</a>; and
        /// <li><code>contents</code>--containing the full contents of the file, as a
        /// Reader Field;
        /// </summary>
        public static Document Document(System.IO.FileInfo f)
        {
            // make a new, empty document
            Document doc = new Document();

            // Add the path of the file as a Field named "path".  Use a Text Field, so
            // that the index stores the path, and so that the path is searchable
            doc.Add(Field.Text("path", f.FullName));

            // Add the last modified date of the file a Field named "modified".  Use a
            // Keyword Field, so that it's searchable, but so that no attempt is made
            // to tokenize the Field into words.
            doc.Add(Field.Keyword("modified", DateField.TimeToString(((f.LastWriteTime.Ticks - 621355968000000000) / 10000))));

            // Add the contents of the file a Field named "contents".  Use a Text
            // Field, specifying a Reader, so that the text of the file is tokenized.
            // ?? why doesn't FileReader work here ??
            System.IO.FileStream   is_Renamed = new System.IO.FileStream(f.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.StreamReader reader     = new System.IO.StreamReader(new System.IO.StreamReader(is_Renamed, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(is_Renamed, System.Text.Encoding.Default).CurrentEncoding);
            doc.Add(Field.Text("contents", reader));

            // return the document
            return(doc);
        }
Exemple #9
0
        public virtual void  TestEmptyIndex()
        {
            // creating two directories for indices
            Directory indexStoreA = new RAMDirectory();
            Directory indexStoreB = new RAMDirectory();

            // creating a document to store
            Document lDoc = new Document();

            lDoc.Add(Field.Text("fulltext", "Once upon a time....."));
            lDoc.Add(Field.Keyword("id", "doc1"));
            lDoc.Add(Field.Keyword("handle", "1"));

            // creating a document to store
            Document lDoc2 = new Document();

            lDoc2.Add(Field.Text("fulltext", "in a galaxy far far away....."));
            lDoc2.Add(Field.Keyword("id", "doc2"));
            lDoc2.Add(Field.Keyword("handle", "1"));

            // creating a document to store
            Document lDoc3 = new Document();

            lDoc3.Add(Field.Text("fulltext", "a bizarre bug manifested itself...."));
            lDoc3.Add(Field.Keyword("id", "doc3"));
            lDoc3.Add(Field.Keyword("handle", "1"));

            // creating an index writer for the first index
            IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(), true);
            // creating an index writer for the second index, but writing nothing
            IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), true);

            //--------------------------------------------------------------------
            // scenario 1
            //--------------------------------------------------------------------

            // writing the documents to the first index
            writerA.AddDocument(lDoc);
            writerA.AddDocument(lDoc2);
            writerA.AddDocument(lDoc3);
            writerA.Optimize();
            writerA.Close();

            // closing the second index
            writerB.Close();

            // creating the query
            Query query = Lucene.Net.QueryParsers.QueryParser.Parse("handle:1", "fulltext", new StandardAnalyzer());

            // building the searchables
            Searcher[] searchers = new Searcher[2];
            // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
            searchers[0] = new IndexSearcher(indexStoreB);
            searchers[1] = new IndexSearcher(indexStoreA);
            // creating the multiSearcher
            Searcher mSearcher = GetMultiSearcherInstance(searchers);
            // performing the search
            Hits hits = mSearcher.Search(query);

            Assert.AreEqual(3, hits.Length());

            try
            {
                // iterating over the hit documents
                for (int i = 0; i < hits.Length(); i++)
                {
                    Document d = hits.Doc(i);
                }
            }
            catch (System.IndexOutOfRangeException e)
            {
                Assert.Fail("ArrayIndexOutOfBoundsException thrown: " + e.Message);
                System.Console.Error.WriteLine(e.Source);
            }
            finally
            {
                mSearcher.Close();
            }


            //--------------------------------------------------------------------
            // scenario 2
            //--------------------------------------------------------------------

            // adding one document to the empty index
            writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
            writerB.AddDocument(lDoc);
            writerB.Optimize();
            writerB.Close();

            // building the searchables
            Searcher[] searchers2 = new Searcher[2];
            // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
            searchers2[0] = new IndexSearcher(indexStoreB);
            searchers2[1] = new IndexSearcher(indexStoreA);
            // creating the mulitSearcher
            Searcher mSearcher2 = GetMultiSearcherInstance(searchers2);
            // performing the same search
            Hits hits2 = mSearcher2.Search(query);

            Assert.AreEqual(4, hits2.Length());

            try
            {
                // iterating over the hit documents
                for (int i = 0; i < hits2.Length(); i++)
                {
                    // no exception should happen at this point
                    Document d = hits2.Doc(i);
                }
            }
            catch (System.Exception e)
            {
                Assert.Fail("Exception thrown: " + e.Message);
                System.Console.Error.WriteLine(e.Source);
            }
            finally
            {
                mSearcher2.Close();
            }

            //--------------------------------------------------------------------
            // scenario 3
            //--------------------------------------------------------------------

            // deleting the document just added, this will cause a different exception to take place
            Term        term    = new Term("id", "doc1");
            IndexReader readerB = IndexReader.Open(indexStoreB);

            readerB.Delete(term);
            readerB.Close();

            // optimizing the index with the writer
            writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
            writerB.Optimize();
            writerB.Close();

            // building the searchables
            Searcher[] searchers3 = new Searcher[2];

            searchers3[0] = new IndexSearcher(indexStoreB);
            searchers3[1] = new IndexSearcher(indexStoreA);
            // creating the mulitSearcher
            Searcher mSearcher3 = GetMultiSearcherInstance(searchers3);
            // performing the same search
            Hits hits3 = mSearcher3.Search(query);

            Assert.AreEqual(3, hits3.Length());

            try
            {
                // iterating over the hit documents
                for (int i = 0; i < hits3.Length(); i++)
                {
                    Document d = hits3.Doc(i);
                }
            }
            catch (System.IO.IOException e)
            {
                Assert.Fail("IOException thrown: " + e.Message);
                System.Console.Error.WriteLine(e.Source);
            }
            finally
            {
                mSearcher3.Close();
            }
        }