Beispiel #1
0
        public void SynonymTest()
        {
            var indexWriter = new IndexWriter(new RAMDirectory(), _contentAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);

            AddDocument(indexWriter, "This device is HDTV compatible.");
            AddDocument(indexWriter, "Purchasing officer");
            indexWriter.commit();
            var indexSearcher = new IndexSearcher(indexWriter.getReader());

            var query = _queryParser.parse("hdtv");
            var hits  = indexSearcher.search(query, 10);

            Assert.AreEqual(1, hits.totalHits);

            query = _queryParser.parse("high definition television");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(1, hits.totalHits);

            query = _queryParser.parse("television");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(1, hits.totalHits);

            query = _queryParser.parse("\"high definition television\"");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(1, hits.totalHits);

            // Case 11017 "Purchasing Officer" returned while searching for "Technical coordinator".

            query = _queryParser.parse("Technical coordinator");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(0, hits.totalHits);
        }
Beispiel #2
0
        public void StemmerTest()
        {
            var indexWriter = new IndexWriter(new RAMDirectory(), _contentAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);

            AddDocument(indexWriter, "All our accountants and lawyers are busy at the moment");
            indexWriter.commit();
            var indexSearcher = new IndexSearcher(indexWriter.getReader());

            var query = _queryParser.parse("Accountant");
            var hits  = indexSearcher.search(query, 10);

            Assert.AreEqual(1, hits.totalHits);

            query = _queryParser.parse("account");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(0, hits.totalHits);

            query = _queryParser.parse("Lawyers");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(1, hits.totalHits);

            query = _queryParser.parse("Lawyer");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(1, hits.totalHits);
        }
        /*
         * This shows how to construct a phrase query containing shingles.
         */
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void testShingleAnalyzerWrapperPhraseQuery() throws Exception
        public virtual void testShingleAnalyzerWrapperPhraseQuery()
        {
            PhraseQuery q = new PhraseQuery();

            TokenStream ts = analyzer.tokenStream("content", "this sentence");

            try
            {
                int j = -1;

                PositionIncrementAttribute posIncrAtt = ts.addAttribute(typeof(PositionIncrementAttribute));
                CharTermAttribute          termAtt    = ts.addAttribute(typeof(CharTermAttribute));

                ts.reset();
                while (ts.incrementToken())
                {
                    j += posIncrAtt.PositionIncrement;
                    string termText = termAtt.ToString();
                    q.add(new Term("content", termText), j);
                }
                ts.end();
            }
            finally
            {
                IOUtils.closeWhileHandlingException(ts);
            }

            ScoreDoc[] hits  = searcher.search(q, null, 1000).scoreDocs;
            int[]      ranks = new int[] { 0 };
            compareRanks(hits, ranks);
        }
        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();
        }
Beispiel #5
0
        private ICollection <EntityId> InternalQuery(Query query, QueryContext contextOrNull)
        {
            if (this._directory == null)
            {
                return(Collections.emptySet());
            }

            try
            {
                Sort          sorting = contextOrNull != null ? contextOrNull.Sorting : null;
                bool          prioritizeCorrectness = contextOrNull == null || !contextOrNull.TradeCorrectnessForSpeed;
                IndexSearcher theSearcher           = Searcher(prioritizeCorrectness);
                query = IncludeOrphans(query);
                DocValuesCollector docValuesCollector = new DocValuesCollector(prioritizeCorrectness);
                theSearcher.search(query, docValuesCollector);
                ICollection <EntityId> result = new List <EntityId>();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.eclipse.collections.api.iterator.LongIterator valuesIterator = docValuesCollector.getSortedValuesIterator(KEY_DOC_ID, sorting);
                LongIterator valuesIterator = docValuesCollector.GetSortedValuesIterator(KEY_DOC_ID, sorting);
                while (valuesIterator.hasNext())
                {
                    result.Add(new EntityId_IdData(valuesIterator.next()));
                }
                return(result);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
Beispiel #6
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)));
        }
