### Install Lucene-Kit Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Install the Lucene-Kit package using npm. This command is used for both Node.js and browser environments. ```bash npm install lucene-kit ``` -------------------------------- ### Search and Filtering Examples Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Provides examples of using Lucene-Kit's filter function with various query syntaxes on a sample dataset. Includes filtering by exact age, age range, and combined conditions. ```typescript const data = [ { id: 1, gender: 'Male', firstName: 'Ambrose', age: 47 }, { id: 2, gender: 'Non-binary', firstName: 'Jarid', age: 15 }, { id: 3, gender: 'Female', firstName: 'Corette', age: 55 }, { id: 4, gender: 'Female', firstName: 'Kaleena', age: 77 }, { id: 5, gender: 'Male', firstName: 'Brennen', age: 84 }, ]; // Helper function, just for demo const $q = (q) => new QueryParser(q); filter($q('age:47'), data); filter($q('age:[0 TO 80]'), data); filter($q('gender:*ale OR age:>55'), data); ``` -------------------------------- ### CommonJS Usage Example Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Demonstrates how to import and use the filter and QueryParser from Lucene-Kit in a CommonJS environment. ```javascript const { filter, QueryParser } = require('lucene-kit'); console.log(filter(new QueryParser('age:12'), data)); ``` -------------------------------- ### Wildcard Search (Trailing) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Matches values that start with a specified prefix, followed by any characters. ```ruby # Trailing wildcard: matches everything containing 'word' and everything after it word* ``` -------------------------------- ### ECMAScript Modules Usage Example Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Shows how to import and use the filter and QueryParser from Lucene-Kit in an ECMAScript Modules (ESM) environment. ```javascript import { filter, QueryParser } from 'lucene-kit'; console.log(filter(new QueryParser('age:12'), data)); ``` -------------------------------- ### Resolve Variable Reference to a Query Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md This example shows how to resolve a variable reference to an entirely new query. This is useful for substituting complex, predefined queries or evaluating terms into queries dynamically. ```typescript import { filter, QueryParser, ReferenceResolver } from 'lucene-kit'; const resolver = new ReferenceResolver(); resolver.addVariableResolver('kid', new QueryParser('kid:[0 TO 14]')); resolver.addVariableResolver( 'adult', (node) => /* also possible with functions*/ new QueryParser('adult:[18 TO *]'), ); // $kid resolves to kid:[0 TO 14] => firstName:A* AND kid:[0 TO 14] console.log(filter(new QueryParser('firstName:A* AND $kid'), data, resolver)); ``` -------------------------------- ### Private Fields Exclusion Example Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Demonstrates how to configure Lucene-Kit to exclude private fields (prefixed with '_') from wildcard matches unless explicitly queried. This enhances search precision by allowing control over which fields are indexed. ```typescript const data = [ { id: 2, name: 'Luxury Car Model AC', _description: 'Car Model AC stands out with its unique features.', age: 15 }, { id: 3, name: 'Car Model AD', _description: 'Experience the luxury of Car Model AD.', age: 30 }, ]; const $q = (q) => new QueryParser(q); filter($q('luxury'), data, {..., featureEnablePrivateField: true}); filter($q('description:luxury'), data, {..., featureEnablePrivateField: true}); ``` -------------------------------- ### Nested Field Search (Prefix) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for a term in object properties whose keys start with a specific prefix, including nested properties. ```ruby # Search for 'word' in the object properties starting with 's' and any nested property, e.g., [{s: {a: 'word'}}] s*:word ``` -------------------------------- ### Complex Field Grouping and Logical Operations Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Demonstrates complex combinations of field grouping, logical AND, OR, and NOT operations. ```ruby # Combination of previously used syntax (firstName:(Ambrose OR Brandon) AND lastName:(Harpur OR Dunbleton)) OR (firstName:(Corette OR Kaleena) AND lastName:(Bannard OR Eady)) ``` -------------------------------- ### Field Grouping with OR Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for a field that matches any of the specified values within the group. ```ruby # Field gender includes Male or Female gender:(Male Female) gender:(Male OR Female) ``` -------------------------------- ### Regex Search with Flags Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Applies flags, such as case-insensitivity, to regular expression searches. ```ruby # Regex with flags /test/i ``` -------------------------------- ### Wildcard Search (Leading) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Matches values that end with a specified suffix, preceded by any characters. ```ruby # Leading wildcard: matches everything containing 'word' and everything before it *word ``` -------------------------------- ### Basic String Search (Case Insensitive) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for a word anywhere within a field, ignoring case. ```ruby # Search for the word anywhere in the object (case insensitive) word ``` -------------------------------- ### Exact Match String Search (Case Sensitive) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for an exact word match, respecting case sensitivity. ```ruby # Search for the word anywhere in the object (exact match, case sensitive) 'word' "word" ``` -------------------------------- ### Combined Field and Regex Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Combines field-specific searches with regular expression matching. ```ruby # Previously mentioned syntax can be combined with fields s:/word/ s:w?r* age:[0 TO 20] ``` -------------------------------- ### Wildcard Search (Infix) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Matches values containing a specific sequence of characters, with any characters before or after. ```ruby # Infix wildcard: matches everything containing 'w' and 'ord' with any characters in between w*rd ``` -------------------------------- ### Disjunction (OR) Logical Operation Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Combines two or more conditions, requiring at least one to be true for a document to match. ```ruby # Disjunction: Either condition must be true gender:Non-binary OR age:15 ``` -------------------------------- ### Conjunction (AND) Logical Operation Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Combines two or more conditions, requiring all to be true for a document to match. ```ruby # Conjunction: Both conditions must be true gender:Male AND age:47 ``` -------------------------------- ### Field Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for a term within a specific object property. ```ruby # Search for 'word' in the object property 'sentence' sentence:word ``` -------------------------------- ### Field Grouping with AND and Regex Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Combines multiple conditions within a field group, including logical AND and regex matching. ```ruby gender:(/a/ AND /le/) ``` -------------------------------- ### Number Comparison Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Filters documents based on numerical comparisons for a specified field. ```ruby # Search for age greater than, greater than or equal to, less than, less than or equal to age:100 age:>100 age:>=100 age:<100 age:<=100 ``` -------------------------------- ### Wildcard Search (Single Character) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Matches values where a specific character is replaced by any single character. ```ruby # Single character wildcard: matches everything containing 'w' and 'ord' with any single character in between w?rd ``` -------------------------------- ### Basic Regex Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Performs searches using JavaScript regular expressions. ```ruby # Basic regex: supports the whole JavaScript regex subset /[a-z]/ ``` -------------------------------- ### Generate AST from Query String Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Converts a Lucene query string into an Abstract Syntax Tree (AST) for further processing. Ensure you import necessary typings and type guards for AST manipulation. ```typescript // Returns the AST (Abstract Syntax Tree) const ast = new QueryParser('gender:*ale OR age:>55').toAST(); ``` -------------------------------- ### Negation (NOT) Logical Operation Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Excludes documents that match a specified condition. ```ruby # Negation: Excludes documents that match the specified condition NOT gender:Female # Negation with '!' before field !gender:Female ``` -------------------------------- ### Resolve Function Reference with Complex Logic Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md This snippet illustrates resolving a function reference like 'maturity(level:1)' with a callback. The callback receives the FunctionNode and data, allowing for filtering, returning new queries, or resolving static values based on parameters. ```typescript import { filter, QueryParser, ReferenceResolver } from 'lucene-kit'; const resolver = new ReferenceResolver().addFunctionResolver('mature', (node, data) => { const { params } = node.params; // Perform operations based on parameters const level = params.find(...); if (level <= 1) { // Filter the current data return { data: data.filter(p => p.age >= 14 && p.age <= 18) }; } else if (level <= 10) { // Return a query to be evaluated return new QueryParser('age:[20 TO 30]'); } else { // Resolve a value return 99; } }); console.log(filter(new QueryParser('age:maturity(level:1)'), data, resolver)); ``` -------------------------------- ### Range Search (Inclusive/Exclusive) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for values within a specified numerical or date range. Supports open-ended ranges. ```ruby # Search within a range (from, to) age:[0 TO 20] # Search within a range (from, infinity) (-infinity, to) age:[0 TO *] age:[* TO 20] # Search date (Requirement: field is of type date and provided date is instantiable by new Date(...)) birth:[2000 TO '2004-01-01'] birth:[2020 TO *] ``` -------------------------------- ### Wildcard Search (Mixed) Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Combines different wildcard types for complex pattern matching. ```ruby # Mixed wildcard: combination of all wildcards *t?ain ``` -------------------------------- ### Mixed Logical Operations with Nesting Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Combines AND, OR, and NOT operations with nested parentheses for complex query structures. ```ruby # Mixing with nesting and grouping (gender:Male AND age:40) OR (NOT gender:Female) gender:Female AND (age:20 OR age:60) NOT (gender:Male OR age:20) !(gender:Female AND (age:40 OR age:50)) ((age:55 AND NOT gender:Male) OR (age:20 AND gender:Female)) AND NOT (firstName:"Ambrose" OR lastName:"Bannard") ``` -------------------------------- ### Resolve Variable Reference with Static Value Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Use this snippet to resolve a simple variable reference like '$nb' to a static string value 'Non-Binary'. This is useful for substituting predefined values into queries. ```typescript import { filter, QueryParser, ReferenceResolver } from 'lucene-kit'; console.log( filter(new QueryParser('gender:$nb'), data, new ReferenceResolver().addVariableResolver('nb', 'Non-Binary')), ); ``` -------------------------------- ### Resolve Variable Reference with Conditional Logic Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md This snippet demonstrates resolving a variable reference using a callback function. The function receives the VariableNode and can conditionally return a value based on the node's field or scope, enabling dynamic value resolution. ```typescript import { filter, QueryParser, ReferenceResolver, VariableNode } from 'lucene-kit'; const resolver = new ReferenceResolver(); resolver.addVariableResolver('nb', (node: VariableNode) => { if (node.field == 'gender') { return 'Non-Binary'; } else { return node.scoped ? 'some value' : 'default value'; } }); console.log(filter(new QueryParser('gender:$nb'), data, resolver)); ``` -------------------------------- ### Regex Search with Escaped Characters Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Uses regular expressions with escaped special characters for precise matching. ```ruby # Regex with escaped characters /\d+\.\d*/ ``` -------------------------------- ### Wildcard Nested Field Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for a term in nested properties where the key uses wildcards. ```ruby # Search for 'word' in the object by nested key, where '?' can be any character, e.g., [{s: {a: 'word'}, r: {a: 'word'}}] ?.a:word # Search for 'word' in the object by nested key, where '*' can represent any string, e.g., [{sentence_one: {a: 'word'}, sentence_two: {a: 'word'}}] *.a:word ``` -------------------------------- ### Deeply Nested Field Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for a term in deeply nested object properties using dot notation. ```ruby # Search for 'word' in the object by nested key 's.a' s.a:word ``` -------------------------------- ### Boolean Field Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Filters documents based on boolean values (true or false) for a specified field. ```ruby male:true male:false ``` -------------------------------- ### Array Field Search Source: https://github.com/oxdev03/lucene-kit/blob/master/README.md Searches for a term within an array associated with a specific field. ```ruby # Search for 'word' in array e.g. [{ s: ['word','number'] }] s:word ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.