예제 #1
0
        static void Main(string[] args)
        {
            var    directory           = LuceneIndex();
            var    luceneIndexSearcher = new Lucene.Net.Search.IndexSearcher(directory);
            string input;

            Console.WriteLine(luceneIndexSearcher.MaxDoc);
            Console.WriteLine(luceneIndexSearcher.Doc(0));
            do
            {
                input = Console.ReadLine();
                //build a query object
                var searchTerm = new Lucene.Net.Index.Term("Data", input);
                var query      = new Lucene.Net.Search.FuzzyQuery(searchTerm, 0.05f);

                //execute the query
                var hits = luceneIndexSearcher.Search(query, 10);
                //iterate over the results.
                for (int i = 0; i < Math.Min(10, hits.TotalHits); i++)
                {
                    var    doc          = hits.ScoreDocs[i];
                    string contentValue = luceneIndexSearcher.Doc(doc.Doc).Get("Data");

                    Console.WriteLine(contentValue);
                }
                if (input == "all")
                {
                    for (int i = 0; i < luceneIndexSearcher.MaxDoc; i++)
                    {
                        Console.WriteLine(luceneIndexSearcher.Doc(i));
                    }
                }
            }while (input != "exit");
            directory.Dispose();
        }
예제 #2
0
        public void Run()
        {
            Lucene.Net.Index.IndexReader indexReader = Lucene.Net.Index.IndexReader.Open(_directory, true);
            Lucene.Net.Search.Searcher   indexSearch = new Lucene.Net.Search.IndexSearcher(indexReader);

            var queryParser = new QueryParser(luceneVersion, "TAGS", _analyzer);

            Console.Write("검색어를 입력해주세요 :");
            string q = Console.ReadLine();

            var query = queryParser.Parse(q);

            Console.WriteLine("[검색어] {0}", q);

            Lucene.Net.Search.TopDocs resultDocs = indexSearch.Search(query, indexReader.MaxDoc);

            var hits = resultDocs.ScoreDocs;

            int currentRow = 0;

            foreach (var hit in hits)
            {
                var documentFromSearch = indexSearch.Doc(hit.Doc);
                Console.WriteLine("* Result {0}", ++currentRow);
                Console.WriteLine("\t-제목 : {0}", documentFromSearch.Get("TITLE"));
                Console.WriteLine("\t-내용 : {0}", documentFromSearch.Get("SUMMARY"));
                Console.WriteLine("\t-태그 : {0}", documentFromSearch.Get("TAGS"));
            }

            Console.WriteLine();
        }
예제 #3
0
        public Task <IEnumerable <ISearchItem> > Search(string pattern, int page)
        {
            using (Analyzer analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48))
                using (Lucene.Net.Store.Directory index = new SimpleFSDirectory(Path.ChangeExtension(_bookFile.FullName,
                                                                                                     Convert.ToInt32(LuceneVersion.LUCENE_48).ToString())))
                    using (IndexReader reader = DirectoryReader.Open(index))
                    {
                        Lucene.Net.Search.Query query =
                            new QueryParser(LuceneVersion.LUCENE_48, nameof(TabHtmlText.Html), analyzer).Parse(pattern);
                        Lucene.Net.Search.TopScoreDocCollector collector =
                            Lucene.Net.Search.TopScoreDocCollector.Create(512, true);
                        Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(reader);
                        searcher.Search(query, collector);
                        Lucene.Net.Search.TopDocs docs = collector.GetTopDocs(page * PageSize, PageSize);

                        QueryScorer scorer      = new QueryScorer(query);
                        Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(), scorer) // SpanGradientFormatter
                        {
                            TextFragmenter = new SimpleSpanFragmenter(scorer, 30)
                        };

                        IEnumerable <ISearchItem> items = docs.ScoreDocs.Select(scoreDoc =>
                        {
                            Document doc       = searcher.Doc(scoreDoc.Doc);
                            string html        = doc.Get(nameof(TabHtmlText.Html));
                            string[] fragments = highlighter.GetBestFragments(new HTMLStripCharAnalyzer(),
                                                                              nameof(TabHtmlText.Html), html, 3);
                            return(new SearchItem(int.Parse(doc.Get(nameof(TabHtmlText.NumId))), string.Join("\n", fragments)));
                        });

                        return(Task.FromResult(items.ToList().AsEnumerable()));
                    }
        }
예제 #4
0
        public virtual void  TestRAMDirectoryString()
        {
            MockRAMDirectory ramDir = new MockRAMDirectory(indexDir.FullName);

            // Check size
            Assert.AreEqual(ramDir.SizeInBytes(), ramDir.GetRecomputedSizeInBytes());

            // open reader to test document count
            IndexReader reader = IndexReader.Open(ramDir);

            Assert.AreEqual(docsToAdd, reader.NumDocs());

            // open search zo check if all doc's are there
            IndexSearcher searcher = new IndexSearcher(reader);

            // search for all documents
            for (int i = 0; i < docsToAdd; i++)
            {
                Document doc = searcher.Doc(i);
                Assert.IsTrue(doc.GetField("content") != null);
            }

            // cleanup
            reader.Close();
            searcher.Close();
        }
예제 #5
0
        public virtual void  TestDemo_Renamed()
        {
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

            // Store the index in memory:
            Directory directory = new RAMDirectory();
            // To store an index on disk, use this instead:
            //Directory directory = FSDirectory.open("/tmp/testindex");
            IndexWriter iwriter = new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(25000));
            Document    doc     = new Document();

            System.String text = "This is the text to be indexed.";
            doc.Add(new Field("fieldname", text, Field.Store.YES, Field.Index.ANALYZED));
            iwriter.AddDocument(doc);
            iwriter.Close();

            // Now search the index:
            IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
            // Parse a simple query that searches for "text":
            QueryParser parser = new QueryParser(Util.Version.LUCENE_CURRENT, "fieldname", analyzer);
            Query       query  = parser.Parse("text");

            ScoreDoc[] hits = isearcher.Search(query, null, 1000).ScoreDocs;
            Assert.AreEqual(1, hits.Length);
            // Iterate through the results:
            for (int i = 0; i < hits.Length; i++)
            {
                Document hitDoc = isearcher.Doc(hits[i].Doc);
                Assert.AreEqual(hitDoc.Get("fieldname"), "This is the text to be indexed.");
            }
            isearcher.Close();
            directory.Close();
        }
예제 #6
0
		public virtual void  TestDemo_Renamed()
		{
			
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
			
			// Store the index in memory:
			Directory directory = new RAMDirectory();
			// To store an index on disk, use this instead:
			//Directory directory = FSDirectory.open("/tmp/testindex");
			IndexWriter iwriter = new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(25000));
			Document doc = new Document();
			System.String text = "This is the text to be indexed.";
			doc.Add(new Field("fieldname", text, Field.Store.YES, Field.Index.ANALYZED));
			iwriter.AddDocument(doc);
			iwriter.Close();
			
			// Now search the index:
			IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
			// Parse a simple query that searches for "text":
			QueryParser parser = new QueryParser("fieldname", analyzer);
			Query query = parser.Parse("text");
			ScoreDoc[] hits = isearcher.Search(query, null, 1000).scoreDocs;
			Assert.AreEqual(1, hits.Length);
			// Iterate through the results:
			for (int i = 0; i < hits.Length; i++)
			{
				Document hitDoc = isearcher.Doc(hits[i].doc);
				Assert.AreEqual(hitDoc.Get("fieldname"), "This is the text to be indexed.");
			}
			isearcher.Close();
			directory.Close();
		}