Beispiel #7
0
        private void DoTest(Random random, PrintWriter @out, bool useCompoundFiles, int MAX_DOCS)
        {
            Directory         directory = newDirectory();
            Analyzer          analyzer  = new MockAnalyzer(random);
            IndexWriterConfig conf      = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
            MergePolicy       mp        = conf.MergePolicy;

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

            if (VERBOSE)
            {
                Console.WriteLine("TEST: now build index MAX_DOCS=" + MAX_DOCS);
            }

            for (int j = 0; j < MAX_DOCS; j++)
            {
                Document d = new Document();
                d.Add(newTextField(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES));
                d.Add(newTextField(ID_FIELD, Convert.ToString(j), Field.Store.YES));
                writer.addDocument(d);
            }
            writer.Dispose();

            // try a search without OR
            IndexReader   reader   = DirectoryReader.Open(directory);
            IndexSearcher searcher = newSearcher(reader);

            Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY));

            @out.println("Query: " + query.ToString(PRIORITY_FIELD));
            if (VERBOSE)
            {
                Console.WriteLine("TEST: search query=" + query);
            }

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

            ScoreDoc[] hits = searcher.Search(query, null, MAX_DOCS, sort).scoreDocs;
            PrintHits(@out, hits, searcher);
            CheckHits(hits, MAX_DOCS, searcher);

            // try a new search with OR
            searcher = newSearcher(reader);
            hits     = null;

            BooleanQuery booleanQuery = new BooleanQuery();

            booleanQuery.Add(new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur_e.SHOULD);
            booleanQuery.Add(new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur_e.SHOULD);
            @out.println("Query: " + booleanQuery.ToString(PRIORITY_FIELD));

            hits = searcher.search(booleanQuery, null, MAX_DOCS, sort).scoreDocs;
            PrintHits(@out, hits, searcher);
            CheckHits(hits, MAX_DOCS, searcher);

            reader.Close();
            directory.Close();
        }
Beispiel #8
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();
        }
Beispiel #9
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static java.util.List<long> getAllNodes(org.apache.lucene.store.Directory directory, org.neo4j.values.storable.Value propertyValue) throws java.io.IOException
        public static IList <long> GetAllNodes(Directory directory, Value propertyValue)
        {
            using (SearcherManager manager = new SearcherManager(directory, new SearcherFactory()))
            {
                IndexSearcher     searcher  = manager.acquire();
                Query             query     = LuceneDocumentStructure.NewSeekQuery(propertyValue);
                AllNodesCollector collector = new AllNodesCollector();
                searcher.search(query, collector);
                return(collector._nodeIds);
            }
        }
Beispiel #10
0
        private TopDocs Search(IndexSearcher indexSearcher, string queryString)
        {
            var query = _queryParser.parse(queryString);

            Console.WriteLine();
            Console.WriteLine(queryString);
            Console.WriteLine(query.toString());
            var hits = indexSearcher.search(query, 10);

            return(hits);
        }
Beispiel #11
0
        public void PunctuationTest()
        {
            var indexWriter = new IndexWriter(new RAMDirectory(), _contentAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);

            AddDocument(indexWriter, "One, two.Three");
            indexWriter.commit();
            var indexSearcher = new IndexSearcher(indexWriter.getReader());

            var query = _queryParser.parse("one");
            var hits  = indexSearcher.search(query, 10);

            Assert.AreEqual(1, hits.totalHits);

            query = _queryParser.parse("two");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(1, hits.totalHits);

            query = _queryParser.parse("three");
            hits  = indexSearcher.search(query, 10);
            Assert.AreEqual(1, hits.totalHits);
        }
Beispiel #12
0
 private DocValuesCollector Search(Query query)
 {
     try
     {
         DocValuesCollector docValuesCollector = new DocValuesCollector();
         IndexSearcher.search(query, docValuesCollector);
         return(docValuesCollector);
     }
     catch (IOException e)
     {
         throw new Exception(e);
     }
 }
Beispiel #13
0
        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));
            }));
        }
 private ScoreEntityIterator IndexQuery(Query query)
 {
     try
     {
         DocValuesCollector docValuesCollector = new DocValuesCollector(true);
         IndexSearcher.search(query, docValuesCollector);
         ValuesIterator sortedValuesIterator = docValuesCollector.GetSortedValuesIterator(LuceneFulltextDocumentStructure.FIELD_ENTITY_ID, Sort.RELEVANCE);
         return(new ScoreEntityIterator(sortedValuesIterator));
     }
     catch (IOException e)
     {
         throw new Exception(e);
     }
 }
Beispiel #15
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();
            }
        }
Beispiel #16
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.apache.lucene.search.TopDocs toTopDocs(org.apache.lucene.search.Query query, org.neo4j.index.lucene.QueryContext context, org.apache.lucene.search.IndexSearcher searcher) throws java.io.IOException
        private TopDocs ToTopDocs(Query query, QueryContext context, IndexSearcher searcher)
        {
            Sort    sorting = context != null ? context.Sorting : null;
            TopDocs topDocs;

            if (sorting == null && context != null)
            {
                topDocs = searcher.search(query, context.Top);
            }
            else
            {
                if (context == null || !context.TradeCorrectnessForSpeed)
                {
                    TopFieldCollector collector = LuceneDataSource.ScoringCollector(sorting, context.Top);
                    searcher.search(query, collector);
                    topDocs = collector.topDocs();
                }
                else
                {
                    topDocs = searcher.search(query, null, context.Top, sorting);
                }
            }
            return(topDocs);
        }
