### Configure SearchEngine in C# Source: https://github.com/lofcz/infidex/blob/master/README.md Demonstrates the initialization of the `SearchEngine` class with various configuration options. It covers n-gram sizes, padding, normalization, tokenization, coverage, term limits, word matching (exact and fuzzy), and field weights. This setup is crucial for defining the search behavior and performance. ```csharp var engine = new SearchEngine( indexSizes: new[] { 2, 3 }, // Character n-gram sizes startPadSize: 2, // Pad start of words stopPadSize: 0, // Pad end of words enableCoverage: true, // Enable lexical matching textNormalizer: TextNormalizer.CreateDefault(), tokenizerSetup: TokenizerSetup.CreateDefault(), coverageSetup: CoverageSetup.CreateDefault(), stopTermLimit: 1_250_000, // Max terms before stop-word filtering wordMatcherSetup: new WordMatcherSetup { SupportLD1 = true, // Enable fuzzy matching SupportAffix = true, // Enable prefix/suffix MinimumWordSizeExact = 2, MaximumWordSizeExact = 50, MinimumWordSizeLD1 = 4, MaximumWordSizeLD1 = 20 }, fieldWeights: new[] { 1.0f, 2.0f } // Per-field weights ); ``` -------------------------------- ### SearchEngine.CreateDefault() - Initialize Default Search Engine Source: https://context7.com/lofcz/infidex/llms.txt This section details how to initialize a default Infidex search engine with optimized settings for indexing and searching. It includes an example of creating the engine, indexing simple text documents, and performing a search with typo tolerance. ```APIDOC ## Initialize Default Search Engine ### Description Creates a search engine with optimal default configuration including 2-gram and 3-gram indexing, coverage analysis enabled, fuzzy matching with Levenshtein distance ≤1, and multi-field support with configurable weights. ### Method Static Method ### Endpoint N/A (Local library method) ### Parameters None ### Request Example ```csharp using Infidex; using Infidex.Core; // Create default engine with optimal settings var engine = SearchEngine.CreateDefault(); // Index simple documents var documents = new[] { new Document(1L, "The quick brown fox jumps over the lazy dog"), new Document(2L, "A journey of a thousand miles begins with a single step"), new Document(3L, "To be or not to be that is the question") }; engine.IndexDocuments(documents); // Search with typo tolerance - "quik" still finds "quick" var results = engine.Search("quik fox", maxResults: 10); foreach (var result in results.Records) { var doc = engine.GetDocument(result.DocumentId); Console.WriteLine($"Score: {result.Score}, Text: {doc.IndexedText}"); } // Output: Matches document 1 despite the typo in "quik" ``` ### Response #### Success Response - **engine** (SearchEngine) - An initialized SearchEngine object. #### Response Example N/A (Object instantiation) ``` -------------------------------- ### Core C# Indexing and Searching Methods Source: https://github.com/lofcz/infidex/blob/master/README.md Provides examples of key methods for interacting with the `SearchEngine`. This includes methods for indexing documents asynchronously and synchronously, performing searches using text or a `Query` object, retrieving documents by key, and setting the document field schema. ```csharp // Indexing engine.IndexDocuments(IEnumerable docs) await engine.IndexDocumentsAsync(IEnumerable docs) // Searching SearchResult Search(string text, int maxResults = 10) SearchResult Search(Query query) // Document management Document? GetDocument(long documentKey) List GetDocuments(long documentKey) // All segments // Schema engine.DocumentFieldSchema = fields; ``` -------------------------------- ### Sorting Search Results (C#) Source: https://github.com/lofcz/infidex/blob/master/README.md Provides examples of how to sort search results in ascending or descending order based on specific fields like 'year' or 'rating'. This allows users to prioritize results according to their needs. ```csharp // Sort by year (descending) var query = new Query("thriller", maxResults: 20) { SortBy = fields.GetField("year"), SortAscending = false }; // Sort by rating (ascending) var query = new Query("comedy", maxResults: 20) { SortBy = fields.GetField("rating"), SortAscending = true }; ``` -------------------------------- ### Infiscript Filter Language Examples in C# Source: https://context7.com/lofcz/infidex/llms.txt Illustrates the capabilities of Infiscript for defining filter expressions in C#. This covers simple and complex boolean logic, string operations, range and list filtering, null checks, regular expressions, and ternary conditional expressions. It also shows how to compile filters to bytecode for performance optimization and reuse. ```csharp // Simple comparison filters var genreFilter = Filter.Parse("genre = 'Sci-Fi'"); var yearFilter = Filter.Parse("year >= 2000"); // Complex boolean expressions with grouping var complexFilter = Filter.Parse( "(genre = 'Fantasy' AND year >= 2000) OR (genre = 'Horror' AND year >= 1980)" ); // String operations var containsFilter = Filter.Parse("title CONTAINS 'matrix'"); var startsFilter = Filter.Parse("title STARTS WITH 'The'"); var endsFilter = Filter.Parse("title ENDS WITH 'End'"); var likeFilter = Filter.Parse("description LIKE '%dream%'"); // % is wildcard // Range and list operations var rangeFilter = Filter.Parse("year BETWEEN 2000 AND 2020"); var inFilter = Filter.Parse("genre IN ('Sci-Fi', 'Fantasy', 'Adventure')"); // Null checks var notNullFilter = Filter.Parse("director IS NOT NULL"); var nullFilter = Filter.Parse("tagline IS NULL"); // Regex matching var emailFilter = Filter.Parse("email MATCHES '^[\\w\\.-]+@[\\w\\.-]+\\.\\w+$"'); // Ternary expressions (conditional logic) var ageCategory = Filter.Parse("age >= 18 ? 'adult' : 'minor'"); var gradeFilter = Filter.Parse("score >= 90 ? 'A' : score >= 80 ? 'B' : 'C'"); // Compile to bytecode for reuse (performance optimization) var filter = Filter.Parse("genre = 'Sci-Fi' AND year >= 2000"); byte[] bytecode = filter.CompileToBytes(); // Save bytecode to disk File.WriteAllBytes("filter.bin", bytecode); // Load and reuse later var loadedFilter = Filter.FromBytecode(File.ReadAllBytes("filter.bin")); var query = new Query("space exploration", maxResults: 50) { CompiledFilterBytecode = bytecode // Use precompiled bytecode }; var results = engine.Search(query); Console.WriteLine($"Filtered results: {results.Records.Length}"); ``` -------------------------------- ### SearchEngine Configuration Source: https://github.com/lofcz/infidex/blob/master/README.md Demonstrates how to configure and initialize the SearchEngine with various parameters such as index sizes, padding, text normalization, tokenizer, coverage, stop term limit, word matching, and field weights. ```APIDOC ## SearchEngine Configuration ### Description This section details the configuration of the `SearchEngine` class, allowing for fine-tuning of indexing and searching behavior. ### Method Instantiation of `SearchEngine` class. ### Endpoint N/A (Code-based configuration) ### Parameters - **indexSizes** (int[]) - N/A - Character n-gram sizes for indexing. - **startPadSize** (int) - N/A - Size of padding applied to the start of words. - **stopPadSize** (int) - N/A - Size of padding applied to the end of words. - **enableCoverage** (bool) - N/A - Enables lexical matching for coverage. - **textNormalizer** (TextNormalizer) - N/A - Configuration for text normalization. - **tokenizerSetup** (TokenizerSetup) - N/A - Configuration for text tokenization. - **coverageSetup** (CoverageSetup) - N/A - Configuration for coverage matching. - **stopTermLimit** (int) - N/A - Maximum number of terms before stop-word filtering is applied. - **wordMatcherSetup** (WordMatcherSetup) - N/A - Configuration for word matching, including fuzzy matching and affix support. - **SupportLD1** (bool) - N/A - Enables Levenshtein distance 1 matching. - **SupportAffix** (bool) - N/A - Enables prefix and suffix matching. - **MinimumWordSizeExact** (int) - N/A - Minimum word size for exact matching. - **MaximumWordSizeExact** (int) - N/A - Maximum word size for exact matching. - **MinimumWordSizeLD1** (int) - N/A - Minimum word size for Levenshtein distance 1 matching. - **MaximumWordSizeLD1** (int) - N/A - Maximum word size for Levenshtein distance 1 matching. - **fieldWeights** (float[]) - N/A - Weights assigned to different fields for searching. ### Request Example ```csharp var engine = new SearchEngine( indexSizes: new[] { 2, 3 }, startPadSize: 2, stopPadSize: 0, enableCoverage: true, textNormalizer: TextNormalizer.CreateDefault(), tokenizerSetup: TokenizerSetup.CreateDefault(), coverageSetup: CoverageSetup.CreateDefault(), stopTermLimit: 1_250_000, wordMatcherSetup: new WordMatcherSetup { SupportLD1 = true, SupportAffix = true, MinimumWordSizeExact = 2, MaximumWordSizeExact = 50, MinimumWordSizeLD1 = 4, MaximumWordSizeLD1 = 20 }, fieldWeights: new[] { 1.0f, 2.0f } ); ``` ### Response N/A (This is a code snippet for object instantiation) ``` -------------------------------- ### Configure Custom Search Engine in C# Source: https://context7.com/lofcz/infidex/llms.txt Illustrates how to create a custom 'SearchEngine' with fine-tuned parameters for n-gram sizes, padding, normalization, coverage, stop-term limits, and word matching. This allows for specialized search behavior tailored to specific needs. ```csharp using Infidex.Tokenization; using Infidex.Coverage; using Infidex.WordMatcher; // Assuming SearchEngine and other necessary types are available // Create custom configuration var customEngine = new SearchEngine( indexSizes: new[] { 2, 3, 4 }, // Use 2, 3, and 4-grams startPadSize: 2, // Pad start of words stopPadSize: 1, // Pad end of words enableCoverage: true, // Enable lexical matching textNormalizer: TextNormalizer.CreateDefault(), tokenizerSetup: TokenizerSetup.CreateDefault(), coverageSetup: new CoverageSetup { EnableExactMatch = true, EnableFuzzyMatch = true, EnablePrefixMatch = true, EnableSuffixMatch = true, EnableLCS = true, FuzzyThreshold = 0.25f // Max normalized edit distance }, stopTermLimit: 1_000_000, // Apply stop-word filtering above 1M terms wordMatcherSetup: new WordMatcherSetup { SupportLD1 = true, // Enable Levenshtein distance 1 SupportAffix = true, // Enable prefix/suffix matching MinimumWordSizeExact = 2, MaximumWordSizeExact = 50, MinimumWordSizeLD1 = 4, // Fuzzy match words 4+ chars MaximumWordSizeLD1 = 20 }, fieldWeights: new[] { 1.0f, 1.5f, 2.0f } // Custom field weight multipliers ); // Index and search with custom configuration // Assuming 'documents' is an array of Document objects // customEngine.IndexDocuments(documents); // var results = customEngine.Search("specialized query"); // Console.WriteLine($"Engine indexed {documents.Length} documents"); // Console.WriteLine($"Found {results.Records.Length} matches"); // Get engine statistics // var stats = customEngine.GetStatistics(); // Console.WriteLine($"Engine stats: {stats}"); // Monitor indexing progress // customEngine.ProgressChanged += (sender, progress) => // { // Console.WriteLine($"Indexing progress: {progress}%"); // }; ``` -------------------------------- ### Create Default Search Engine (C#) Source: https://github.com/lofcz/infidex/blob/master/README.md Demonstrates the creation of a default `SearchEngine` instance, which is recommended for a balanced approach to speed and accuracy. This configuration uses multi-size n-grams and enables all coverage algorithms. ```csharp var engine = SearchEngine.CreateDefault(); // - Multi-size n-grams: [2, 3] // - Coverage: Enabled (all 5 algorithms) // - Fuzzy matching: LD ≤ 1 // - Balanced for speed and accuracy ``` -------------------------------- ### Create Default Search Engine and Index Documents Source: https://context7.com/lofcz/infidex/llms.txt Initializes a default Infidex search engine with optimal settings for 2-gram and 3-gram indexing, coverage analysis, fuzzy matching, and multi-field support. It then indexes simple text documents and performs a search with typo tolerance, demonstrating how to retrieve documents and their scores. ```csharp using Infidex; using Infidex.Core; // Create default engine with optimal settings var engine = SearchEngine.CreateDefault(); // Index simple documents var documents = new[] { new Document(1L, "The quick brown fox jumps over the lazy dog"), new Document(2L, "A journey of a thousand miles begins with a single step"), new Document(3L, "To be or not to be that is the question") }; engine.IndexDocuments(documents); // Search with typo tolerance - "quik" still finds "quick" var results = engine.Search("quik fox", maxResults: 10); foreach (var result in results.Records) { var doc = engine.GetDocument(result.DocumentId); Console.WriteLine($"Score: {result.Score}, Text: {doc.IndexedText}"); } // Output: Matches document 1 despite the typo in "quik" ``` -------------------------------- ### Configure Advanced Search Queries in C# Source: https://context7.com/lofcz/infidex/llms.txt Demonstrates how to build and execute complex search queries in C# using the Infidex API. This includes setting search terms, configuring features like faceting and boosting, applying filters, sorting results, and managing timeouts. It also shows how to retrieve and process the search results. ```csharp using Infidex.Api; // Build advanced query var query = new Query("science fiction", maxResults: 20) { EnableCoverage = true, // Enable fuzzy matching EnableFacets = true, // Calculate facet aggregations EnableBoost = true, // Apply boost rules CoverageDepth = 500, // Top 500 TF-IDF candidates for coverage RemoveDuplicates = true, // Consolidate document segments TimeOutLimitMilliseconds = 1000, // Filter to recent highly-rated content Filter = Filter.Parse("year >= 2000 AND rating > 7.0"), // Boost recent and highly-rated content Boosts = new[] { new Boost(Filter.Parse("year >= 2020"), BoostStrength.Large), // +20 score new Boost(Filter.Parse("rating >= 8.5"), BoostStrength.Medium) // +10 score }, // Sort by year descending SortBy = fields.GetField("year"), SortAscending = false }; var results = engine.Search(query); Console.WriteLine($"Found {results.Records.Length} matches in {results.ExecutionTimeMs}ms"); Console.WriteLine($"Query timed out: {results.DidTimeOut}"); // Process results foreach (var record in results.Records) { var doc = engine.GetDocument(record.DocumentId); var title = doc.Fields.GetField("title")?.Value; var year = doc.Fields.GetField("year")?.Value; Console.WriteLine($"[{record.Score}] {title} ({year})"); } ``` -------------------------------- ### Create Minimal Search Engine (C#) Source: https://github.com/lofcz/infidex/blob/master/README.md Shows how to create a minimal `SearchEngine` for prioritizing speed over accuracy. This configuration uses a single n-gram size and disables coverage analysis, resulting in faster but potentially less precise search results. ```csharp var engine = SearchEngine.CreateMinimal(); // - Single n-gram: [3] // - Coverage: Disabled // - Faster but less accurate ``` -------------------------------- ### Run Tests using dotnet test Source: https://github.com/lofcz/infidex/blob/master/README.md This command executes the comprehensive test suite for the project, which includes over 300 tests. Ensure you are in the project directory with the test project configured to run this command. ```bash dotnet test ``` -------------------------------- ### Index and Search Segmented Documents in C# Source: https://context7.com/lofcz/infidex/llms.txt Demonstrates how to index large documents by splitting them into segments, each with a unique SegmentNumber. It also shows how to search these segments and consolidate results to return only one match per original document. ```csharp using System; using Infidex.Engine; using Infidex.Fields; // Index a book split into chapters/segments long bookId = 100L; var bookSegments = new[] { new Document(bookId, 0, new DocumentFields { ["content"] = new Field("content", "Chapter 1: The hero begins his journey through the dark forest.", Weight.Med) { Indexable = true } }, "book:Epic Tale, chapter:1"), new Document(bookId, 1, new DocumentFields { ["content"] = new Field("content", "The forest was full of dangers and mysterious creatures.", Weight.Med) { Indexable = true } }, "book:Epic Tale, chapter:1"), new Document(bookId, 2, new DocumentFields { ["content"] = new Field("content", "Chapter 2: The hero discovers an ancient temple in the mountains.", Weight.Med) { Indexable = true } }, "book:Epic Tale, chapter:2"), new Document(bookId, 3, new DocumentFields { ["content"] = new Field("content", "Inside the temple, ancient scrolls revealed the prophecy.", Weight.Med) { Indexable = true } }, "book:Epic Tale, chapter:2") }; // Assuming 'engine' is an initialized SearchEngine instance // engine.IndexDocuments(bookSegments); // Search returns individual segments var query = new Query("ancient temple", maxResults: 10); // var results = engine.Search(query); // foreach (var record in results.Records) // { // var doc = engine.GetDocument(record.DocumentId); // Console.WriteLine($"Book {doc.DocumentKey}, Segment {doc.SegmentNumber}:"); // Console.WriteLine($" {doc.IndexedText}"); // Console.WriteLine($" Metadata: {doc.DocumentClientInformation}"); // } // Consolidate segments (remove duplicates by DocumentKey) var consolidatedQuery = new Query("ancient temple", maxResults: 10) { RemoveDuplicates = true // Only return one result per DocumentKey }; // var consolidatedResults = engine.Search(consolidatedQuery); // Console.WriteLine($"Segments: {results.Records.Length}, Consolidated: {consolidatedResults.Records.Length}"); ``` -------------------------------- ### Async Indexing with Cancellation and Progress Reporting (C#) Source: https://context7.com/lofcz/infidex/llms.txt Indexes documents asynchronously, providing progress updates and supporting cancellation. This is crucial for maintaining UI responsiveness during large indexing operations. It uses CancellationTokenSource for cancellation and IProgress for progress reporting. ```csharp var cts = new CancellationTokenSource(); cts.CancelAfter(TimeSpan.FromSeconds(30)); // Auto-cancel after 30 seconds var progress = new Progress(percent => { Console.WriteLine($"Indexing: {percent}%"); }); try { await engine.IndexDocumentsAsync( largeDocumentCollection, progress, cts.Token ); Console.WriteLine("Indexing completed successfully"); } catch (OperationCanceledException) { Console.WriteLine("Indexing was cancelled"); } catch (Exception ex) { Console.WriteLine($"Indexing failed: {ex.Message}"); } if (engine.Status == SearchEngineStatus.Ready) { var results = engine.Search("query text"); Console.WriteLine($"Search returned {results.Records.Length} results"); } ``` -------------------------------- ### Compile and Load Filter Bytecode (C#) Source: https://github.com/lofcz/infidex/blob/master/README.md Demonstrates compiling an Infiscript filter to portable bytecode for efficient storage and later retrieval. This allows for 'compile once, use many times' scenarios and serialization of filters. ```csharp var filter = Filter.Parse("genre = 'Sci-Fi' AND year >= 2000"); var bytecode = filter.CompileToBytes(); // Save to disk File.WriteAllBytes("filter.bin", bytecode); // Load and use later var loaded = Filter.FromBytecode(File.ReadAllBytes("filter.bin")); var query = new Query("space") { CompiledFilterBytecode = bytecode }; ``` -------------------------------- ### Apply Document Boosting in C# Source: https://context7.com/lofcz/infidex/llms.txt Allows conditional score boosts to be applied to documents that match specific filter criteria. Multiple boost rules, each with predefined strengths (Small, Medium, Large, Extreme), can be combined to fine-tune document relevance. This feature is enabled by setting `EnableBoost` to true and providing an array of `Boost` objects to the `Boosts` property. ```csharp // Define boost rules var recentBoost = new Boost( Filter.Parse("year >= 2020"), BoostStrength.Large // +20 to score ); var highRatingBoost = new Boost( Filter.Parse("rating >= 8.0"), BoostStrength.Medium // +10 to score ); var popularBoost = new Boost( Filter.Parse("views >= 10000"), BoostStrength.Small // +5 to score ); var premiumBoost = new Boost( Filter.Parse("isPremium = true"), BoostStrength.Extreme // +40 to score ); // Apply all boosts to query var query = new Query("action thriller", maxResults: 20) { EnableBoost = true, Boosts = new[] { recentBoost, highRatingBoost, popularBoost, premiumBoost } }; var results = engine.Search(query); Console.WriteLine($"Max possible boost: {query.MaxBoost}"); // Sum of all boosts Console.WriteLine($"Documents boosted: {query.DocumentsBoosted}"); // Results are re-ranked with boosted scores foreach (var record in results.Records.Take(5)) { var doc = engine.GetDocument(record.DocumentId); var title = doc.Fields.GetField("title")?.Value; var year = doc.Fields.GetField("year")?.Value; var rating = doc.Fields.GetField("rating")?.Value; Console.WriteLine($"[{record.Score}] {title} - {year} (Rating: {rating})"); } // Premium, recent, highly-rated content appears first ``` -------------------------------- ### Basic Document Indexing and Searching in Infidex (.NET) Source: https://github.com/lofcz/infidex/blob/master/README.md Demonstrates the fundamental usage of Infidex for indexing plain text documents and performing basic searches. It shows how to create a search engine instance, index a collection of documents, and retrieve results even with typos in the query. This is suitable for simple text search requirements. ```csharp using Infidex; using Infidex.Core; // Create search engine var engine = SearchEngine.CreateDefault(); // Index documents var documents = new[] { new Document(1L, "The quick brown fox jumps over the lazy dog"), new Document(2L, "A journey of a thousand miles begins with a single step"), new Document(3L, "To be or not to be that is the question") }; engine.IndexDocuments(documents); // Search with typos - still finds matches! var results = engine.Search("quik fox", maxResults: 10); foreach (var result in results.Results) { Console.WriteLine($"Doc {result.DocumentId}: Score {result.Score}"); } ``` -------------------------------- ### Indexing Documents Source: https://github.com/lofcz/infidex/blob/master/README.md Methods for indexing documents into the search engine, both synchronously and asynchronously. ```APIDOC ## Indexing Documents ### Description These methods are used to add `Document` objects to the search engine's index. ### Method - `IndexDocuments(IEnumerable docs)` - `IndexDocumentsAsync(IEnumerable docs)` ### Endpoint N/A (Code-based operation) ### Parameters - **docs** (`IEnumerable`) - Required - A collection of `Document` objects to be indexed. ### Request Example ```csharp // Synchronous indexing engine.IndexDocuments(documentsToIndex); // Asynchronous indexing await engine.IndexDocumentsAsync(documentsToIndex); ``` ### Response N/A (Indexing operations typically do not return specific data upon success, but may throw exceptions on failure.) ``` -------------------------------- ### Document Boosting for Relevance (C#) Source: https://github.com/lofcz/infidex/blob/master/README.md Illustrates how to increase the relevance scores of specific documents based on defined criteria using Boost objects. It combines multiple boost rules to enhance search results for recent and highly-rated content. ```csharp // Boost recent movies var recentBoost = new Boost( Filter.Parse("year >= 2020"), BoostStrength.Large // +20 to score ); // Boost highly-rated content var ratingBoost = new Boost( Filter.Parse("rating >= 8.0"), BoostStrength.Medium // +10 to score ); var query = new Query("action movie", maxResults: 20) { EnableBoost = true, Boosts = new[] { recentBoost, ratingBoost } }; var results = engine.Search(query); ``` -------------------------------- ### Retrieving Documents by ID and Key (C#) Source: https://context7.com/lofcz/infidex/llms.txt Retrieves full document objects from the index using their unique DocumentId or DocumentKey. This allows access to all document fields, metadata, and segmented content, useful for displaying detailed information after a search. ```csharp var query = new Query("science fiction", maxResults: 10); var results = engine.Search(query); foreach (var record in results.Records) { var doc = engine.GetDocument(record.DocumentId); if (doc != null) { var title = doc.Fields.GetField("title")?.Value; var description = doc.Fields.GetField("description")?.Value; var genre = doc.Fields.GetField("genre")?.Value; var year = doc.Fields.GetField("year")?.Value; Console.WriteLine($"Score: {record.Score}"); Console.WriteLine($"Title: {title}"); Console.WriteLine($"Description: {description}"); Console.WriteLine($"Genre: {genre}, Year: {year}"); if (doc.DocumentClientInformation != null) { Console.WriteLine($"Metadata: {doc.DocumentClientInformation}"); } Console.WriteLine($"DocumentKey: {doc.DocumentKey}, Segment: {doc.SegmentNumber}"); Console.WriteLine(); } } long documentKey = 100L; var allSegments = engine.GetDocuments(documentKey); Console.WriteLine($"Document {documentKey} has {allSegments.Count} segments:"); foreach (var segment in allSegments) { Console.WriteLine($" Segment {segment.SegmentNumber}: {segment.IndexedText.Substring(0, 50)}..."); } ``` -------------------------------- ### Multi-Field Search Configuration and Indexing in Infidex (.NET) Source: https://github.com/lofcz/infidex/blob/master/README.md Illustrates how to configure Infidex for multi-field searching by defining searchable, facetable, and filterable fields with specified weights. It then shows how to index structured documents with these fields, enabling more sophisticated search queries based on specific document attributes. ```csharp using Infidex.Api; // Define fields with weights var fields = new DocumentFields(); fields.AddSearchableField("title", weight: 2.0f); // Title is more important fields.AddSearchableField("description", weight: 1.0f); fields.AddFacetableField("genre"); fields.AddFilterableField("year"); engine.DocumentFieldSchema = fields; // Index structured documents var movies = new[] { new Document(1L, new Dictionary { ["title"] = "The Matrix", ["description"] = "A computer hacker learns about the true nature of reality", ["genre"] = "Sci-Fi", ["year"] = 1999 }), new Document(2L, new Dictionary { ["title"] = "Inception", ["description"] = "A thief who steals corporate secrets through dream-sharing", ["genre"] = "Sci-Fi", ["year"] = 2010 }) }; engine.IndexDocuments(movies); ``` -------------------------------- ### Faceted Search and Aggregations (C#) Source: https://github.com/lofcz/infidex/blob/master/README.md Shows how to enable faceted search in a query to build dynamic filters and navigate data. It retrieves facet counts for different fields, displaying the values and the number of documents associated with each value. ```csharp var query = new Query("science fiction", maxResults: 50) { EnableFacets = true }; var results = engine.Search(query); // Get facet counts if (results.Facets != null) { foreach (var (fieldName, values) in results.Facets) { Console.WriteLine($"\n{fieldName}:"); foreach (var (value, count) in values) { Console.WriteLine($" {value}: {count} documents"); } } } ``` -------------------------------- ### Configure Multi-Field Search with Weighted Fields Source: https://context7.com/lofcz/infidex/llms.txt Demonstrates how to configure a multi-field search schema in Infidex, assigning different weights to fields like 'title', 'description', 'genre', and 'year' to influence search relevance. It then indexes structured movie documents and performs a query, showing how field weights affect search results. ```csharp using Infidex.Api; // Define schema with weighted fields var fields = new DocumentFields(); fields.AddField(new Field("title", null, Weight.High) { Indexable = true }); fields.AddField(new Field("description", null, Weight.Med) { Indexable = true }); fields.AddField(new Field("genre", null, Weight.Low) { Facetable = true, Filterable = true }); fields.AddField(new Field("year", null, Weight.Low) { Filterable = true, Sortable = true }); engine.DocumentFieldSchema = fields; // Index structured documents var movies = new[] { new Document(1L, new DocumentFields { ["title"] = new Field("title", "The Matrix", Weight.High) { Indexable = true }, ["description"] = new Field("description", "A computer hacker learns about reality", Weight.Med) { Indexable = true }, ["genre"] = new Field("genre", "Sci-Fi", Weight.Low) { Facetable = true, Filterable = true }, ["year"] = new Field("year", 1999, Weight.Low) { Filterable = true, Sortable = true } }), new Document(2L, new DocumentFields { ["title"] = new Field("title", "Inception", Weight.High) { Indexable = true }, ["description"] = new Field("description", "A thief steals secrets through dreams", Weight.Med) { Indexable = true }, ["genre"] = new Field("genre", "Sci-Fi", Weight.Low) { Facetable = true, Filterable = true }, ["year"] = new Field("year", 2010, Weight.Low) { Filterable = true, Sortable = true } }) }; engine.IndexDocuments(movies); // Search across all searchable fields var query = new Query("computer hacker", maxResults: 10); var results = engine.Search(query); // Title matches are weighted higher due to Weight.High setting ``` -------------------------------- ### Document Boosting Source: https://context7.com/lofcz/infidex/llms.txt Apply conditional score boosts to documents that match specific filters. Multiple boosts can be stacked, with predefined strengths for consistent relevance tuning. ```APIDOC ## Document Boosting ### Description Apply conditional score boosts to documents matching specific filters. Multiple boosts can be stacked, with predefined strengths (Small=+5, Medium=+10, Large=+20, Extreme=+40) for consistent relevance tuning. ### Method SEARCH (Implicit within the Query object) ### Endpoint N/A (Operates on the search engine instance) ### Parameters #### Query Parameters - **EnableBoost** (boolean) - Optional - Set to `true` to enable boost calculations. - **Boosts** (Array of Boost objects) - Optional - An array of boost rules to apply to the query. ### Request Example ```csharp // Define boost rules var recentBoost = new Boost( Filter.Parse("year >= 2020"), BoostStrength.Large // +20 to score ); var highRatingBoost = new Boost( Filter.Parse("rating >= 8.0"), BoostStrength.Medium // +10 to score ); // Apply all boosts to query var query = new Query("action thriller", maxResults: 20) { EnableBoost = true, Boosts = new[] { recentBoost, highRatingBoost } }; var results = engine.Search(query); ``` ### Response #### Success Response (200) - **Records** (Array of Record objects) - Search results, re-ranked based on boosted scores. #### Response Example ```json [ { "Score": 15.7, "DocumentId": "doc123" }, { "Score": 12.1, "DocumentId": "doc456" } ] ``` ``` -------------------------------- ### Multi-Field Search with Weighted Fields Source: https://context7.com/lofcz/infidex/llms.txt This section explains how to configure multi-field search capabilities in Infidex. It covers defining a document schema with fields, assigning weights to control relevance, and indexing structured documents for searching across multiple attributes. ```APIDOC ## Multi-Field Search with Weighted Fields ### Description Define document structure with multiple fields, each having specific weights (High=1.5x, Med=1.25x, Low=1.0x) to control their influence on search relevance. Fields can be marked as searchable, filterable, facetable, or sortable. ### Method N/A (Configuration and Indexing) ### Endpoint N/A (Local library configuration) ### Parameters #### Request Body (DocumentFields Schema Definition) - **Field Name** (string) - The name of the field. - **Field Data** (object) - The data associated with the field. - **Weight** (Weight enum: High, Med, Low) - The relevance weight for the field. - **Indexable** (boolean) - Whether the field should be indexed for searching. - **Filterable** (boolean) - Whether the field can be used for filtering. - **Facetable** (boolean) - Whether the field can be used for faceting. - **Sortable** (boolean) - Whether the field can be used for sorting. ### Request Example ```csharp using Infidex.Api; // Define schema with weighted fields var fields = new DocumentFields(); fields.AddField(new Field("title", null, Weight.High) { Indexable = true }); fields.AddField(new Field("description", null, Weight.Med) { Indexable = true }); fields.AddField(new Field("genre", null, Weight.Low) { Facetable = true, Filterable = true }); fields.AddField(new Field("year", null, Weight.Low) { Filterable = true, Sortable = true }); engine.DocumentFieldSchema = fields; // Index structured documents var movies = new[] { new Document(1L, new DocumentFields { ["title"] = new Field("title", "The Matrix", Weight.High) { Indexable = true }, ["description"] = new Field("description", "A computer hacker learns about reality", Weight.Med) { Indexable = true }, ["genre"] = new Field("genre", "Sci-Fi", Weight.Low) { Facetable = true, Filterable = true }, ["year"] = new Field("year", 1999, Weight.Low) { Filterable = true, Sortable = true } }), new Document(2L, new DocumentFields { ["title"] = new Field("title", "Inception", Weight.High) { Indexable = true }, ["description"] = new Field("description", "A thief steals secrets through dreams", Weight.Med) { Indexable = true }, ["genre"] = new Field("genre", "Sci-Fi", Weight.Low) { Facetable = true, Filterable = true }, ["year"] = new Field("year", 2010, Weight.Low) { Filterable = true, Sortable = true } }) }; engine.IndexDocuments(movies); // Search across all searchable fields var query = new Query("computer hacker", maxResults: 10); var results = engine.Search(query); // Title matches are weighted higher due to Weight.High setting ``` ### Response #### Success Response - **results** (SearchResults) - The search results object containing matching documents. #### Response Example N/A (Illustrative code example) ``` -------------------------------- ### Schema Management Source: https://github.com/lofcz/infidex/blob/master/README.md Allows setting the document field schema for the search engine. ```APIDOC ## Schema Management ### Description This operation allows you to define the schema for the fields within your documents, which influences how data is indexed and searched. ### Method `engine.DocumentFieldSchema = fields;` ### Endpoint N/A (Code-based operation) ### Parameters - **fields** (`DocumentFields`) - Required - An object defining the schema for document fields. ### Request Example ```csharp var fields = DocumentFields.CreateDefault(); // Or a custom configuration fields.Add("title", FieldType.Text, FieldOptions.Tokenized | FieldOptions.Searchable); fields.Add("author", FieldType.Keyword, FieldOptions.Searchable); engine.DocumentFieldSchema = fields; ``` ### Response N/A (Schema modification is typically an in-process configuration change and does not return specific data upon success.) ``` -------------------------------- ### Enable Facets for Dynamic Filtering in C# Source: https://context7.com/lofcz/infidex/llms.txt Enables facet calculation to generate value counts for searchable fields, allowing users to refine search results using dynamic filters. This feature helps visualize the distribution of results across different field values. It requires a `Query` object with `EnableFacets` set to true and optionally accepts a pre-applied `Filter`. ```csharp var query = new Query("science fiction", maxResults: 50) { EnableFacets = true, Filter = Filter.Parse("year >= 1990") // Optionally pre-filter }; var results = engine.Search(query); // Examine facet distributions if (results.Facets != null) { foreach (var (fieldName, values) in results.Facets) { Console.WriteLine($"\n{fieldName}:"); foreach (var (value, count) in values.OrderByDescending(x => x.Value)) { Console.WriteLine($" {value}: {count} documents"); } } } // Use facet values to create refined search var refinedQuery = new Query("science fiction", maxResults: 20) { Filter = Filter.Parse("genre = 'Sci-Fi' AND year = 2020"), EnableFacets = true }; var refinedResults = engine.Search(refinedQuery); Console.WriteLine($"Refined to {refinedResults.Records.Length} results"); ``` -------------------------------- ### Searching Documents Source: https://github.com/lofcz/infidex/blob/master/README.md Methods for performing searches within the indexed documents, either by a simple text query or a complex `Query` object. ```APIDOC ## Searching Documents ### Description Methods to execute search queries against the indexed documents. You can search using a simple text string or a more detailed `Query` object. ### Method - `Search(string text, int maxResults = 10)` - `Search(Query query)` ### Endpoint N/A (Code-based operation) ### Parameters - **text** (`string`) - Required - The text to search for (used in the first overload). - **maxResults** (`int`) - Optional - The maximum number of search results to return (defaults to 10). - **query** (`Query`) - Required - A `Query` object specifying search criteria (used in the second overload). ### Request Example ```csharp // Search by text var resultsByText = engine.Search("example search term"); // Search with max results var top5Results = engine.Search("another term", 5); // Search using a Query object var complexQuery = new Query { /* ... configure query ... */ }; var resultsByQuery = engine.Search(complexQuery); ``` ### Response #### Success Response (200) - **SearchResult** - An object containing the search results. #### Response Example ```json { "results": [ { "documentId": 1, "score": 0.95 }, { "documentId": 5, "score": 0.88 } ], "totalHits": 150 } ``` ``` -------------------------------- ### Document Management Source: https://github.com/lofcz/infidex/blob/master/README.md Methods for retrieving individual documents or all segments of a document based on its key. ```APIDOC ## Document Management ### Description Provides functionality to retrieve documents from the index using their unique identifiers. ### Method - `GetDocument(long documentKey)` - `GetDocuments(long documentKey)` ### Endpoint N/A (Code-based operation) ### Parameters - **documentKey** (`long`) - Required - The unique identifier of the document to retrieve. ### Request Example ```csharp // Get a single document Document? doc = engine.GetDocument(123); // Get all segments of a document List allDocSegments = engine.GetDocuments(456); ``` ### Response #### Success Response (200) - **Document?** - A single `Document` object if found, or null. - **List** - A list of `Document` objects representing all segments of the document. #### Response Example ```json { "documentKey": 123, "fields": { "title": "Sample Document Title", "content": "This is the content of the sample document." } } ``` ``` -------------------------------- ### Sorting Results Source: https://context7.com/lofcz/infidex/llms.txt Override relevance-based sorting to order results by any sortable field in ascending or descending order. This is useful for displaying results by date, price, rating, or other attributes. ```APIDOC ## Sorting Results ### Description Override relevance-based sorting to order results by any sortable field in ascending or descending order. Useful for displaying results by date, price, rating, or other attributes. ### Method SEARCH (Implicit within the Query object) ### Endpoint N/A (Operates on the search engine instance) ### Parameters #### Query Parameters - **SortBy** (Field object) - Optional - The field to sort the results by. - **SortAscending** (boolean) - Optional - Set to `true` for ascending order, `false` for descending order. Defaults to `false` (descending). - **Filter** (Filter object) - Optional - Pre-filter for the search query. ### Request Example ```csharp // Sort by year descending (newest first) var newestQuery = new Query("thriller", maxResults: 20) { SortBy = fields.GetField("year"), SortAscending = false }; var newestResults = engine.Search(newestQuery); // Sort by price descending (most expensive first) var premiumQuery = new Query("laptop", maxResults: 10) { SortBy = fields.GetField("price"), SortAscending = false, Filter = Filter.Parse("inStock = true") }; var premiumResults = engine.Search(premiumQuery); ``` ### Response #### Success Response (200) - **Records** (Array of Record objects) - Search results, ordered according to the specified sort parameters. #### Response Example ```json [ { "Score": 9.8, "DocumentId": "doc789" }, { "Score": 9.5, "DocumentId": "doc101" } ] ``` ``` -------------------------------- ### Infiscript Filter Parsing and Usage in Infidex (.NET) Source: https://github.com/lofcz/infidex/blob/master/README.md Details the use of Infiscript, Infidex's query language, for creating complex filters. This snippet covers parsing various filter types, including simple comparisons, boolean logic, string operations, range checks, list membership, null checks, regex matching, and ternary expressions. It also shows how to integrate these filters into search queries. ```csharp using Infidex.Api; // Simple comparison var filter = Filter.Parse("genre = 'Sci-Fi'"); // Boolean logic var filter = Filter.Parse("genre = 'Sci-Fi' AND year >= 2000"); // Complex expressions with grouping var filter = Filter.Parse("(genre = 'Fantasy' AND year >= 2000) OR (genre = 'Horror' AND year >= 1980)"); // String operations var filter = Filter.Parse("title CONTAINS 'matrix'"); var filter = Filter.Parse("title STARTS WITH 'The'"); var filter = Filter.Parse("description LIKE '%dream%'"); // Range checks var filter = Filter.Parse("year BETWEEN 2000 AND 2020"); var filter = Filter.Parse("rating >= 8.0"); // List membership var filter = Filter.Parse("genre IN ('Sci-Fi', 'Fantasy', 'Adventure')"); // Null checks var filter = Filter.Parse("director IS NOT NULL"); // Regex matching var filter = Filter.Parse("email MATCHES '^[\\w\\.-]+@[\\w\\.-]+\\.\\w+$'"); // Ternary expressions (conditional logic) var filter = Filter.Parse("age >= 18 ? 'adult' : 'minor'"); var filter = Filter.Parse("score >= 90 ? 'A' : score >= 80 ? 'B' : 'C'"); // Use filters in queries var query = new Query("matrix", maxResults: 20) { Filter = Filter.Parse("year >= 2000 AND rating > 7.0") }; var results = engine.Search(query); ```