示例#1
0
        private IndexSearcher Searcher(bool allowRefreshSearcher)
        {
            if (this._searcher != null && (!_modified || !allowRefreshSearcher))
            {
                return(this._searcher);
            }

            try
            {
                IndexReader newReader = this._reader == null?DirectoryReader.open(this._writer) : DirectoryReader.openIfChanged(( DirectoryReader )this._reader);

                if (newReader == null)
                {
                    return(this._searcher);
                }
                LuceneUtil.Close(_reader);
                this._reader = newReader;
                LuceneUtil.Close(_searcher);
                _searcher = new IndexSearcher(_reader);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
            finally
            {
                if (allowRefreshSearcher)
                {
                    this._modified = false;
                }
            }
            return(this._searcher);
        }
示例#2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void switchToVerification() throws java.io.IOException
        private void SwitchToVerification()
        {
            _indexPopulator.close(true);
            assertEquals(InternalIndexState.ONLINE, _provider.getInitialState(_index));
            _reader   = DirectoryReader.open(_directory);
            _searcher = new IndexSearcher(_reader);
        }
示例#3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: IndexReference createIndexReference(IndexIdentifier identifier) throws java.io.IOException, org.neo4j.internal.kernel.api.exceptions.explicitindex.ExplicitIndexNotFoundKernelException
        internal override IndexReference CreateIndexReference(IndexIdentifier identifier)
        {
            IndexWriter   writer        = NewIndexWriter(identifier);
            IndexReader   reader        = DirectoryReader.open(writer, true);
            IndexSearcher indexSearcher = NewIndexSearcher(identifier, reader);

            return(new WritableIndexReference(identifier, indexSearcher, writer));
        }
示例#4
0
        public virtual void TestNegativeQueryBoost()
        {
            Query q = new TermQuery(new Term("foo", "bar"));

            q.Boost = -42f;
            Assert.AreEqual(-42f, q.Boost, 0.0f);

            Directory directory = newDirectory();

            try
            {
                Analyzer          analyzer = new MockAnalyzer(random());
                IndexWriterConfig conf     = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);

                IndexWriter writer = new IndexWriter(directory, conf);
                try
                {
                    Document d = new Document();
                    d.add(newTextField("foo", "bar", Field.Store.YES));
                    writer.addDocument(d);
                }
                finally
                {
                    writer.close();
                }

                IndexReader reader = DirectoryReader.open(directory);
                try
                {
                    IndexSearcher searcher = newSearcher(reader);

                    ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
                    Assert.AreEqual(1, hits.Length);
                    Assert.IsTrue("score is not negative: " + hits[0].score, hits[0].score < 0);

                    Explanation explain = searcher.explain(q, hits[0].doc);
                    Assert.AreEqual("score doesn't match explanation", hits[0].score, explain.Value, 0.001f);
                    Assert.IsTrue("explain doesn't think doc is a match", explain.Match);
                }
                finally
                {
                    reader.close();
                }
            }
            finally
            {
                directory.close();
            }
        }