Beispiel #17
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);
     }
 }
Beispiel #18
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();
        }
Beispiel #19
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);
            }
        }
Beispiel #20
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.eclipse.collections.api.set.primitive.LongSet gatherIdsModifiedInTransactionState(java.util.List<EntityId> simpleTransactionStateIds, org.apache.lucene.search.IndexSearcher fulltextTransactionStateSearcher, org.apache.lucene.search.Query query) throws java.io.IOException
        private LongSet GatherIdsModifiedInTransactionState(IList <EntityId> simpleTransactionStateIds, IndexSearcher fulltextTransactionStateSearcher, Query query)
        {
            // If there's no state them don't bother gathering it
            if (simpleTransactionStateIds.Count == 0 && fulltextTransactionStateSearcher == null)
            {
                return(LongSets.immutable.empty());
            }
            // There's potentially some state
            DocValuesCollector docValuesCollector = null;
            int fulltextSize = 0;

            if (fulltextTransactionStateSearcher != null)
            {
                docValuesCollector = new DocValuesCollector();
                fulltextTransactionStateSearcher.search(query, docValuesCollector);
                fulltextSize = docValuesCollector.TotalHits;
                // Nah, no state
                if (simpleTransactionStateIds.Count == 0 && fulltextSize == 0)
                {
                    return(LongSets.immutable.empty());
                }
            }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.eclipse.collections.api.set.primitive.MutableLongSet set = new org.eclipse.collections.impl.set.mutable.primitive.LongHashSet(simpleTransactionStateIds.size() + fulltextSize);
            MutableLongSet set = new LongHashSet(simpleTransactionStateIds.Count + fulltextSize);

            // Add from simple tx state
            foreach (EntityId id in simpleTransactionStateIds)
            {
                set.add(id.Id());
            }

            if (docValuesCollector != null)
            {
                // Add from fulltext tx state
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.eclipse.collections.api.iterator.LongIterator valuesIterator = docValuesCollector.getValuesIterator(LuceneExplicitIndex.KEY_DOC_ID);
                LongIterator valuesIterator = docValuesCollector.GetValuesIterator(LuceneExplicitIndex.KEY_DOC_ID);
                while (valuesIterator.hasNext())
                {
                    set.add(valuesIterator.next());
                }
            }
            return(set);
        }
Beispiel #21
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void letThroughAdditions(org.apache.lucene.search.IndexSearcher additionsSearcher, org.apache.lucene.search.Query query, java.util.Collection<EntityId> removed) throws java.io.IOException
        private void LetThroughAdditions(IndexSearcher additionsSearcher, Query query, ICollection <EntityId> removed)
        {
            // This could be improved further by doing a term-dict lookup for every term in removed
            // and retaining only those that did not match.
            // This is getting quite low-level though
            DocValuesCollector collector = new DocValuesCollector(false);

            additionsSearcher.search(query, collector);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.eclipse.collections.api.iterator.LongIterator valuesIterator = collector.getValuesIterator(KEY_DOC_ID);
            LongIterator valuesIterator = collector.GetValuesIterator(KEY_DOC_ID);
            LongCostume  id             = new LongCostume();

            while (valuesIterator.hasNext())
            {
                long value = valuesIterator.next();
                removed.remove(id.setId(value));
            }
        }
Beispiel #22
0
        static void SearchForPhrase(IndexSearcher searcher, string phrase)
        {
            using (new AutoStopWatch(string.Format("Search for {0}", phrase)))
            {
                var   parser = new QueryParser(org.apache.lucene.util.Version.LUCENE_CURRENT, "Body", new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_CURRENT));
                Query query  = parser.parse(phrase);

                var hits = searcher.search(query, 100);
                Console.WriteLine("Found {0} results for {1}", hits.totalHits, phrase);
                int max = hits.totalHits;
                if (max > 100)
                {
                    max = 100;
                }
                for (int i = 0; i < max; i++)
                {
                    Console.WriteLine(hits.scoreDocs[i].doc);
                }
            }
        }