예제 #7
0
        /// <summary>
        /// Search for files.
        /// </summary>
        /// <param name="queryText">The query text.</param>
        /// <returns>The files that match the query text.</returns>
        public SourceFile[] Search(string queryText)
        {
            Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(
                Lucene.Net.Util.Version.LUCENE_30,
                "body",
                _analyzer);

            Lucene.Net.Search.Query query = parser.Parse(queryText);

            using (Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(_directory, true))
            {
                Lucene.Net.Search.TopDocs result = searcher.Search(query, int.MaxValue);

                List <SourceFile> files = new List <SourceFile>();
                foreach (Lucene.Net.Search.ScoreDoc d in result.ScoreDocs)
                {
                    Lucene.Net.Documents.Document doc = searcher.Doc(d.Doc);
                    files.Add(new SourceFile(
                                  doc.Get("id"),
                                  doc.Get("type"),
                                  doc.Get("name"),
                                  doc.Get("fileName"),
                                  null));
                }

                return(files.ToArray());
            }
        }
예제 #8
0
 public IEnumerable <int> Get(string search, int max = 100, int minScore = 1)
 {
     if (!built)
     {
         BuildIndexes();
         built = true;
     }
     try
     {
         var dir = FSDirectory.Open(new System.IO.DirectoryInfo(@"C:\lucene"));
         Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
         Lucene.Net.QueryParsers.QueryParser           parser   = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_30, "body", analyzer);
         Lucene.Net.Search.Query query = null;
         query = parser.Parse(search);
         var searcher = new Lucene.Net.Search.IndexSearcher(dir);
         var hits     = searcher.Search(query, max);
         var doc      = searcher.Doc(hits.ScoreDocs[0].Doc);
         var result   = hits.ScoreDocs.Where(s => s.Score > minScore).Select(h => int.Parse(searcher.Doc(h.Doc).GetField("id").StringValue));
         return(result);
     }
     catch (Exception e)
     {
         throw;
     }
 }
예제 #9
0
        } // End Sub BuildIndex

        // https://lucenenet.apache.org/
        // https://www.codeproject.com/Articles/609980/Small-Lucene-NET-Demo-App
        // https://stackoverflow.com/questions/12600196/lucene-how-to-index-file-names
        private static void SearchPath(string phrase, string indexPath)
        {
            Lucene.Net.Util.LuceneVersion version = Lucene.Net.Util.LuceneVersion.LUCENE_48;
            Lucene.Net.Store.Directory    luceneIndexDirectory = Lucene.Net.Store.FSDirectory.Open(indexPath);

            Lucene.Net.Index.IndexReader r = Lucene.Net.Index.DirectoryReader.Open(luceneIndexDirectory);

            Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(r);
            Lucene.Net.Analysis.Analyzer    analyzer = GetWrappedAnalyzer();

            Lucene.Net.QueryParsers.Classic.QueryParser parser = new Lucene.Net.QueryParsers.Classic.QueryParser(version, "file_name", analyzer);

            // https://stackoverflow.com/questions/15170097/how-to-search-across-all-the-fields
            // Lucene.Net.QueryParsers.Classic.MultiFieldQueryParser parser = new Lucene.Net.QueryParsers.Classic.MultiFieldQueryParser(version, GetFields(r), analyzer);


            Lucene.Net.Search.Query query = parser.Parse(Lucene.Net.QueryParsers.Classic.QueryParser.Escape(phrase));

            Lucene.Net.Search.ScoreDoc[] hits = searcher.Search(query, 10).ScoreDocs;
            foreach (Lucene.Net.Search.ScoreDoc hit in hits)
            {
                Lucene.Net.Documents.Document foundDoc = searcher.Doc(hit.Doc);
                System.Console.WriteLine(hit.Score);
                string full_name = foundDoc.Get("full_name");
                System.Console.WriteLine(full_name);
                // string favoritePhrase = foundDoc.Get("favoritePhrase");
                // System.Console.WriteLine(favoritePhrase);
            } // Next hit
        }     // End Sub SearchPath
예제 #10
0
        public IEnumerable <SearchResult> Search(string text, int maxResults, int minScore)
        {
            var query    = _parser.Parse(text);
            var searcher = new Lucene.Net.Search.IndexSearcher(_dir);
            var hits     = searcher.Search(query, maxResults);

            if (hits.ScoreDocs.Length == 0)
            {
                return(new List <SearchResult>());
            }

            var doc    = searcher.Doc(hits.ScoreDocs[0].Doc);
            var result = hits.ScoreDocs
                         .Where(s => s.Score > minScore)
                         .Select(h => new SearchResult()
            {
                Id = int.Parse(searcher.Doc(h.Doc).GetField("Id").StringValue), Score = h.Score
            });

            return(result);
        }
예제 #11
0
        public virtual void  TestFieldSetValue()
        {
            Field    field = new Field("id", "id1", Field.Store.YES, Field.Index.NOT_ANALYZED);
            Document doc   = new Document();

            doc.Add(field);
            doc.Add(new Field("keyword", "test", Field.Store.YES, Field.Index.NOT_ANALYZED));

            RAMDirectory dir    = new RAMDirectory();
            IndexWriter  writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);

            writer.AddDocument(doc);
            field.SetValue("id2");
            writer.AddDocument(doc);
            field.SetValue("id3");
            writer.AddDocument(doc);
            writer.Close();

            Searcher searcher = new IndexSearcher(dir);

            Query query = new TermQuery(new Term("keyword", "test"));

            // ensure that queries return expected results without DateFilter first
            ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
            Assert.AreEqual(3, hits.Length);
            int result = 0;

            for (int i = 0; i < 3; i++)
            {
                Document doc2 = searcher.Doc(hits[i].doc);
                Field    f    = doc2.GetField("id");
                if (f.StringValue().Equals("id1"))
                {
                    result |= 1;
                }
                else if (f.StringValue().Equals("id2"))
                {
                    result |= 2;
                }
                else if (f.StringValue().Equals("id3"))
                {
                    result |= 4;
                }
                else
                {
                    Assert.Fail("unexpected id field");
                }
            }
            searcher.Close();
            dir.Close();
            Assert.AreEqual(7, result, "did not see all IDs");
        }
