The `search-query-parser` library provides a robust, dependency-free solution for parsing advanced search query strings into a structured JavaScript object. It supports syntax similar to popular search engines, allowing for field-specific searches (e.g., `from:user`, `subject:topic`), date ranges (`date:1/10/2013-15/04/2014`), multi-value fields, and exclusion syntax (`-keyword:value`). The current stable version is 1.6.0, released over five years ago, indicating a mature and stable, but not actively developed, state. Key features include configurable keywords and range fields, optional tokenization of free text, the ability to always return matched keyword values as arrays, and the inclusion of character offsets for parsed elements. Since version 1.6.0, it also offers a `stringify` method to convert the parsed object back into a query string. This package primarily targets CommonJS environments, relying on the `require` syntax.
npm install search-query-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing a complex search query with keywords, ranges, exclusion, and then stringifying the result. It highlights configuring parser options and accessing different parts of the parsed object.
Always use `const searchQuery = require('search-query-parser');` for importing the module in Node.js environments.Ensure `search-query-parser` is updated to version `1.6.0` or higher to use the `stringify()` method. Run `npm install search-query-parser@latest`.
Set the `tokenize: true` option to receive non-keyword text terms as an array under the `text` key, or ensure keywords/ranges are configured to capture all intended structured data.
Set `alwaysArray: true` in the options object (`{ alwaysArray: true }`) to ensure all matched keyword values are always returned as arrays, simplifying type handling.If offset information is not required, disable it by setting `offsets: false` in the options object to reduce the size of the parsed output.
Ensure you are using `const searchQuery = require('search-query-parser');` and then calling `searchQuery.parse(query, options);`.For consistent object output, always provide relevant `keywords` and `ranges` options. To ensure `text` is an array, set `tokenize: true`. For consistent array output for keyword values, set `alwaysArray: true`.
No dependency data recorded yet.