Beispiel #23
0
        public override long CountIndexedNodes(long nodeId, int[] propertyKeyIds, params Value[] propertyValues)
        {
            Query nodeIdQuery = new TermQuery(LuceneDocumentStructure.newTermForChangeOrRemove(nodeId));
            Query valueQuery  = LuceneDocumentStructure.newSeekQuery(propertyValues);

            BooleanQuery.Builder nodeIdAndValueQuery = (new BooleanQuery.Builder()).setDisableCoord(true);
            nodeIdAndValueQuery.add(nodeIdQuery, BooleanClause.Occur.MUST);
            nodeIdAndValueQuery.add(valueQuery, BooleanClause.Occur.MUST);
            try
            {
                TotalHitCountCollector collector = new TotalHitCountCollector();
                IndexSearcher.search(nodeIdAndValueQuery.build(), collector);
                // A <label,propertyKeyId,nodeId> tuple should only match at most a single propertyValue
                return(collector.TotalHits);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
Beispiel #24
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void verify(org.neo4j.storageengine.api.NodePropertyAccessor accessor, int[] propKeyIds) throws org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException, java.io.IOException
        public override void Verify(NodePropertyAccessor accessor, int[] propKeyIds)
        {
            try
            {
                DuplicateCheckingCollector collector = DuplicateCheckingCollector.ForProperties(accessor, propKeyIds);
                IndexSearcher searcher = IndexSearcher();
                foreach (LeafReaderContext leafReaderContext in searcher.IndexReader.leaves())
                {
                    Fields fields = leafReaderContext.reader().fields();
                    foreach (string field in fields)
                    {
                        if (LuceneDocumentStructure.useFieldForUniquenessVerification(field))
                        {
                            TermsEnum terms = LuceneDocumentStructure.originalTerms(fields.terms(field), field);
                            BytesRef  termsRef;
                            while ((termsRef = terms.next()) != null)
                            {
                                if (terms.docFreq() > 1)
                                {
                                    collector.Init(terms.docFreq());
                                    searcher.search(new TermQuery(new Term(field, termsRef)), collector);
                                }
                            }
                        }
                    }
                }
            }
            catch (IOException e)
            {
                Exception cause = e.InnerException;
                if (cause is IndexEntryConflictException)
                {
                    throw ( IndexEntryConflictException )cause;
                }
                throw e;
            }
        }
Beispiel #25
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);
            }
        }
Beispiel #26
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.neo4j.graphdb.index.IndexHits<org.apache.lucene.document.Document> search(IndexReference searcherRef, org.apache.lucene.search.IndexSearcher fulltextTransactionStateSearcher, org.apache.lucene.search.Query query, org.neo4j.index.lucene.QueryContext additionalParametersOrNull, java.util.Collection<EntityId> removed) throws java.io.IOException
        private IndexHits <Document> Search(IndexReference searcherRef, IndexSearcher fulltextTransactionStateSearcher, Query query, QueryContext additionalParametersOrNull, ICollection <EntityId> removed)
        {
            if (fulltextTransactionStateSearcher != null && removed.Count > 0)
            {
                LetThroughAdditions(fulltextTransactionStateSearcher, query, removed);
            }

            IndexSearcher        searcher = fulltextTransactionStateSearcher == null ? searcherRef.Searcher : new IndexSearcher(new MultiReader(searcherRef.Searcher.IndexReader, fulltextTransactionStateSearcher.IndexReader));
            IndexHits <Document> result;

            if (additionalParametersOrNull != null && additionalParametersOrNull.Top > 0)
            {
                result = new TopDocsIterator(query, additionalParametersOrNull, searcher);
            }
            else
            {
                Sort sorting    = additionalParametersOrNull != null ? additionalParametersOrNull.Sorting : null;
                bool forceScore = additionalParametersOrNull == null || !additionalParametersOrNull.TradeCorrectnessForSpeed;
                DocValuesCollector collector = new DocValuesCollector(forceScore);
                searcher.search(query, collector);
                return(collector.GetIndexHits(sorting));
            }
            return(result);
        }
        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;
            }
        }
Beispiel #28
0
        static void SearchForPhrase(IndexSearcher searcher, string phrase)
        {
            using (new AutoStopWatch(string.Format("Search for {0}", phrase)))
            {
                var parser = new QueryParser(org.apache.lucene.util.Version.LUCENE_CURRENT, "Body", new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_CURRENT));
                Query query = parser.parse(phrase);

                var hits = searcher.search(query, 100);
                Console.WriteLine("Found {0} results for {1}", hits.totalHits, phrase);
                int max = hits.totalHits;
                if (max > 100)
                    max = 100;
                for (int i = 0; i < max; i++)
                {
                    Console.WriteLine(hits.scoreDocs[i].doc);
                }
            }
        }
        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;
        }