예제 #12
0
        public List <indexVideo> searchArticles(string queryString, int numberOfResults, bool komin)
        {
            List <indexVideo> resultsList = new List <indexVideo>();

            Lucene.Net.Store.Directory index = publicIndex;
            if (komin)
            {
                index = kominIndex;
            }

            if (!string.IsNullOrEmpty(queryString))
            {
                Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new[] { "title", "shortDescription" }, analyser);
                try
                {
                    Lucene.Net.Search.Query query = parser.Parse(queryString + "~");

                    Lucene.Net.Search.Searcher searcher = new Lucene.Net.Search.IndexSearcher(index, true);

                    Lucene.Net.Search.TopScoreDocCollector collector = Lucene.Net.Search.TopScoreDocCollector.Create(numberOfResults, true);

                    searcher.Search(query, collector);

                    Lucene.Net.Search.ScoreDoc[] hits = collector.TopDocs().ScoreDocs;

                    if (hits.Length >= 1)
                    {
                        for (int i = 0; i < hits.Length; i++)
                        {
                            indexVideo video = new indexVideo();

                            int   docId = hits[i].Doc;
                            float score = hits[i].Score;

                            Lucene.Net.Documents.Document doc = searcher.Doc(docId);

                            video.bctid            = doc.Get("bctid");
                            video.title            = doc.Get("title");
                            video.score            = score;
                            video.shortDescription = doc.Get("shortDescription");
                            video.imageURL         = doc.Get("imageURL");

                            resultsList.Add(video);
                        }
                    }
                }
                catch (Exception e)
                {
                }
            }
            return(resultsList);
        }
예제 #13
0
        /* Open pre-lockless index, add docs, do a delete &
         * setNorm, and search */
        public virtual void  ChangeIndexNoAdds(System.String dirName)
        {
            dirName = FullDir(dirName);

            Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(dirName));

            // make sure searching sees right # hits
            IndexSearcher searcher = new IndexSearcher(dir, true);

            ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(34, hits.Length, "wrong number of hits");
            Document d = searcher.Doc(hits[0].Doc);

            Assert.AreEqual("21", d.Get("id"), "wrong first document");
            searcher.Close();

            // make sure we can do a delete & setNorm against this
            // pre-lockless segment:
            IndexReader reader     = IndexReader.Open(dir, false);
            Term        searchTerm = new Term("id", "6");
            int         delCount   = reader.DeleteDocuments(searchTerm);

            Assert.AreEqual(1, delCount, "wrong delete count");
            reader.SetNorm(22, "content", (float)2.0);
            reader.Close();

            // make sure they "took":
            searcher = new IndexSearcher(dir, true);
            hits     = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(33, hits.Length, "wrong number of hits");
            d = searcher.Doc(hits[0].Doc);
            Assert.AreEqual("22", d.Get("id"), "wrong first document");
            TestHits(hits, 33, searcher.IndexReader);
            searcher.Close();

            // optimize
            IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED);

            writer.Optimize();
            writer.Close();

            searcher = new IndexSearcher(dir, true);
            hits     = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(33, hits.Length, "wrong number of hits");
            d = searcher.Doc(hits[0].Doc);
            Assert.AreEqual("22", d.Get("id"), "wrong first document");
            TestHits(hits, 33, searcher.IndexReader);
            searcher.Close();

            dir.Close();
        }
예제 #14
0
        // Test that FieldScoreQuery returns docs in expected order.
        private void  DoTestRank(System.String field, FieldScoreQuery.Type tp)
        {
            IndexSearcher s = new IndexSearcher(dir, true);
            Query         q = new FieldScoreQuery(field, tp);

            Log("test: " + q);
            QueryUtils.Check(q, s);
            ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs;
            Assert.AreEqual(N_DOCS, h.Length, "All docs should be matched!");
            System.String prevID = "ID" + (N_DOCS + 1);             // greater than all ids of docs in this test
            for (int i = 0; i < h.Length; i++)
            {
                System.String resID = s.Doc(h[i].Doc).Get(ID_FIELD);
                Log(i + ".   score=" + h[i].Score + "  -  " + resID);
                Log(s.Explain(q, h[i].Doc));
                Assert.IsTrue(String.CompareOrdinal(resID, prevID) < 0, "res id " + resID + " should be < prev res id " + prevID);
                prevID = resID;
            }
        }
예제 #15
0
        private IEnumerable <Document> SearchText(string searchText, IEnumerable <string> fields, int page, int pageSize, Dictionary <string, string> condition)
        {
            StringBuilder conditionWhere = new StringBuilder();

            foreach (var item in condition)
            {
                conditionWhere.Append(" +" + item.Key + ":" + item.Value);
            }

            Open();

            var parser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields.ToArray(), _analyzer);
            var search = new Lucene.Net.Search.IndexSearcher(_luDirectory, true);

            var query = parser.Parse("+" + searchText + conditionWhere.ToString());

            var searchDocs = search.Search(query, 100).ScoreDocs;

            return(searchDocs.Select(t => search.Doc(t.Doc)));
        }
예제 #16
0
        public virtual void  TestGetValuesForIndexedDocument()
        {
            RAMDirectory dir    = new RAMDirectory();
            IndexWriter  writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);

            writer.AddDocument(MakeDocumentWithFields());
            writer.Close();

            Searcher searcher = new IndexSearcher(dir);

            // search for something that does exists
            Query query = new TermQuery(new Term("keyword", "test1"));

            // ensure that queries return expected results without DateFilter first
            ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
            Assert.AreEqual(1, hits.Length);

            DoAssert(searcher.Doc(hits[0].doc), true);
            searcher.Close();
        }
예제 #17
0
        // Test that queries based on reverse/ordFieldScore scores correctly
        private void  DoTestRank(System.String field, bool inOrder)
        {
            IndexSearcher s = new IndexSearcher(dir, true);
            ValueSource   vs;

            if (inOrder)
            {
                vs = new OrdFieldSource(field);
            }
            else
            {
                vs = new ReverseOrdFieldSource(field);
            }

            Query q = new ValueSourceQuery(vs);

            Log("test: " + q);
            QueryUtils.Check(q, s);
            ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs;
            Assert.AreEqual(N_DOCS, h.Length, "All docs should be matched!");
            System.String prevID = inOrder?"IE":"IC";             // smaller than all ids of docs in this test ("ID0001", etc.)

            for (int i = 0; i < h.Length; i++)
            {
                System.String resID = s.Doc(h[i].Doc).Get(ID_FIELD);
                Log(i + ".   score=" + h[i].Score + "  -  " + resID);
                Log(s.Explain(q, h[i].Doc));
                if (inOrder)
                {
                    Assert.IsTrue(String.CompareOrdinal(resID, prevID) < 0, "res id " + resID + " should be < prev res id " + prevID);
                }
                else
                {
                    Assert.IsTrue(String.CompareOrdinal(resID, prevID) > 0, "res id " + resID + " should be > prev res id " + prevID);
                }
                prevID = resID;
            }
        }
