Exemple #1
0
        private DateTime?GetMetaTimestamp()
        {
            var indexSearcher = new IndexSearcher(_indexWriter.getReader());

            // Find "meta" document.

            var query = new TermQuery(MetaDocIdTerm);
            var hits  = indexSearcher.search(query, 1);

            if (hits.totalHits == 0)
            {
                return(null);
            }

            // Get "meta" document timestamp.

            var metaDoc   = indexSearcher.doc(hits.scoreDocs[0].doc);
            var timestamp = metaDoc.get(SearchFieldName.Timestamp);

            if (string.IsNullOrEmpty(timestamp))
            {
                return(null);
            }

            return(new DateTime(long.Parse(timestamp)));
        }
Exemple #2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static org.neo4j.kernel.api.impl.index.partition.PartitionSearcher createPartitionSearcher(int maxDoc, int partition, int maxSize) throws java.io.IOException
        private static PartitionSearcher CreatePartitionSearcher(int maxDoc, int partition, int maxSize)
        {
            PartitionSearcher partitionSearcher = mock(typeof(PartitionSearcher));
            IndexSearcher     indexSearcher     = mock(typeof(IndexSearcher));
            IndexReader       indexReader       = mock(typeof(IndexReader));

            when(partitionSearcher.IndexSearcher).thenReturn(indexSearcher);
            when(indexSearcher.IndexReader).thenReturn(indexReader);
            when(indexReader.maxDoc()).thenReturn(maxDoc);

            when(indexSearcher.doc(0)).thenReturn(CreateDocument(UniqueDocValue(1, partition, maxSize)));
            when(indexSearcher.doc(1)).thenReturn(CreateDocument(UniqueDocValue(2, partition, maxSize)));
            when(indexSearcher.doc(2)).thenReturn(CreateDocument(UniqueDocValue(3, partition, maxSize)));

            return(partitionSearcher);
        }
        static public void query(Sentence[] sentences, string query, string indexPath = "luceneIndex")
        {
            Dictionary <string, Sentence> map = new Dictionary <string, Sentence>();

            foreach (Sentence s in sentences)
            {
                if (!map.ContainsKey(s.sentnece))
                {
                    map.Add(s.sentnece, s);
                }
            }

            java.io.File    indexDir       = new java.io.File(indexPath);
            FSDirectory     indexFSDir     = new SimpleFSDirectory(indexDir);
            IndexSearcher   searcher       = new IndexSearcher(IndexReader.open(indexFSDir));
            EnglishAnalyzer luceneAnalyzer = new EnglishAnalyzer(org.apache.lucene.util.Version.LUCENE_35);
            QueryParser     qp             = new QueryParser(org.apache.lucene.util.Version.LUCENE_35, "text", luceneAnalyzer);

            Query   q     = qp.parse(query);
            TopDocs tdocs = searcher.search(q, 99999999);

            ScoreDoc[] sdocs = tdocs.scoreDocs;
            for (int i = 0; i < sdocs.Length; i++)
            {
                ScoreDoc sd  = sdocs[i];
                Document res = searcher.doc(sd.doc);

                string docText = res.get("text");
                float  score   = sd.score;

                map[docText].lucene = score;
            }
            searcher.close();
        }
        private static Guid[] Search(MemberSearchQuery memberQuery)
        {
            var query  = _indexer.GetQuery(memberQuery);
            var filter = _indexer.GetFilter(memberQuery, null, null);

            var hits = _indexSearcher.search(query, filter, 10);

            return(Array.ConvertAll(hits.scoreDocs, scoreDoc =>
            {
                var document = _indexSearcher.doc(scoreDoc.doc);
                return new Guid(document.get(SearchFieldName.Id));
            }));
        }
Exemple #5
0
 internal static Document FindDocument(IndexType type, IndexSearcher searcher, long entityId)
 {
     try
     {
         TopDocs docs = searcher.search(type.IdTermQuery(entityId), 1);
         if (docs.scoreDocs.length > 0)
         {
             return(searcher.doc(docs.scoreDocs[0].doc));
         }
         return(null);
     }
     catch (IOException e)
     {
         throw new Exception(e);
     }
 }
