### Install Search Syntax Parser (Bash) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Provides the Composer command to install the search-syntax-parser package. ```Bash composer require hosmelq/search-syntax-parser ``` -------------------------------- ### Create Custom Output Adapters in PHP Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Shows how to create custom output formats by implementing the 'QueryAdapterInterface'. This example defines an 'EloquentAdapter' to convert the parsed Abstract Syntax Tree (AST) into a format suitable for Eloquent queries. ```PHP use HosmelQ\SearchSyntaxParser\Adapter\QueryAdapterInterface; use HosmelQ\SearchSyntaxParser\AST\Node\NodeInterface; use HosmelQ\SearchSyntaxParser\SearchParser; class EloquentAdapter implements QueryAdapterInterface { public function build(NodeInterface $ast): mixed { // Convert the AST to your preferred format return ['where' => ['name', 'john']]; } } $parser = SearchParser::query('name:john'); $parser->extend('eloquent', fn () => new EloquentAdapter()); $result = $parser->build('eloquent'); ``` -------------------------------- ### Validate Specific Array Indices with at() in PHP Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Illustrates validating specific elements of an array, treated like a tuple, using the 'at()' method. This example validates a 'location' array, ensuring the first element is a numeric latitude between -90 and 90, and the second is a numeric longitude between -180 and 180. ```PHP use HosmelQ\SearchSyntaxParser\SearchParser; use HosmelQ\SearchSyntaxParser\Validation\AllowedField; $parser = SearchParser::query('location:37.7749,-122.4194') ->allowedFields([ AllowedField::array('location') ->size(2) ->at(0, fn ($rules) => $rules->numeric()->between(-90, 90)) ->at(1, fn ($rules) => $rules->numeric()->between(-180, 180)), ]); $result = $parser->build(); ``` -------------------------------- ### Initialize and Build Search Query (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Demonstrates how to initialize the SearchParser with a query string and build the structured output using the default array adapter. ```PHP use HosmelQ\SearchSyntaxParser\SearchParser; $result = SearchParser::query('age:>25 AND name:john')->build(); ``` ```PHP use HosmelQ\SearchSyntaxParser\SearchParser; $result = SearchParser::query('title:Coffee AND price:<10')->build(); ``` -------------------------------- ### Run Tests with Composer Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md A simple bash command to execute the project's tests using Composer. ```Bash composer test ``` -------------------------------- ### Default Array Adapter Output (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Illustrates the structured PHP array output generated by the default array adapter for a simple search query. ```PHP use HosmelQ\SearchSyntaxParser\SearchParser; $result = SearchParser::query('title:Coffee')->build(); // Array output [ 'field' => 'title', 'operator' => '=', 'type' => 'comparison', 'value' => 'Coffee', ] ``` -------------------------------- ### Basic Term Search (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Illustrates how to perform a basic search for terms that match default searchable fields. ```PHP SearchParser::query('coffee')->build(); ``` -------------------------------- ### Field Validation and Mapping (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Shows how to define allowed fields with constraints (min, max, allowed values) and map external field names to internal ones. ```PHP use HosmelQ\SearchSyntaxParser\SearchParser; use HosmelQ\SearchSyntaxParser\Validation\AllowedField; $parser = SearchParser::query('age:25 AND status:ACTIVE')->allowedFields([ AllowedField::integer('age')->min(0), AllowedField::in('status', ['ACTIVE', 'DRAFT', 'PENDING']), AllowedField::string('name')->size(2), ]); $result = $parser->build(); // throws if any value is invalid or a field is not allowed ``` ```PHP $parser = SearchParser::query('age:10')->allowedFields([ AllowedField::integer('age', 'user_age'), ]); $result = $parser->build(); // The array adapter will output the internal name "user_age" for the field ``` -------------------------------- ### Exists Queries (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Demonstrates how to search for documents where a field has a value or does not exist using wildcard syntax. ```PHP SearchParser::query('category:*')->build(); // Field has any value ``` ```PHP SearchParser::query('NOT discount:*')->build(); // Field doesn't exist ``` -------------------------------- ### Comparison Operators in Search Queries (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Demonstrates various comparison operators for field-value relationships, including greater than, less than, and not equal. ```PHP SearchParser::query('price:>10')->build(); // Greater than ``` ```PHP SearchParser::query('price:>=10')->build(); // Greater than or equal ``` ```PHP SearchParser::query('price:<50')->build(); // Less than ``` ```PHP SearchParser::query('price:<=50')->build(); // Less than or equal ``` ```PHP SearchParser::query('status:!=sold')->build(); // Not equal ``` -------------------------------- ### Handle Parsing Errors with ParseException in PHP Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Demonstrates how to catch and handle parsing errors using the 'ParseException' class. This snippet shows a try-catch block to gracefully manage invalid syntax during the parsing process. ```PHP use HosmelQ\SearchSyntaxParser\Exception\ParseException; use HosmelQ\SearchSyntaxParser\SearchParser; try { SearchParser::query('invalid:syntax:here')->build(); } catch (ParseException $e) { echo "Parse error: " . $e->getMessage(); } ``` -------------------------------- ### Range Queries (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Shows how to perform range searches on numeric or date fields using 'TO' syntax. ```PHP SearchParser::query('price:[10 TO 50]')->build(); // Numeric range ``` ```PHP SearchParser::query('date:[2025-01-01 TO 2025-12-31]')->build(); // Date range ``` -------------------------------- ### Multi-Value Field Searches (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Explains how to perform searches on a single field with multiple values, acting as OR operations, and supporting quoted values. ```PHP // Single field, multiple values SearchParser::query('status:ACTIVE,DRAFT,PENDING')->build(); // Equivalent to: status:ACTIVE OR status:DRAFT OR status:PENDING ``` ```PHP // Works with any operator SearchParser::query('status:!=SOLD,EXPIRED')->build(); // Equivalent to: status:!=SOLD OR status:!=EXPIRED ``` ```PHP // Can be combined with boolean logic SearchParser::query('status:ACTIVE,DRAFT AND price:>100')->build(); ``` ```PHP // Supports quoted values SearchParser::query('category:"Home & Garden","Sports & Outdoors"')->build(); ``` -------------------------------- ### Boolean Connectives in Search Queries (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Shows how to use explicit AND, OR, and implicit AND operators to combine multiple search terms. ```PHP SearchParser::query('title:Coffee AND price:<10')->build(); // Explicit AND ``` ```PHP SearchParser::query('title:Coffee OR title:Tea')->build(); // OR operator ``` ```PHP SearchParser::query('title:Coffee price:<10')->build(); // Implicit AND ``` -------------------------------- ### NOT Modifier Usage (PHP) Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Demonstrates how to negate terms or subqueries using the 'NOT' or '-' modifiers. ```PHP SearchParser::query('NOT title:Coffee')->build(); // NOT modifier ``` ```PHP SearchParser::query('-title:Coffee')->build(); // - modifier (equivalent) ``` -------------------------------- ### Validate Array Items with each() in PHP Source: https://github.com/hosmelq/search-syntax-parser/blob/main/README.md Demonstrates how to validate each item within an array field using the 'each()' method in the Search Syntax Parser. It shows how to apply string validation with a maximum length to all elements of the 'tags' array. ```PHP use HosmelQ\SearchSyntaxParser\SearchParser; use HosmelQ\SearchSyntaxParser\Validation\AllowedField; $parser = SearchParser::query('tags:alpha,beta') ->allowedFields([ AllowedField::array('tags') ->max(5) ->each(fn ($rules) => $rules->string()->max(10)), ]); $result = $parser->build(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.