예제 #18
0
        public List <OfficeData> Search(Lucene.Net.Search.Query query, int maxResult)
        {
            List <OfficeData> results = new List <OfficeData>();

            Lucene.Net.Search.ScoreDoc[] hitsFound = searcher.Search(query, maxResult).ScoreDocs;
            OfficeData data = null;

            foreach (var hit in hitsFound)
            {
                data = new OfficeData();
                Document doc = searcher.Doc(hit.Doc);
                data.Body          = doc.Get("Body");
                data.LastWriteTime = doc.Get("LastWriteTime");
                data.FileName      = doc.Get("FileName");

                float score = hit.Score;
                data.Score       = score;
                data.ScoreDocDoc = hit.Doc; //vi tri co the xoa document (IndexReader)

                results.Add(data);
            }
            return(results);
        }
예제 #19
0
 public void Search(string keyword)
 {
     using (var indexer = Lucene.Net.Index.DirectoryReader.Open(_directory))
     {
         var searcher     = new Lucene.Net.Search.IndexSearcher(indexer);
         var queryParsers = new Lucene.Net.QueryParsers.Classic.QueryParser(Lucene.Net.Util.LuceneVersion.LUCENE_48, "Title", new Lucene.Net.Analysis.PanGu.PanGuAnalyzer(true));
         var query        = queryParsers.Parse(keyword);
         var hits         = searcher.Search(query, null, 16).ScoreDocs;
         var results      = hits.Select(hit =>
         {
             var doc = searcher.Doc(hit.Doc);
             return(new EditViewArticle
             {
                 Id = Convert.ToInt32(doc.Get("Id")),
                 Title = doc.Get("Title"),
                 Summary = doc.Get("TContents"),
                 //Title = HightLight(searchInfo, doc.Get("Title")),
                 //Summary = HightLight(searchInfo, doc.Get("TContents")),
                 CreateTime = DateTime.Parse(doc.Get("CreateTime"))
             });
         }).OrderByDescending(a => a.CreateTime).ToList();
     }
 }
        public virtual void ChangeIndexNoAdds(Random random, Directory dir)
        {
            // make sure searching sees right # hits
            DirectoryReader reader = DirectoryReader.Open(dir);
            IndexSearcher searcher = new IndexSearcher(reader);
            ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(34, hits.Length, "wrong number of hits");
            Document d = searcher.Doc(hits[0].Doc);
            Assert.AreEqual("wrong first document", "21", d.Get("id"));
            reader.Dispose();

            // fully merge
            IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode_e.APPEND));
            writer.ForceMerge(1);
            writer.Dispose();

            reader = DirectoryReader.Open(dir);
            searcher = new IndexSearcher(reader);
            hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(34, hits.Length, "wrong number of hits");
            DoTestHits(hits, 34, searcher.IndexReader);
            reader.Dispose();
        }
예제 #21
0
        /* Open pre-lockless index, add docs, do a delete &
         * setNorm, and search */
        public virtual void  ChangeIndexWithAdds(System.String dirName)
        {
            System.String origDirName = dirName;
            dirName = FullDir(dirName);

            Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(dirName));

            // open writer
            IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED);

            // add 10 docs
            for (int i = 0; i < 10; i++)
            {
                AddDoc(writer, 35 + i);
            }

            // make sure writer sees right total -- writer seems not to know about deletes in .del?
            int expected;

            if (Compare(origDirName, "24") < 0)
            {
                expected = 45;
            }
            else
            {
                expected = 46;
            }
            Assert.AreEqual(expected, writer.MaxDoc(), "wrong doc count");
            writer.Close();

            // make sure searching sees right # hits
            IndexSearcher searcher = new IndexSearcher(dir, true);

            ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Document   d    = searcher.Doc(hits[0].Doc);

            Assert.AreEqual("21", d.Get("id"), "wrong first document");
            TestHits(hits, 44, searcher.IndexReader);
            searcher.Close();

            // make sure we can do delete & setNorm against this
            // pre-lockless segment:
            IndexReader reader     = IndexReader.Open(dir, false);
            Term        searchTerm = new Term("id", "6");
            int         delCount   = reader.DeleteDocuments(searchTerm);

            Assert.AreEqual(1, delCount, "wrong delete count");
            reader.SetNorm(22, "content", (float)2.0);
            reader.Close();

            // make sure they "took":
            searcher = new IndexSearcher(dir, true);
            hits     = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(43, hits.Length, "wrong number of hits");
            d = searcher.Doc(hits[0].Doc);
            Assert.AreEqual("22", d.Get("id"), "wrong first document");
            TestHits(hits, 43, searcher.IndexReader);
            searcher.Close();

            // optimize
            writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED);
            writer.Optimize();
            writer.Close();

            searcher = new IndexSearcher(dir, true);
            hits     = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(43, hits.Length, "wrong number of hits");
            d = searcher.Doc(hits[0].Doc);
            TestHits(hits, 43, searcher.IndexReader);
            Assert.AreEqual("22", d.Get("id"), "wrong first document");
            searcher.Close();

            dir.Close();
        }
예제 #22
0
 // Make sure the documents returned by the search match the expected list
 // Copied from TestSort.java
 private void AssertMatches(IndexSearcher searcher, Query query, Sort sort, string expectedResult)
 {
     ScoreDoc[] result = searcher.Search(query, null, 1000, sort).ScoreDocs;
     StringBuilder buff = new StringBuilder(10);
     int n = result.Length;
     for (int i = 0; i < n; ++i)
     {
         Document doc = searcher.Doc(result[i].Doc);
         IndexableField[] v = doc.GetFields("tracer");
         for (int j = 0; j < v.Length; ++j)
         {
             buff.Append(v[j].StringValue);
         }
     }
     Assert.AreEqual(expectedResult, buff.ToString());
 }
예제 #23
0
		public virtual void  TestFieldSetValue()
		{
			
			Field field = new Field("id", "id1", Field.Store.YES, Field.Index.NOT_ANALYZED);
			Document doc = new Document();
			doc.Add(field);
			doc.Add(new Field("keyword", "test", Field.Store.YES, Field.Index.NOT_ANALYZED));
			
			RAMDirectory dir = new RAMDirectory();
			IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
			writer.AddDocument(doc);
			field.SetValue("id2");
			writer.AddDocument(doc);
			field.SetValue("id3");
			writer.AddDocument(doc);
			writer.Close();
			
			Searcher searcher = new IndexSearcher(dir);
			
			Query query = new TermQuery(new Term("keyword", "test"));
			
			// ensure that queries return expected results without DateFilter first
			ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs;
			Assert.AreEqual(3, hits.Length);
			int result = 0;
			for (int i = 0; i < 3; i++)
			{
				Document doc2 = searcher.Doc(hits[i].doc);
				Field f = doc2.GetField("id");
				if (f.StringValue().Equals("id1"))
					result |= 1;
				else if (f.StringValue().Equals("id2"))
					result |= 2;
				else if (f.StringValue().Equals("id3"))
					result |= 4;
				else
					Assert.Fail("unexpected id field");
			}
			searcher.Close();
			dir.Close();
			Assert.AreEqual(7, result, "did not see all IDs");
		}