Exemple #6
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();
        }
Exemple #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void assertIndexedValues(Hit... expectedHits) throws java.io.IOException
        private void AssertIndexedValues(params Hit[] expectedHits)
        {
            SwitchToVerification();

            foreach (Hit hit in expectedHits)
            {
                TopDocs hits = _searcher.search(LuceneDocumentStructure.NewSeekQuery(hit.Value), 10);
                assertEquals(hit.NodeIds.Length, hits.totalHits, "Unexpected number of index results from " + hit.Value);
                ISet <long> foundNodeIds = new HashSet <long>();
                for (int i = 0; i < hits.totalHits; i++)
                {
                    Document document = _searcher.doc(hits.scoreDocs[i].doc);
                    foundNodeIds.Add(parseLong(document.get("id")));
                }
                assertEquals(asSet(hit.NodeIds), foundNodeIds);
            }
        }
Exemple #8
0
        protected internal override Document FetchNextOrNull()
        {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            if (!_iterator.hasNext())
            {
                return(null);
            }
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
            _currentDoc = _iterator.next();
            try
            {
                return(_searcher.doc(_currentDoc.doc));
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
Exemple #9
0
        public virtual void TestDemo()
        {
            Analyzer analyzer = new MockAnalyzer(random());

            // Store the index in memory:
            Directory directory = newDirectory();
            // To store an index on disk, use this instead:
            // Directory directory = FSDirectory.open(new File("/tmp/testindex"));
            RandomIndexWriter iwriter  = new RandomIndexWriter(random(), directory, analyzer);
            Document          doc      = new Document();
            string            longTerm = "longtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongterm";
            string            text     = "this is the text to be indexed. " + longTerm;

            doc.add(newTextField("fieldname", text, Field.Store.YES));
            iwriter.addDocument(doc);
            iwriter.close();

            // Now search the index:
            IndexReader   ireader   = DirectoryReader.open(directory); // read-only=true
            IndexSearcher isearcher = newSearcher(ireader);

            Assert.AreEqual(1, isearcher.search(new TermQuery(new Term("fieldname", longTerm)), 1).totalHits);
            Query   query = new TermQuery(new Term("fieldname", "text"));
            TopDocs hits  = isearcher.search(query, null, 1);

            Assert.AreEqual(1, hits.totalHits);
            // Iterate through the results:
            for (int i = 0; i < hits.scoreDocs.length; i++)
            {
                Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
                Assert.AreEqual(text, hitDoc.get("fieldname"));
            }

            // Test simple phrase query
            PhraseQuery phraseQuery = new PhraseQuery();

            phraseQuery.add(new Term("fieldname", "to"));
            phraseQuery.add(new Term("fieldname", "be"));
            Assert.AreEqual(1, isearcher.search(phraseQuery, null, 1).totalHits);

            ireader.close();
            directory.close();
        }
Exemple #10
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void removeFromCache(long entityId) throws java.io.IOException
        private void RemoveFromCache(long entityId)
        {
            IndexSearcher searcher = _searcherManager.acquire();

            try
            {
                Query   query = _type.idTermQuery(entityId);
                TopDocs docs  = searcher.search(query, 1);
                if (docs.totalHits > 0)
                {
                    Document document = searcher.doc(docs.scoreDocs[0].doc);
                    foreach (IndexableField field in document.Fields)
                    {
                        string key   = field.name();
                        object value = field.stringValue();
                        RemoveFromCache(entityId, key, value);
                    }
                }
            }
            finally
            {
                _searcherManager.release(searcher);
            }
        }
        private bool Load(IndexSearcher searcher)
        {
            var query = new BooleanQuery();
            query.add(new TermQuery(new Term(PreferencesDocumentField, PreferencesName)), BooleanClause.Occur.MUST);

            nextFolderId = 1;

            var results = searcher.search(query, 1);
            if (results.totalHits > 0)
            {
                var doc = searcher.doc(results.scoreDocs[0].doc);
                if (doc.get(RegisteredFoldersField) != null)
                {
                    var prefs = JsonConvert.DeserializeObject<FolderPreferences>(doc.get(RegisteredFoldersField));
                    nextFolderId = prefs.NextFolderId;
                    foreach (var fm in prefs.FolderMapping)
                        registeredFolders.Add(fm.Id, fm.Path);
                    return true;
                }
            }

            return false;
        }
        public void IndexFolder(string folderName)
        {
            IDictionary<string,JToken> exifInfo = null;

            var didAddFiles = false;
            string[] allFilenames;
            try
            {
                allFilenames = System.IO.Directory.GetFiles(folderName);
            }
            catch (Exception ex)
            {
                logger.Error(String.Format("Exception getting filenames from {0}: {1}", folderName, ex));
                return;
            }

            foreach (var filename in allFilenames)
            {
                try
                {
                    if (mainReader.Value == null)
                    {
                        mainReader.Value = DirectoryReader.open(mainWriter.getDirectory());
                    }

                    CheckForCommit(mainWriter);

                    Interlocked.Increment(ref fileNumber);
                    if ((FileNumber % 100) == 0)
                    {
                        var seconds = (DateTime.Now - startTime).TotalSeconds;
                        logger.Debug("Processed {0} files in {1:N0} seconds ({2:N2} files/second); processing {3}", 
                            FileNumber,
                            seconds,
                            FileNumber / seconds,
                            filename);
                    }

                    if (!FileTypes.IsSupportedFilename(filename))
                        continue;

                    string hashValue = CalculateHash(filename);
                    var query = new BooleanQuery();
                    var fileInfo = new FileInfo(filename);

                    query.add(new TermQuery(new Term(FieldName.SignatureIndexPath, indexPreferences.ToIndexPath(filename))), BooleanClause.Occur.MUST);

                    var searcher = new IndexSearcher(mainReader.Value);
                    var results = searcher.search(query, 1);
                    if (results.totalHits > 0)
                    {
                        var existingDoc = searcher.doc(results.scoreDocs[0].doc);

                        // Skip this file? - no changes since it was last indexed
                        if (IsCurrent(existingDoc, filename, hashValue, fileInfo.Length))
                        {
                            // Ensure a thumbnail exists for this file
                            thumbnailIndexer.CheckThumbnail(
                                filename, 
                                existingDoc.get(FieldName.MimeType), 
                                existingDoc.get(FieldName.Size));

                            continue;
                        }

                        var deleteQuery = new BooleanQuery();
                        deleteQuery.add(new TermQuery(new Term(FieldName.SignatureIndexPath, indexPreferences.ToIndexPath(filename))), BooleanClause.Occur.MUST);
                        mainWriter.deleteDocuments(deleteQuery);
                    }

                    if (exifInfo == null)
                    {
                        try
                        {
                            exifInfo = BatchExifTool(folderName);
                        }
                        catch (Exception e)
                        {
                            logger.Warn("Skipping '{0}' due to exif failure: {1}", Path.GetDirectoryName(filename), e.Message);
                            break;
                        }
                    }

                    var exifSourceFile = filename;
                    foreach (var folderPrefix in Preferences.Instance.FoldersToWatch)
                        exifSourceFile = exifSourceFile.Replace(folderPrefix, "");

                    JToken exif;
                    exifInfo.TryGetValue(exifSourceFile.ToLower().Replace('\\', '/'), out exif);
                    var doc = new Document();

                    if (ProcessFile(filename, doc, hashValue, exif))
                    {
                        var facetsConfig = FindAPhoto.Common.Facets.FacetsConfig;
                        mainWriter.addDocument(facetsConfig.build(taxonomyWriter, doc));
                        ++FilesAdded;
                        didAddFiles = true;

                        MadeChange();
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(String.Format("Exception handling file {0}: {1}", filename, ex));
                }
            }

            // Added files aren't visible to this instance of the reader; the search won't detect them until a new
            // reader is opened. On the other hand, opening a reader after each addition is quite expensive.
            if (didAddFiles)
            {
                mainReader.Value.close();
                mainReader.Value = null;
            }
        }