Skip to content

stereobooster/search_syntax

Repository files navigation

search_syntax

Parser for "advanced search" query language. Inspired by GitHub's search syntax

There is no wide accepted terminology for this kind of query language. Sometimes it's called: query string, search query, advanced search query, generic query.

Package provides only parser. AST produced by parser can be converted to a specific "backend", for example: ransack, MySQL Full-Text Search, PostgreSQL Full-Text Search, Meilisearch, Elasticsearch, Solr, Lucene (Lucene vs Solr), Sphinx.

Transforming AST is out of scope of this package, except for Ransack, which serves as an example of implementation.

Disclaimer

So far parser only supports bare strings, quoted strings ("some string") and parameters (param:1).

Parser doesn't support negation (not/-), boolean operations (and/&/or/|) and groupping ((a | b)).

This probably will change as soon as I understand how to add those "advanced" features without making it less user-friendly for non-techy people. See Language design for explanations.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add search_syntax

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install search_syntax

Usage

With ransack

def index
-  @q = Person.ransack(params[:q])
+  query_parser = SearchSyntax::Ransack.new(text: :name_or_email_cont, params: Person.column_names - ["id"])
+  @q = Person.ransack(query_parser.parse(params[:search]))
  @people = @q.result(distinct: true)
end

Standalone

see lib/search_syntax/ransack.rb for an example of how to use parser

Similar packages

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

To regenrate parser

$ bin/tt lib/search_syntax/search_syntax_grammar.tt

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/stereobooster/search_syntax. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the SearchSyntax project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.