예제 #24
0
		// Test that queries based on reverse/ordFieldScore scores correctly
		private void  DoTestRank(System.String field, bool inOrder)
		{
			IndexSearcher s = new IndexSearcher(dir, true);
			ValueSource vs;
			if (inOrder)
			{
				vs = new OrdFieldSource(field);
			}
			else
			{
				vs = new ReverseOrdFieldSource(field);
			}
			
			Query q = new ValueSourceQuery(vs);
			Log("test: " + q);
			QueryUtils.Check(q, s);
			ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs;
			Assert.AreEqual(N_DOCS, h.Length, "All docs should be matched!");
			System.String prevID = inOrder?"IE":"IC"; // smaller than all ids of docs in this test ("ID0001", etc.)
			
			for (int i = 0; i < h.Length; i++)
			{
				System.String resID = s.Doc(h[i].Doc).Get(ID_FIELD);
				Log(i + ".   score=" + h[i].Score + "  -  " + resID);
				Log(s.Explain(q, h[i].Doc));
				if (inOrder)
				{
					Assert.IsTrue(String.CompareOrdinal(resID, prevID) < 0, "res id " + resID + " should be < prev res id " + prevID);
				}
				else
				{
					Assert.IsTrue(String.CompareOrdinal(resID, prevID) > 0, "res id " + resID + " should be > prev res id " + prevID);
				}
				prevID = resID;
			}
		}
예제 #25
0
			public override void  Run()
			{
				while (!stopped)
				{
					if (index % 2 == 0)
					{
						// refresh reader synchronized
						ReaderCouple c = (Enclosing_Instance.RefreshReader(r, test, index, true));
						SupportClass.CollectionsHelper.AddIfNotContains(readersToClose, c.newReader);
						SupportClass.CollectionsHelper.AddIfNotContains(readersToClose, c.refreshedReader);
						readers.Add(c);
						// prevent too many readers
						break;
					}
					else
					{
						// not synchronized
						IndexReader refreshed = r.Reopen();
						
						
						IndexSearcher searcher = new IndexSearcher(refreshed);
						ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("field1", "a" + rnd.Next(refreshed.MaxDoc()))), null, 1000).ScoreDocs;
						if (hits.Length > 0)
						{
							searcher.Doc(hits[0].doc);
						}
						
						// r might have changed because this is not a 
						// synchronized method. However we don't want
						// to make it synchronized to test 
						// thread-safety of IndexReader.close().
						// That's why we add refreshed also to 
						// readersToClose, because double closing is fine
						if (refreshed != r)
						{
							refreshed.Close();
						}
						SupportClass.CollectionsHelper.AddIfNotContains(readersToClose, refreshed);
					}
					lock (this)
					{
						System.Threading.Monitor.Wait(this, TimeSpan.FromMilliseconds(1000));
					}
				}
			}
예제 #26
0
		public virtual void  TestRAMDirectory_Renamed_Method()
		{
			
			Directory dir = FSDirectory.GetDirectory(indexDir);
			MockRAMDirectory ramDir = new MockRAMDirectory(dir);
			
			// close the underlaying directory and delete the index
			dir.Close();
			
			// Check size
			Assert.AreEqual(ramDir.SizeInBytes(), ramDir.GetRecomputedSizeInBytes());
			
			// open reader to test document count
			IndexReader reader = IndexReader.Open(ramDir);
			Assert.AreEqual(docsToAdd, reader.NumDocs());
			
			// open search zo check if all doc's are there
			IndexSearcher searcher = new IndexSearcher(reader);
			
			// search for all documents
			for (int i = 0; i < docsToAdd; i++)
			{
				Lucene.Net.Documents.Document doc = searcher.Doc(i);
				Assert.IsTrue(doc.GetField("content") != null);
			}
			
			// cleanup
			reader.Close();
			searcher.Close();
		}