示例#5
0
        private void DoTestSearch(Random random, PrintWriter @out, bool useCompoundFile)
        {
            Directory         directory = newDirectory();
            Analyzer          analyzer  = new MockAnalyzer(random);
            IndexWriterConfig conf      = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
            MergePolicy       mp        = conf.MergePolicy;

            mp.NoCFSRatio = useCompoundFile ? 1.0 : 0.0;
            IndexWriter writer = new IndexWriter(directory, conf);

            string[] docs = new string[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" };
            for (int j = 0; j < docs.Length; j++)
            {
                Document d = new Document();
                d.add(newTextField("contents", docs[j], Field.Store.YES));
                d.add(newStringField("id", "" + j, Field.Store.NO));
                writer.addDocument(d);
            }
            writer.close();

            IndexReader   reader   = DirectoryReader.open(directory);
            IndexSearcher searcher = newSearcher(reader);

            ScoreDoc[] hits = null;

            Sort sort = new Sort(SortField.FIELD_SCORE, new SortField("id", SortField.Type.INT));

            foreach (Query query in BuildQueries())
            {
                @out.println("Query: " + query.ToString("contents"));
                if (VERBOSE)
                {
                    Console.WriteLine("TEST: query=" + query);
                }

                hits = searcher.search(query, null, 1000, sort).scoreDocs;

                @out.println(hits.Length + " total results");
                for (int i = 0; i < hits.Length && i < 10; i++)
                {
                    Document d = searcher.doc(hits[i].doc);
                    @out.println(i + " " + hits[i].score + " " + d.get("contents"));
                }
            }
            reader.close();
            directory.close();
        }
示例#6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void main(String[] args) throws Exception
        public static void Main(string[] args)
        {
            string field    = null;
            int    numTerms = DEFAULT_NUMTERMS;

            if (args.Length == 0 || args.Length > 4)
            {
                usage();
                Environment.Exit(1);
            }

            Directory dir = FSDirectory.open(new File(args[0]));

            IComparer <TermStats> comparator = new DocFreqComparator();

            for (int i = 1; i < args.Length; i++)
            {
                if (args[i].Equals("-t"))
                {
                    comparator = new TotalTermFreqComparator();
                }
                else
                {
                    try
                    {
                        numTerms = Convert.ToInt32(args[i]);
                    }
                    catch (NumberFormatException)
                    {
                        field = args[i];
                    }
                }
            }

            IndexReader reader = DirectoryReader.open(dir);

            TermStats[] terms = getHighFreqTerms(reader, numTerms, field, comparator);

            for (int i = 0; i < terms.Length; i++)
            {
                System.out.printf(Locale.ROOT, "%s:%s \t totalTF = %,d \t docFreq = %,d \n", terms[i].field, terms[i].termtext.utf8ToString(), terms[i].totalTermFreq, terms[i].docFreq);
            }
            reader.close();
        }
示例#7
0
        // LUCENE-1448
        // TODO: instead of testing it this way, we can test
        // with BaseTokenStreamTestCase now...
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void testEndOffsetPositionWithTeeSinkTokenFilter() throws Exception
        public virtual void testEndOffsetPositionWithTeeSinkTokenFilter()
        {
            Directory          dir         = newDirectory();
            Analyzer           analyzer    = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
            IndexWriter        w           = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
            Document           doc         = new Document();
            TokenStream        tokenStream = analyzer.tokenStream("field", "abcd   ");
            TeeSinkTokenFilter tee         = new TeeSinkTokenFilter(tokenStream);
            TokenStream        sink        = tee.newSinkTokenStream();
            FieldType          ft          = new FieldType(TextField.TYPE_NOT_STORED);

            ft.StoreTermVectors         = true;
            ft.StoreTermVectorOffsets   = true;
            ft.StoreTermVectorPositions = true;
            Field f1 = new Field("field", tee, ft);
            Field f2 = new Field("field", sink, ft);

            doc.add(f1);
            doc.add(f2);
            w.addDocument(doc);
            w.close();

            IndexReader r      = DirectoryReader.open(dir);
            Terms       vector = r.getTermVectors(0).terms("field");

            assertEquals(1, vector.size());
            TermsEnum termsEnum = vector.iterator(null);

            termsEnum.next();
            assertEquals(2, termsEnum.totalTermFreq());
            DocsAndPositionsEnum positions = termsEnum.docsAndPositions(null, null);

            assertTrue(positions.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
            assertEquals(2, positions.freq());
            positions.nextPosition();
            assertEquals(0, positions.startOffset());
            assertEquals(4, positions.endOffset());
            positions.nextPosition();
            assertEquals(8, positions.startOffset());
            assertEquals(12, positions.endOffset());
            assertEquals(DocIdSetIterator.NO_MORE_DOCS, positions.nextDoc());
            r.close();
            dir.close();
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void testLimitTokenCountIndexWriter() throws java.io.IOException
        public virtual void testLimitTokenCountIndexWriter()
        {
            foreach (bool consumeAll in new bool[] { true, false })
            {
                Directory    dir   = newDirectory();
                int          limit = TestUtil.Next(random(), 50, 101000);
                MockAnalyzer mock  = new MockAnalyzer(random());

                // if we are consuming all tokens, we can use the checks,
                // otherwise we can't
                mock.EnableChecks = consumeAll;
                Analyzer a = new LimitTokenCountAnalyzer(mock, limit, consumeAll);

                IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, a));

                Document      doc = new Document();
                StringBuilder b   = new StringBuilder();
                for (int i = 1; i < limit; i++)
                {
                    b.Append(" a");
                }
                b.Append(" x");
                b.Append(" z");
                doc.add(newTextField("field", b.ToString(), Field.Store.NO));
                writer.addDocument(doc);
                writer.close();

                IndexReader reader = DirectoryReader.open(dir);
                Term        t      = new Term("field", "x");
                assertEquals(1, reader.docFreq(t));
                t = new Term("field", "z");
                assertEquals(0, reader.docFreq(t));
                reader.close();
                dir.close();
            }
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void split() throws java.io.IOException
        public virtual void Split()
        {
            bool            success = false;
            DirectoryReader reader  = DirectoryReader.open(input);

            try
            {
                // pass an individual config in here since one config can not be reused!
                createIndex(config1, dir1, reader, docsInFirstIndex, false);
                createIndex(config2, dir2, reader, docsInFirstIndex, true);
                success = true;
            }
            finally
            {
                if (success)
                {
                    IOUtils.close(reader);
                }
                else
                {
                    IOUtils.closeWhileHandlingException(reader);
                }
            }
        }
示例#10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public static void Main(string[] args)
        {
            if (args.Length < 5)
            {
                Console.Error.WriteLine("Usage: MultiPassIndexSplitter -out <outputDir> -num <numParts> [-seq] <inputIndex1> [<inputIndex2 ...]");
                Console.Error.WriteLine("\tinputIndex\tpath to input index, multiple values are ok");
                Console.Error.WriteLine("\t-out ouputDir\tpath to output directory to contain partial indexes");
                Console.Error.WriteLine("\t-num numParts\tnumber of parts to produce");
                Console.Error.WriteLine("\t-seq\tsequential docid-range split (default is round-robin)");
                Environment.Exit(-1);
            }
            List <IndexReader> indexes = new List <IndexReader>();
            string             outDir  = null;
            int  numParts = -1;
            bool seq      = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].Equals("-out"))
                {
                    outDir = args[++i];
                }
                else if (args[i].Equals("-num"))
                {
                    numParts = Convert.ToInt32(args[++i]);
                }
                else if (args[i].Equals("-seq"))
                {
                    seq = true;
                }
                else
                {
                    File file = new File(args[i]);
                    if (!file.exists() || !file.Directory)
                    {
                        Console.Error.WriteLine("Invalid input path - skipping: " + file);
                        continue;
                    }
                    Directory dir = FSDirectory.open(new File(args[i]));
                    try
                    {
                        if (!DirectoryReader.indexExists(dir))
                        {
                            Console.Error.WriteLine("Invalid input index - skipping: " + file);
                            continue;
                        }
                    }
                    catch (Exception)
                    {
                        Console.Error.WriteLine("Invalid input index - skipping: " + file);
                        continue;
                    }
                    indexes.Add(DirectoryReader.open(dir));
                }
            }
            if (outDir == null)
            {
                throw new Exception("Required argument missing: -out outputDir");
            }
            if (numParts < 2)
            {
                throw new Exception("Invalid value of required argument: -num numParts");
            }
            if (indexes.Count == 0)
            {
                throw new Exception("No input indexes to process");
            }
            File @out = new File(outDir);

            if ([email protected]())
            {
                throw new Exception("Can't create output directory: " + @out);
            }
            Directory[] dirs = new Directory[numParts];
            for (int i = 0; i < numParts; i++)
            {
                dirs[i] = FSDirectory.open(new File(@out, "part-" + i));
            }
            MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
            IndexReader            input;

            if (indexes.Count == 1)
            {
                input = indexes[0];
            }
            else
            {
                input = new MultiReader(indexes.ToArray());
            }
            splitter.Split(Version.LUCENE_CURRENT, input, dirs, seq);
        }
