public void TestBuildIndex() { BuildIndex(); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); var result1 = CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"Dummy File\""), 10); Assert.That(result1.Length, Is.EqualTo(1)); Assert.AreEqual(@"Dummy File", result1[0].Get(nameof(CodeSource.FileName))); Assert.AreEqual(@"cs", result1[0].Get(nameof(CodeSource.FileExtension))); Assert.AreEqual(@"C:\Dummy File.cs", result1[0].Get(nameof(CodeSource.FilePath))); Assert.AreEqual(@"C:\Dummy File.cs", result1[0].Get(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix)); Assert.AreEqual("Test Content" + Environment.NewLine + "A New Line For Test", result1[0].Get(nameof(CodeSource.Content))); Assert.AreEqual(new DateTime(2020, 1, 1).Ticks, result1[0].GetField(nameof(CodeSource.IndexDate)).GetInt64Value()); var generator = new QueryGenerator(); var result2 = CodeIndexSearcher.Search(Config.LuceneIndexForCode, generator.GetQueryFromStr("FFFF test"), 10); Assert.That(result2.Length, Is.EqualTo(2)); Assert.IsTrue(result2.Any(u => u.Get(nameof(CodeSource.FileName)) == "Dummy File")); Assert.IsTrue(result2.Any(u => u.Get(nameof(CodeSource.FileName)) == "A new File")); var result3 = CodeIndexSearcher.Search(Config.LuceneIndexForCode, generator.GetQueryFromStr("FFFF"), 10); Assert.That(result3.Length, Is.EqualTo(1)); Assert.IsTrue(result3.Any(u => u.Get(nameof(CodeSource.FileName)) == "A new File")); }
public void TestUpdateIndex() { BuildIndex(); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); var result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, @"D:\DDDD\A new Name.cs")), 10); Assert.That(result.Length, Is.EqualTo(1)); CodeIndexBuilder.UpdateIndex(Config.LuceneIndexForCode, new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, @"d:\dddd\a new name.cs"), CodeIndexBuilder.GetDocumentFromSource(new CodeSource() { Content = "AAA", FileExtension = "CCC", FilePath = "BBB", FileName = "DDD", IndexDate = new DateTime(1999, 12, 31), LastWriteTimeUtc = new DateTime(2000, 1, 1) })); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, @"d:\dddd\a new name.cs")), 10); Assert.That(result.Length, Is.EqualTo(0)); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FilePath) + Constants.NoneTokenizeFieldSuffix, "BBB")), 10); Assert.That(result.Length, Is.EqualTo(1)); Assert.AreEqual(@"DDD", result[0].Get(nameof(CodeSource.FileName))); Assert.AreEqual(@"CCC", result[0].Get(nameof(CodeSource.FileExtension))); Assert.AreEqual(@"BBB", result[0].Get(nameof(CodeSource.FilePath))); Assert.AreEqual("AAA", result[0].Get(nameof(CodeSource.Content))); Assert.AreEqual(new DateTime(1999, 12, 31).Ticks, result[0].GetField(nameof(CodeSource.IndexDate)).GetInt64Value()); Assert.AreEqual(new DateTime(2000, 1, 1).Ticks, result[0].GetField(nameof(CodeSource.LastWriteTimeUtc)).GetInt64Value()); }
public void TestSearch_ReaderFromWriter() { CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }, new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }, new CodeSource { FileName = "Dummy File 2", FileExtension = "xml", FilePath = @"C:\Dummy File.xml", Content = "Test Content" + Environment.NewLine + "A New Line For Test" } }); var results = CodeIndexSearcher.Search(Config.LuceneIndexForCode, new TermQuery(new Term(nameof(CodeSource.FileExtension), "xml")), 10); Assert.That(results.Length, Is.EqualTo(1)); }
public void TestDeleteAllIndex() { BuildIndex(); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); Assert.AreEqual(1, CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"Dummy File\""), 10).Length); Assert.AreEqual(1, CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"A new File\""), 10).Length); CodeIndexBuilder.DeleteAllIndex(Config); Assert.AreEqual(0, CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"Dummy File\""), 10).Length); Assert.AreEqual(0, CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"A new File\""), 10).Length); }
public void TestDeleteIndex() { BuildIndex(); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); var generator = new QueryGenerator(); var result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, generator.GetQueryFromStr("FFFF test"), 10); Assert.That(result.Length, Is.EqualTo(2)); CodeIndexBuilder.DeleteIndex(Config.LuceneIndexForCode, new Term(nameof(CodeSource.FileExtension), "xml")); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, generator.GetQueryFromStr("FFFF test"), 10); Assert.That(result.Length, Is.EqualTo(1)); CodeIndexBuilder.DeleteIndex(Config.LuceneIndexForCode, generator.GetQueryFromStr("Test")); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, generator.GetQueryFromStr("FFFF test"), 10); Assert.That(result.Length, Is.EqualTo(0)); }
void FileRenamed(string oldFullPath, string fullPath, PendingRetrySource pendingRetrySource = null) { try { if (IsFile(fullPath)) { var fileInfo = new FileInfo(fullPath); try { if (fileInfo.Exists) { var content = FilesContentHelper.ReadAllText(fullPath); var document = CodeIndexBuilder.GetDocumentFromSource(CodeSource.GetCodeSource(fileInfo, content)); CodeIndexBuilder.UpdateIndex(config.LuceneIndexForCode, GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), oldFullPath), document); pendingChanges++; } } catch (IOException) { AddFileChangesToRetrySouce(fullPath, WatcherChangeTypes.Renamed, pendingRetrySource, oldFullPath); } } else if (IsDirectory(fullPath)) { // Rebuild All Sub Directory Index File, rename the index path var term = new PrefixQuery(GetNoneTokenizeFieldTerm(nameof(CodeSource.FilePath), oldFullPath)); var docs = CodeIndexSearcher.Search(config.LuceneIndexForCode, term, int.MaxValue); foreach (var doc in docs) { CodeIndexBuilder.UpdateCodeFilePath(doc, oldFullPath, fullPath); CodeIndexBuilder.UpdateIndex(config.LuceneIndexForCode, new Term(nameof(CodeSource.CodePK), doc.Get(nameof(CodeSource.CodePK))), doc); pendingChanges++; } } } catch (Exception ex) { log?.Error(ex.ToString()); } }
public void TestBuildIndex_DeleteOldIndexWithSamePath() { BuildIndex(); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); var result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"Dummy File\""), 10); Assert.AreEqual("Test Content" + Environment.NewLine + "A New Line For Test", result.Single().Get(nameof(CodeSource.Content))); CodeIndexBuilder.BuildIndex(Config, true, true, true, new[] { new CodeSource { FileName = "Dummy File New", FileExtension = "cs", FilePath = @"C:\Dummy File.cs", Content = "ABC", IndexDate = new DateTime(2020, 1, 1) } }); LucenePool.SaveResultsAndClearLucenePool(Config.LuceneIndexForCode); result = CodeIndexSearcher.Search(Config.LuceneIndexForCode, Generator.GetQueryFromStr($"{nameof(CodeSource.FileName)}:\"Dummy File New\""), 10); Assert.AreEqual("ABC", result.Single().Get(nameof(CodeSource.Content))); }