예제 #27
0
        public async Task <SearchQueryResponse> Handle(SearchQueryRequest request, CancellationToken cancellationToken)
        {
            IndexReader reader = null;

            try
            {
                // Ensures index backwards compatibility
                var AppLuceneVersion = LuceneVersion.LUCENE_48;

                //Used cached Lucene Index
                var readerWithMetadata = await _luceneReaderService.GetReader(cancellationToken);

                reader = readerWithMetadata.Index;

                //create an analyzer to process the text
                var analyzer = new StandardAnalyzer(AppLuceneVersion);

                var pageLength = request.PageLength ?? 30;
                var pageIndex  = request.PageIndex ?? 0;

                //hardcoded
                var search_phrase = request.Query;

                var startDateQuery_as_int32 = 0; //min value
                if (request.StartDate.HasValue)
                {
                    //var startDateCriteria = DateTime.Parse("2020-01-01"); //if we want to support text to int transformation
                    startDateQuery_as_int32 = request.StartDate.Value;
                }

                var endDateQuery_as_int32 = int.MaxValue;
                if (request.EndDate.HasValue)
                {
                    //var endDateCriteria = DateTime.Parse("2021-12-31"); if we want to suppoer text to int transformation
                    endDateQuery_as_int32 = request.EndDate.Value;
                }



                //date filter
                var date_query = Lucene.Net.Search.NumericRangeQuery.NewInt32Range(DateRangeByTerm, startDateQuery_as_int32, endDateQuery_as_int32, true, true);

                //text query
                var query_classic_parser           = new Lucene.Net.QueryParsers.Classic.QueryParser(LuceneVersion.LUCENE_48, SearchByTerm, analyzer);
                Lucene.Net.Search.Query text_query = query_classic_parser.Parse(search_phrase);

                //merging
                Lucene.Net.Search.BooleanQuery final_query = new Lucene.Net.Search.BooleanQuery();
                final_query.Add(text_query, Lucene.Net.Search.Occur.MUST);
                final_query.Add(date_query, Lucene.Net.Search.Occur.MUST);

                var searcher = new Lucene.Net.Search.IndexSearcher(reader); // writer.GetReader(applyAllDeletes: true));
                                                                            //var hits = searcher.Search(phrase, 20 /* top 20 */).ScoreDocs;
                var maxResult = pageLength * (pageIndex + 1) + 1;           //need an extra one to determine if there is more

                _logger.LogDebug("Search criteria '{0}' with range ('{1}' to '{2}') with pageIndex {3} and pageLength {4}"
                                 , search_phrase
                                 , startDateQuery_as_int32, endDateQuery_as_int32
                                 , pageIndex, pageLength);

                var results = searcher.Search(final_query, maxResult);

                _logger.LogDebug("Search result has {0} records (before skip).", results.ScoreDocs.Length);

                var response = new SearchQueryResponse();
                response.IsValid      = true;
                response.PageIndex    = pageIndex;
                response.PageLength   = pageLength;
                response.IndexDateUtc = readerWithMetadata.LastIndexOffset;

                var lastPage = results.ScoreDocs.Skip(pageLength * pageIndex);

                if (lastPage.Count() == 0)
                {
                    //Console.WriteLine("No results.");
                    response.Hits = new SearchHitEntry[0];
                }
                else
                {
                    response.HasMore = lastPage.Count() > pageLength;   //if it has an extra one, then there is more

                    var records = new List <SearchHitEntry>();
                    foreach (var hit in lastPage.Take(pageLength)) //foreach (var hit in hits)
                    {
                        var foundDoc            = searcher.Doc(hit.Doc);
                        var foundDocCreatedDate = foundDoc.GetField("updated").GetInt32Value();

                        DateTime parsedDate;

                        var searchResultEntry = new SearchHitEntry()
                        {
                            DocId = foundDoc.Get("doc_id"),
                            Text  = foundDoc.Get("content"),
                            Rank  = hit.Score,
                        };

                        if (foundDocCreatedDate.HasValue && DateTime.TryParseExact(foundDocCreatedDate.Value.ToString(), "yyyyMMdd", null,
                                                                                   DateTimeStyles.None, out parsedDate))
                        {
                            searchResultEntry.ModifiedDate = parsedDate;
                        }
                        else
                        {
                            searchResultEntry.ModifiedDate = null;
                        }
                        records.Add(searchResultEntry);
                    }
                    response.Hits = records.ToArray();
                }



                return(response);
            }
            catch (Exception ex)
            {
                var result = new SearchQueryResponse();
                result.IsValid = false;
                result.Errors  = new List <string>();
                result.Errors.Add("Unexpected error occured: " + ex.Message);
                return(result);
            }
            finally
            {
                // since Lucene IndexReader is cached, it is cache responsibility to dispose Index and Directory properly
                //if (reader != null)
                //{
                //    reader.Dispose();
                //}
                //if (azureDirectory != null)
                //{
                //    azureDirectory.Dispose();
                //}
            }
        }
		/* Open pre-lockless index, add docs, do a delete &
		* setNorm, and search */
		public virtual void  ChangeIndexWithAdds(System.String dirName, bool autoCommit)
		{
			System.String origDirName = dirName;
			dirName = FullDir(dirName);
			
			Directory dir = FSDirectory.Open(new System.IO.FileInfo(dirName));
			
			// open writer
			IndexWriter writer = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), false);
			
			// add 10 docs
			for (int i = 0; i < 10; i++)
			{
				AddDoc(writer, 35 + i);
			}
			
			// make sure writer sees right total -- writer seems not to know about deletes in .del?
			int expected;
			if (Compare(origDirName, "24") < 0)
			{
				expected = 45;
			}
			else
			{
				expected = 46;
			}
			Assert.AreEqual(expected, writer.DocCount(), "wrong doc count");
			writer.Close();
			
			// make sure searching sees right # hits
			IndexSearcher searcher = new IndexSearcher(dir);
			ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs;
			Document d = searcher.Doc(hits[0].doc);
			Assert.AreEqual("21", d.Get("id"), "wrong first document");
			TestHits(hits, 44, searcher.GetIndexReader());
			searcher.Close();
			
			// make sure we can do delete & setNorm against this
			// pre-lockless segment:
			IndexReader reader = IndexReader.Open(dir);
			Term searchTerm = new Term("id", "6");
			int delCount = reader.DeleteDocuments(searchTerm);
			Assert.AreEqual(1, delCount, "wrong delete count");
			reader.SetNorm(22, "content", (float) 2.0);
			reader.Close();
			
			// make sure they "took":
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs;
			Assert.AreEqual(43, hits.Length, "wrong number of hits");
			d = searcher.Doc(hits[0].doc);
			Assert.AreEqual("22", d.Get("id"), "wrong first document");
			TestHits(hits, 43, searcher.GetIndexReader());
			searcher.Close();
			
			// optimize
			writer = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), false);
			writer.Optimize();
			writer.Close();
			
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs;
			Assert.AreEqual(43, hits.Length, "wrong number of hits");
			d = searcher.Doc(hits[0].doc);
			TestHits(hits, 43, searcher.GetIndexReader());
			Assert.AreEqual("22", d.Get("id"), "wrong first document");
			searcher.Close();
			
			dir.Close();
		}
		public virtual void  searchIndex(System.String dirName, System.String oldName)
		{
			//QueryParser parser = new QueryParser("contents", new WhitespaceAnalyzer());
			//Query query = parser.parse("handle:1");
			
			dirName = FullDir(dirName);
			
			Directory dir = FSDirectory.Open(new System.IO.FileInfo(dirName));
			IndexSearcher searcher = new IndexSearcher(dir);
			IndexReader reader = searcher.GetIndexReader();
			
			_TestUtil.CheckIndex(dir);
			
			for (int i = 0; i < 35; i++)
			{
				if (!reader.IsDeleted(i))
				{
					Document d = reader.Document(i);
					System.Collections.IList fields = d.GetFields();
					if (!oldName.StartsWith("19.") && !oldName.StartsWith("20.") && !oldName.StartsWith("21.") && !oldName.StartsWith("22."))
					{
						
						if (d.GetField("content3") == null)
						{
							Assert.AreEqual(5, fields.Count);
							Field f = (Field) d.GetField("id");
							Assert.AreEqual("" + i, f.StringValue());
							
							f = (Field) d.GetField("utf8");
							Assert.AreEqual("Lu\uD834\uDD1Ece\uD834\uDD60ne \u0000 \u2620 ab\ud917\udc17cd", f.StringValue());
							
							f = (Field) d.GetField("autf8");
							Assert.AreEqual("Lu\uD834\uDD1Ece\uD834\uDD60ne \u0000 \u2620 ab\ud917\udc17cd", f.StringValue());
							
							f = (Field) d.GetField("content2");
							Assert.AreEqual("here is more content with aaa aaa aaa", f.StringValue());
							
							f = (Field) d.GetField("fie\u2C77ld");
							Assert.AreEqual("field with non-ascii name", f.StringValue());
						}
					}
				}
				// Only ID 7 is deleted
				else
					Assert.AreEqual(7, i);
			}
			
			ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs;
			
			// First document should be #21 since it's norm was
			// increased:
			Document d2 = searcher.Doc(hits[0].doc);
			Assert.AreEqual("21", d2.Get("id"), "didn't get the right document first");
			
			TestHits(hits, 34, searcher.GetIndexReader());
			
			if (!oldName.StartsWith("19.") && !oldName.StartsWith("20.") && !oldName.StartsWith("21.") && !oldName.StartsWith("22."))
			{
				// Test on indices >= 2.3
				hits = searcher.Search(new TermQuery(new Term("utf8", "\u0000")), null, 1000).scoreDocs;
				Assert.AreEqual(34, hits.Length);
				hits = searcher.Search(new TermQuery(new Term("utf8", "Lu\uD834\uDD1Ece\uD834\uDD60ne")), null, 1000).scoreDocs;
				Assert.AreEqual(34, hits.Length);
				hits = searcher.Search(new TermQuery(new Term("utf8", "ab\ud917\udc17cd")), null, 1000).scoreDocs;
				Assert.AreEqual(34, hits.Length);
			}
			
			searcher.Close();
			dir.Close();
		}
        public virtual void TestNumericFields()
        {
            foreach (string name in OldNames)
            {

                Directory dir = OldIndexDirs[name];
                IndexReader reader = DirectoryReader.Open(dir);
                IndexSearcher searcher = new IndexSearcher(reader);

                for (int id = 10; id < 15; id++)
                {
                    ScoreDoc[] hits = searcher.Search(NumericRangeQuery.NewIntRange("trieInt", 4, Convert.ToInt32(id), Convert.ToInt32(id), true, true), 100).ScoreDocs;
                    Assert.AreEqual(1, hits.Length, "wrong number of hits");
                    Document d = searcher.Doc(hits[0].Doc);
                    Assert.AreEqual(Convert.ToString(id), d.Get("id"));

                    hits = searcher.Search(NumericRangeQuery.NewLongRange("trieLong", 4, Convert.ToInt64(id), Convert.ToInt64(id), true, true), 100).ScoreDocs;
                    Assert.AreEqual(1, hits.Length, "wrong number of hits");
                    d = searcher.Doc(hits[0].Doc);
                    Assert.AreEqual(Convert.ToString(id), d.Get("id"));
                }

                // check that also lower-precision fields are ok
                ScoreDoc[] hits_ = searcher.Search(NumericRangeQuery.NewIntRange("trieInt", 4, int.MinValue, int.MaxValue, false, false), 100).ScoreDocs;
                Assert.AreEqual(34, hits_.Length, "wrong number of hits");

                hits_ = searcher.Search(NumericRangeQuery.NewLongRange("trieLong", 4, long.MinValue, long.MaxValue, false, false), 100).ScoreDocs;
                Assert.AreEqual(34, hits_.Length, "wrong number of hits");

                // check decoding into field cache
                FieldCache_Fields.Ints fci = FieldCache_Fields.DEFAULT.GetInts(SlowCompositeReaderWrapper.Wrap(searcher.IndexReader), "trieInt", false);
                int maxDoc = searcher.IndexReader.MaxDoc();
                for (int doc = 0; doc < maxDoc; doc++)
                {
                    int val = fci.Get(doc);
                    Assert.IsTrue(val >= 0 && val < 35, "value in id bounds");
                }

                FieldCache_Fields.Longs fcl = FieldCache_Fields.DEFAULT.GetLongs(SlowCompositeReaderWrapper.Wrap(searcher.IndexReader), "trieLong", false);
                for (int doc = 0; doc < maxDoc; doc++)
                {
                    long val = fcl.Get(doc);
                    Assert.IsTrue(val >= 0L && val < 35L, "value in id bounds");
                }

                reader.Dispose();
            }
        }
 private IEnumerable <LuceneData> MapLuceneDataToIDList(IEnumerable <Lucene.Net.Search.ScoreDoc> hits, Lucene.Net.Search.IndexSearcher searcher)
 {
     return(hits.Select(hit => MapLuceneData(searcher.Doc(hit.Doc))).ToList());
 }
        public virtual void ChangeIndexWithAdds(Random random, Directory dir, string origOldName)
        {
            // open writer
            IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode_e.APPEND));
            // add 10 docs
            for (int i = 0; i < 10; i++)
            {
                AddDoc(writer, 35 + i);
            }

            // make sure writer sees right total -- writer seems not to know about deletes in .del?
            int expected;
            if (Compare(origOldName, "24") < 0)
            {
                expected = 44;
            }
            else
            {
                expected = 45;
            }
            Assert.AreEqual(expected, writer.NumDocs(), "wrong doc count");
            writer.Dispose();

            // make sure searching sees right # hits
            IndexReader reader = DirectoryReader.Open(dir);
            IndexSearcher searcher = new IndexSearcher(reader);
            ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Document d = searcher.IndexReader.Document(hits[0].Doc);
            Assert.AreEqual("wrong first document", "21", d.Get("id"));
            DoTestHits(hits, 44, searcher.IndexReader);
            reader.Dispose();

            // fully merge
            writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode_e.APPEND));
            writer.ForceMerge(1);
            writer.Dispose();

            reader = DirectoryReader.Open(dir);
            searcher = new IndexSearcher(reader);
            hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;
            Assert.AreEqual(44, hits.Length, "wrong number of hits");
            d = searcher.Doc(hits[0].Doc);
            DoTestHits(hits, 44, searcher.IndexReader);
            Assert.AreEqual("wrong first document", "21", d.Get("id"));
            reader.Dispose();
        }
