public void TestThreadSafeForIndexReader() { using var light = new LucenePoolLight(TempIndexDir); light.BuildIndex(new[] { GetDocument(new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }), GetDocument(new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }), GetDocument(new CodeSource { FileName = "Dummy File 2", FileExtension = "xml", FilePath = @"C:\Dummy File.xml", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }) }, true, true, true); var taskList = new List <Task>(); var taskNumber = 3; // set to larger one for real test var addDocumentCount = 10; // set to larger one for real test for (var i = 0; i < taskNumber; i++) { taskList.Add(Task.Run(() => { for (var j = 0; j < addDocumentCount; j++) { if (j % 4 == 0) { light.BuildIndex(new[] { GetDocument(new CodeSource { FileName = $"Dummy File 1 {i} {j}", FileExtension = "cs", FilePath = $@"C:\Dummy File 1 {i} {j}.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }) }, true, true, true); } light.Search(new MatchAllDocsQuery(), int.MaxValue); light.Search(new MatchAllDocsQuery(), int.MaxValue); } })); } Assert.DoesNotThrowAsync(async() => await Task.WhenAll(taskList)); }
public static CodeWord[] SearchWord(this LucenePoolLight lucenePool, Query searchQuery, int maxResults = int.MaxValue) { return(lucenePool.Search(searchQuery, maxResults).Select(u => new CodeWord { Word = u.Get(nameof(CodeWord.Word)), WordLower = u.Get(nameof(CodeWord.WordLower)), }).ToArray()); }
public void TestGetQueryParser() { using var light = new LucenePoolLight(TempIndexDir); var parserA = LucenePoolLight.GetQueryParser(); var parserB = LucenePoolLight.GetQueryParser(); Assert.AreNotEqual(parserA, parserB); Assert.AreEqual(parserA.Analyzer, parserB.Analyzer); Assert.IsTrue(parserA.Analyzer is PerFieldAnalyzerWrapper); }
public void TestSearchWithSpecificFields() { using var light = new LucenePoolLight(TempIndexDir); light.BuildIndex(new[] { GetDocument(new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }), GetDocument(new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }) }, true, true, true); var documents = light.SearchWithSpecificFields(new TermQuery(new Term(nameof(CodeSource.FileName), "2")), int.MaxValue, nameof(CodeSource.FileName), nameof(CodeSource.FileExtension)); Assert.AreEqual(1, documents.Length); Assert.AreEqual("Dummy File 2", documents[0].Get(nameof(CodeSource.FileName))); Assert.AreEqual("cs", documents[0].Get(nameof(CodeSource.FileExtension))); Assert.IsNull(documents[0].Get(nameof(CodeSource.Content))); documents = light.SearchWithSpecificFields(new TermQuery(new Term(nameof(CodeSource.FileName), "2")), int.MaxValue, nameof(CodeSource.Content)); Assert.AreEqual(1, documents.Length); Assert.IsNull(documents[0].Get(nameof(CodeSource.FileName))); Assert.IsNull(documents[0].Get(nameof(CodeSource.FileExtension))); Assert.AreEqual("Test Content" + Environment.NewLine + "A New Line For Test", documents[0].Get(nameof(CodeSource.Content))); documents = light.SearchWithSpecificFields(new MatchAllDocsQuery(), int.MaxValue, nameof(CodeSource.FileName)); Assert.AreEqual(2, documents.Length); CollectionAssert.AreEquivalent(new[] { "Dummy File 1", "Dummy File 2" }, documents.Select(u => u.Get(nameof(CodeSource.FileName)))); }
public void TestUpdateIndexWithRawDocuments() { using var light = new LucenePoolLight(TempIndexDir); light.BuildIndex(new[] { GetDocument(new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }), GetDocument(new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }) }, true, true, true); light.UpdateIndex(new Term(nameof(CodeSource.FileName), "1"), GetDocument(new CodeSource { FileName = "Dummy File New 1", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "New Content" }), out var rawDocuments); Assert.AreEqual(1, rawDocuments.Length); Assert.AreEqual("Test Content" + Environment.NewLine + "A New Line For Test", rawDocuments[0].Get(nameof(CodeSource.Content)), "Still old value"); var documents = light.Search(new TermQuery(new Term(nameof(CodeSource.FileName), "1")), 10); Assert.AreEqual(1, documents.Length); Assert.AreEqual("New Content", documents[0].Get(nameof(CodeSource.Content)), "Content updated"); }
public void TestUpdateIndexCaseSensitive() { using var light = new LucenePoolLight(TempIndexDir); var wordA = "ABC"; light.UpdateIndex(new Term(nameof(CodeWord.Word), wordA), new Document { new StringField(nameof(CodeWord.Word), wordA, Field.Store.YES), new StringField(nameof(CodeWord.WordLower), wordA.ToLowerInvariant(), Field.Store.YES) }); var documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(1, documents.Length); var wordB = "Abc"; light.UpdateIndex(new Term(nameof(CodeWord.Word), wordB), new Document { new StringField(nameof(CodeWord.Word), wordB, Field.Store.YES), new StringField(nameof(CodeWord.WordLower), wordB.ToLowerInvariant(), Field.Store.YES) }); documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(2, documents.Length); light.UpdateIndex(new Term(nameof(CodeWord.Word), wordB), new Document { new StringField(nameof(CodeWord.Word), wordB, Field.Store.YES), new StringField(nameof(CodeWord.WordLower), wordB.ToLowerInvariant(), Field.Store.YES) }); documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(2, documents.Length); CollectionAssert.AreEquivalent(new[] { "ABC", "Abc" }, documents.Select(u => u.Get(nameof(CodeWord.Word)))); }
public static CodeSource[] SearchCode(this LucenePoolLight lucenePool, Query searchQuery, int maxResults = int.MaxValue) { return(lucenePool.Search(searchQuery, maxResults).Select(CodeIndexBuilder.GetCodeSourceFromDocument).ToArray()); }
public QueryGeneratorForTest() : base(LucenePoolLight.GetQueryParser(), LucenePoolLight.GetQueryParser(false)) { }
public void TestDeleteIndexWithDocumentsBeenDeleted() { using var light = new LucenePoolLight(TempIndexDir); light.BuildIndex(new[] { GetDocument(new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }), GetDocument(new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }) }, true, true, true); var documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(2, documents.Length); light.DeleteIndex(new Term(nameof(CodeSource.FileName), "2"), out documents); Assert.AreEqual(1, documents.Length); Assert.AreEqual("Dummy File 2", documents[0].Get(nameof(CodeSource.FileName))); documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(1, documents.Length); light.DeleteIndex(new Term(nameof(CodeSource.FileName), "1"), out documents); Assert.AreEqual(1, documents.Length); Assert.AreEqual("Dummy File 1", documents[0].Get(nameof(CodeSource.FileName))); documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(0, documents.Length); light.DeleteIndex(new Term(nameof(CodeSource.FileName), "1"), out documents); Assert.AreEqual(0, documents.Length); light.BuildIndex(new[] { GetDocument(new CodeSource { FileName = "Dummy File 1", FileExtension = "cs", FilePath = @"C:\Dummy File 1.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }), GetDocument(new CodeSource { FileName = "Dummy File 2", FileExtension = "cs", FilePath = @"C:\Dummy File 2.cs", Content = "Test Content" + Environment.NewLine + "A New Line For Test" }) }, true, true, true); documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(2, documents.Length); light.DeleteIndex(new TermQuery(new Term(nameof(CodeSource.FileName), "2")), out documents); Assert.AreEqual(1, documents.Length); Assert.AreEqual("Dummy File 2", documents[0].Get(nameof(CodeSource.FileName))); documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(1, documents.Length); light.DeleteIndex(new TermQuery(new Term(nameof(CodeSource.FileName), "1")), out documents); Assert.AreEqual(1, documents.Length); Assert.AreEqual("Dummy File 1", documents[0].Get(nameof(CodeSource.FileName))); documents = light.Search(new MatchAllDocsQuery(), int.MaxValue); Assert.AreEqual(0, documents.Length); }