Function to parse search strings into useful search tokens grouped by optional, required, or forbidden:
- quoted phrases (
"this is a phrase"
) - conjunction operators (
AND
,OR
) - parenthesis grouping (
(word AND "phrase in a group")
) (supports 2 levels of nesting) - required/forbidden flagging (
+word
or-word
respectively) (can also be used to flag phrases and groups)
This project is open to updates by its users, I ensure that PRs are relevant to the community. In other words, if you find a bug or want a new feature, please help us by becoming one of the contributors ✌️ ! See the contributing section
Please consider:
- Buying me a coffee ☕
- Supporting Simply Hexagonal on Open Collective 🏆
- Starring this repo on Github 🌟
pnpm i @simplyhexagonal/parse-search-string
# or
yarn add @simplyhexagonal/parse-search-string
# or
npm install @simplyhexagonal/parse-search-string
import parseSearchString from '@simplyhexagonal/parse-search-string';
const result = parseSearchString('"Elon Musk" AND +(Bitcoin OR Doge)');
console.log(JSON.stringify(result, null, 2));
// [
// {
// "index": 0,
// "tokenType": "andOperation",
// "value": "AND",
// "children": [
// {
// "index": 1,
// "tokenType": "optionalPhrase",
// "value": "Elon Musk",
// "children": []
// },
// {
// "index": 17,
// "tokenType": "requiredGroup",
// "value": "(Bitcoin OR Doge)",
// "children": [
// {
// "index": 18,
// "tokenType": "orOperation",
// "value": "OR",
// "children": [
// {
// "index": 18,
// "tokenType": "optionalWord",
// "value": "Bitcoin",
// "children": []
// },
// {
// "index": 29,
// "tokenType": "optionalWord",
// "value": "Doge",
// "children": []
// }
// ]
// }
// ]
// }
// ]
// }
// ]
The token object structure was designed to be human readable yet easy to transform into a $search
aggregation query compatible with MongoDB Query API.
Financial Time's n-search-parser (npm module) which is in turn inspired by Lucene query parser (module) and Building a search query parser (article).
Yes, thank you! This plugin is community-driven, most of its features are from different authors.
Please update the docs and tests and add your name to the package.json
file.
Thanks goes to these wonderful people (emoji key):
Jean Lescure 🚧 💻 📓 |
Copyright (c) 2022-Present Package Contributors.
Licensed under the Apache License 2.0.