예제 #33
0
 // Test that FieldScoreQuery returns docs in expected order.
 private void  DoTestRank(System.String field, FieldScoreQuery.Type tp)
 {
     IndexSearcher s = new IndexSearcher(dir, true);
     Query q = new FieldScoreQuery(field, tp);
     Log("test: " + q);
     QueryUtils.Check(q, s);
     ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs;
     Assert.AreEqual(N_DOCS, h.Length, "All docs should be matched!");
     System.String prevID = "ID" + (N_DOCS + 1); // greater than all ids of docs in this test
     for (int i = 0; i < h.Length; i++)
     {
         System.String resID = s.Doc(h[i].Doc).Get(ID_FIELD);
         Log(i + ".   score=" + h[i].Score + "  -  " + resID);
         Log(s.Explain(q, h[i].Doc));
         Assert.IsTrue(String.CompareOrdinal(resID, prevID) < 0, "res id " + resID + " should be < prev res id " + prevID);
         prevID = resID;
     }
 }
예제 #34
0
		public virtual void  TestRAMDirectoryString()
		{
			
			MockRAMDirectory ramDir = new MockRAMDirectory(indexDir.FullName);
			
			// Check size
			Assert.AreEqual(ramDir.SizeInBytes(), ramDir.GetRecomputedSizeInBytes());
			
			// open reader to test document count
			IndexReader reader = IndexReader.Open(ramDir);
			Assert.AreEqual(docsToAdd, reader.NumDocs());
			
			// open search zo check if all doc's are there
			IndexSearcher searcher = new IndexSearcher(reader);
			
			// search for all documents
			for (int i = 0; i < docsToAdd; i++)
			{
				Document doc = searcher.Doc(i);
				Assert.IsTrue(doc.GetField("content") != null);
			}
			
			// cleanup
			reader.Close();
			searcher.Close();
		}
예제 #35
0
		public virtual void  TestGetValuesForIndexedDocument()
		{
			RAMDirectory dir = new RAMDirectory();
			IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
			writer.AddDocument(MakeDocumentWithFields());
			writer.Close();
			
			Searcher searcher = new IndexSearcher(dir);
			
			// search for something that does exists
			Query query = new TermQuery(new Term("keyword", "test1"));
			
			// ensure that queries return expected results without DateFilter first
			ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs;
			Assert.AreEqual(1, hits.Length);
			
			DoAssert(searcher.Doc(hits[0].doc), true);
			searcher.Close();
		}