示例#11
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void getTermInfo(org.apache.lucene.store.Directory dir, org.apache.lucene.index.Term term) throws Exception
        public static void getTermInfo(Directory dir, Term term)
        {
            IndexReader reader = DirectoryReader.open(dir);

            System.out.printf(Locale.ROOT, "%s:%s \t totalTF = %,d \t doc freq = %,d \n", term.field(), term.text(), reader.totalTermFreq(term), reader.docFreq(term));
        }
示例#12
0
        static void Main()
        {
            // default AzureDirectory stores cache in local temp folder
            CloudStorageAccount cloudStorageAccount;

            CloudStorageAccount.TryParse(CloudConfigurationManager.GetSetting("blobStorage"), out cloudStorageAccount);
            //AzureDirectory azureDirectory = new AzureDirectory(cloudStorageAccount, "TestTest", new RAMDirectory());
            //AzureDirectory azureDirectory = new AzureDirectory(cloudStorageAccount, "TestTest", FSDirectory.Open(@"c:\test"));
            var azureDirectory = new AzureDirectory(cloudStorageAccount, "TestTest" /* default is FSDirectory.Open(@"%temp%/AzureDirectory/TestTest"); */);

            IndexWriter indexWriter = null;

            while (indexWriter == null)
            {
                try
                {
                    var config = new IndexWriterConfig(org.apache.lucene.util.Version.LUCENE_CURRENT, new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_CURRENT));
                    indexWriter = new IndexWriter(azureDirectory, config);
                }
                catch (LockObtainFailedException)
                {
                    Console.WriteLine("Lock is taken, waiting for timeout...");
                    Thread.Sleep(1000);
                }
            }
            Console.WriteLine("IndexWriter lock obtained, this process has exclusive write access to index");
            //indexWriter.setRAMBufferSizeMB(10.0);
            //indexWriter.SetUseCompoundFile(false);
            //indexWriter.SetMaxMergeDocs(10000);
            //indexWriter.SetMergeFactor(100);

            for (int iDoc = 0; iDoc < 10000; iDoc++)
            {
                if (iDoc % 10 == 0)
                {
                    Console.WriteLine(iDoc);
                }
                var doc = new Document();
                doc.add(new TextField("id", DateTime.Now.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture), Field.Store.YES));
                doc.add(new TextField("Title", GeneratePhrase(10), Field.Store.YES));
                doc.add(new TextField("Body", GeneratePhrase(40), Field.Store.YES));
                indexWriter.addDocument(doc);
            }
            Console.WriteLine("Total docs is {0}", indexWriter.numDocs());

            Console.Write("Flushing and disposing writer...");
            // Potentially Expensive: this ensures that all writes are commited to blob storage
            indexWriter.commit();
            indexWriter.close();

            Console.WriteLine("done");
            Console.WriteLine("Hit Key to search again");
            Console.ReadKey();

            IndexSearcher searcher;

            using (new AutoStopWatch("Creating searcher"))
            {
                searcher = new IndexSearcher(DirectoryReader.open(azureDirectory));
            }
            SearchForPhrase(searcher, "dog");
            SearchForPhrase(searcher, Random.Next(32768).ToString(CultureInfo.InvariantCulture));
            SearchForPhrase(searcher, Random.Next(32768).ToString(CultureInfo.InvariantCulture));
            Console.WriteLine("Hit a key to dispose and exit");
            Console.ReadKey();
        }