Skip to content

Parsing of search strings into optional, required, or forbidden phrases, groups, operators, and/or words

Notifications You must be signed in to change notification settings

simplyhexagonal/parse-search-string

Repository files navigation

Parse Search String

Tests Try parse-search-string on RunKit

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)

Open source notice

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

Like this module? ❤

Please consider:

Install

pnpm i @simplyhexagonal/parse-search-string

# or
yarn add @simplyhexagonal/parse-search-string

# or
npm install @simplyhexagonal/parse-search-string

Usage

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.

Inspired By

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).

Contributing

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.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Jean Lescure

🚧 💻 📓 ⚠️ 💡 📖

License

Copyright (c) 2022-Present Package Contributors.
Licensed under the Apache License 2.0.

About

Parsing of search strings into optional, required, or forbidden phrases, groups, operators, and/or words

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published