예제 #36
0
        public virtual void  searchIndex(System.String dirName, System.String oldName)
        {
            //QueryParser parser = new QueryParser("contents", new WhitespaceAnalyzer());
            //Query query = parser.parse("handle:1");

            dirName = FullDir(dirName);

            Directory     dir      = FSDirectory.Open(new System.IO.DirectoryInfo(dirName));
            IndexSearcher searcher = new IndexSearcher(dir, true);
            IndexReader   reader   = searcher.IndexReader;

            _TestUtil.CheckIndex(dir);

            for (int i = 0; i < 35; i++)
            {
                if (!reader.IsDeleted(i))
                {
                    Document d      = reader.Document(i);
                    var      fields = d.GetFields();
                    if (!oldName.StartsWith("19.") && !oldName.StartsWith("20.") && !oldName.StartsWith("21.") && !oldName.StartsWith("22."))
                    {
                        if (d.GetField("content3") == null)
                        {
                            int numFields = oldName.StartsWith("29.") ? 7 : 5;
                            Assert.AreEqual(numFields, fields.Count);
                            Field f = d.GetField("id");
                            Assert.AreEqual("" + i, f.StringValue);

                            f = (Field)d.GetField("utf8");
                            Assert.AreEqual("Lu\uD834\uDD1Ece\uD834\uDD60ne \u0000 \u2620 ab\ud917\udc17cd", f.StringValue);

                            f = (Field)d.GetField("autf8");
                            Assert.AreEqual("Lu\uD834\uDD1Ece\uD834\uDD60ne \u0000 \u2620 ab\ud917\udc17cd", f.StringValue);

                            f = (Field)d.GetField("content2");
                            Assert.AreEqual("here is more content with aaa aaa aaa", f.StringValue);

                            f = (Field)d.GetField("fie\u2C77ld");
                            Assert.AreEqual("field with non-ascii name", f.StringValue);
                        }
                    }
                }
                // Only ID 7 is deleted
                else
                {
                    Assert.AreEqual(7, i);
                }
            }

            ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).ScoreDocs;

            // First document should be #21 since it's norm was
            // increased:
            Document d2 = searcher.Doc(hits[0].Doc);

            Assert.AreEqual("21", d2.Get("id"), "didn't get the right document first");

            TestHits(hits, 34, searcher.IndexReader);

            if (!oldName.StartsWith("19.") && !oldName.StartsWith("20.") && !oldName.StartsWith("21.") && !oldName.StartsWith("22."))
            {
                // Test on indices >= 2.3
                hits = searcher.Search(new TermQuery(new Term("utf8", "\u0000")), null, 1000).ScoreDocs;
                Assert.AreEqual(34, hits.Length);
                hits = searcher.Search(new TermQuery(new Term("utf8", "Lu\uD834\uDD1Ece\uD834\uDD60ne")), null, 1000).ScoreDocs;
                Assert.AreEqual(34, hits.Length);
                hits = searcher.Search(new TermQuery(new Term("utf8", "ab\ud917\udc17cd")), null, 1000).ScoreDocs;
                Assert.AreEqual(34, hits.Length);
            }

            searcher.Close();
            dir.Close();
        }
		/* Open pre-lockless index, add docs, do a delete &
		* setNorm, and search */
		public virtual void  ChangeIndexNoAdds(System.String dirName, bool autoCommit)
		{
			
			dirName = FullDir(dirName);
			
			Directory dir = FSDirectory.Open(new System.IO.FileInfo(dirName));
			
			// make sure searching sees right # hits
			IndexSearcher searcher = new IndexSearcher(dir);
			ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs;
			Assert.AreEqual(34, hits.Length, "wrong number of hits");
			Document d = searcher.Doc(hits[0].doc);
			Assert.AreEqual("21", d.Get("id"), "wrong first document");
			searcher.Close();
			
			// make sure we can do a delete & setNorm against this
			// pre-lockless segment:
			IndexReader reader = IndexReader.Open(dir);
			Term searchTerm = new Term("id", "6");
			int delCount = reader.DeleteDocuments(searchTerm);
			Assert.AreEqual(1, delCount, "wrong delete count");
			reader.SetNorm(22, "content", (float) 2.0);
			reader.Close();
			
			// make sure they "took":
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs;
			Assert.AreEqual(33, hits.Length, "wrong number of hits");
			d = searcher.Doc(hits[0].doc);
			Assert.AreEqual("22", d.Get("id"), "wrong first document");
			TestHits(hits, 33, searcher.GetIndexReader());
			searcher.Close();
			
			// optimize
			IndexWriter writer = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), false);
			writer.Optimize();
			writer.Close();
			
			searcher = new IndexSearcher(dir);
			hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs;
			Assert.AreEqual(33, hits.Length, "wrong number of hits");
			d = searcher.Doc(hits[0].doc);
			Assert.AreEqual("22", d.Get("id"), "wrong first document");
			TestHits(hits, 33, searcher.GetIndexReader());
			searcher.Close();
			
			dir.Close();
		}
예제 #38
0
        public static void Main(System.String[] a)
        {
            System.String indexName = "localhost_index";
            System.String fn        = "c:/Program Files/Apache Group/Apache/htdocs/manual/vhosts/index.html.en";
            System.Uri    url       = null;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i].Equals("-i"))
                {
                    indexName = a[++i];
                }
                else if (a[i].Equals("-f"))
                {
                    fn = a[++i];
                }
                else if (a[i].Equals("-url"))
                {
                    url = new System.Uri(a[++i]);
                }
            }

            System.IO.StreamWriter temp_writer;
            temp_writer           = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Console.Out.Encoding);
            temp_writer.AutoFlush = true;
            System.IO.StreamWriter o   = temp_writer;
            FSDirectory            dir = FSDirectory.Open(new DirectoryInfo(indexName));
            IndexReader            r   = IndexReader.Open(dir, true);

            o.WriteLine("Open index " + indexName + " which has " + r.NumDocs() + " docs");

            MoreLikeThis mlt = new MoreLikeThis(r);

            o.WriteLine("Query generation parameters:");
            o.WriteLine(mlt.DescribeParams());
            o.WriteLine();

            Query query = null;

            if (url != null)
            {
                o.WriteLine("Parsing URL: " + url);
                query = mlt.Like(url);
            }
            else if (fn != null)
            {
                o.WriteLine("Parsing file: " + fn);
                query = mlt.Like(new System.IO.FileInfo(fn));
            }

            o.WriteLine("q: " + query);
            o.WriteLine();
            IndexSearcher searcher = new IndexSearcher(dir, true);

            TopDocs hits = searcher.Search(query, null, 25);
            int     len  = hits.TotalHits;

            o.WriteLine("found: " + len + " documents matching");
            o.WriteLine();
            ScoreDoc[] scoreDocs = hits.ScoreDocs;
            for (int i = 0; i < System.Math.Min(25, len); i++)
            {
                Document      d       = searcher.Doc(scoreDocs[i].Doc);
                System.String summary = d.Get("summary");
                o.WriteLine("score  : " + scoreDocs[i].Score);
                o.WriteLine("url    : " + d.Get("url"));
                o.WriteLine("\ttitle  : " + d.Get("title"));
                if (summary != null)
                {
                    o.WriteLine("\tsummary: " + d.Get("summary"));
                }
                o.WriteLine();
            }
        }