private static void Main(string[] args) { // Create a full text index with default settings var index = new FullTextIndex <string>(); // Index index.Index("A", "This is some text associated with A: fizz"); index.Index("B", "Some buzz text for B"); index.Index("C", "Text associated with C is both fizz and buzz"); // Search for text containing both Fizz *and* Buzz var results = index.Search("Fizz Buzz").ToList(); // Output: Items with both Fizz and Buzz: 1 Console.WriteLine($"Items with both Fizz and Buzz: {results.Count}"); // Search for text containing both Fizz *or* Buzz results = index.Search("Fizz | Buzz").ToList(); // Outputs: Items with Fizz or Buzz: 3 Console.WriteLine($"Items with Fizz or Buzz: {results.Count}"); }
public async Task ShouldRoundTripIndexStructure() { var serializer = new BinarySerializer <string>(); var fileName = Guid.NewGuid().ToString() + ".dat"; using (var stream = File.Open(fileName, FileMode.CreateNew)) { var stopwatch = Stopwatch.StartNew(); await serializer.SerializeAsync(this.index, stream, false); this.output.WriteLine($"Serialized in {stopwatch.ElapsedMilliseconds}ms"); stream.Length.Should().BeGreaterThan(4); var newIndex = new FullTextIndexBuilder <string>().Build(); stream.Position = 0; stopwatch.Restart(); await serializer.DeserializeAsync(newIndex, stream, false); this.output.WriteLine($"Deserialized in {stopwatch.ElapsedMilliseconds}ms"); newIndex.IdLookup.GetIndexedItems().Should().BeEquivalentTo(this.index.IdLookup.GetIndexedItems()); newIndex.Count.Should().Be(this.index.Count); newIndex.Root.ToString().Should().Be(this.index.Root.ToString()); var oldResults = index.Search("test").ToList(); var newResults = newIndex.Search("test").ToList(); oldResults.Should().NotBeEmpty(); newResults.Should().BeEquivalentTo(oldResults); } File.Delete(fileName); }
public void Can_Use_FullTextIndex_Directly() { // First we create an instance of FullTextIndex var index = new FullTextIndex(); // Then we add texts to the index index.Add(_apodArticles); // After adding we can Search for texts that match a Lucene query expression // See https://lucene.apache.org/core/2_9_4/queryparsersyntax.html for a reference on Lucene query syntax var result = index.Search("magellanic nebula visible in southern skies").ToList(); var expected = 10; var actual = result.Count > 0 ? result[0] : -1; Assert.AreEqual<int>(expected, actual); // Article #10 should come up on top result = index.Search("swift-tuttle comet").ToList(); expected = 0; actual = result.Count > 0 ? result[0] : -1; Assert.AreEqual<int>(expected, actual); // Article #0 should come up on top result = index.Search("china observation station in antartica").ToList(); expected = 14; actual = result.Count > 0 ? result[0] : -1; Assert.AreEqual<int>(expected, actual); // Article #14 should come up on top }