Git Product home page Git Product logo

filtres.js's Introduction

FiltrES (Filtrex for ElasticSearch)

Pronounced like Fortress

A simple, safe, ElasticSearch query engine, allowing you or your end-users to enter arbitrary expressions without p0wning you or learning the ElasticSearch Query language.

(height <= 73 or (favorites.color == "green" and height != 73)) and firstname ~= "o.+"

Why?

There are many cases where you want a user to be able enter an arbitrary expression through a user interface or simply want to avoid ElasticSearch's powerful, but complicated query language.

Sure, you could do that with ElasticSearch's "script" filter, but I'm sure I don't have to tell you how stupid that would be. It opens up many potential security issues.

FiltrES defines a really simple expression language that should be familiar to anyone who's ever used a spreadsheet and compile it into an ElasticSearch query at runtime.

Features

  • Simple! End user expression language looks like this transactions <= 5 and profit > 20.5
  • Fast! Expressions get compiled into native ElasticSearch queries, offering the same performance as if it had been hand coded. e.g. {"filtered" : {"filter" : {"bool" : {"must" : {"term" : { "tag" : "wow" }}, "must_not" : {"range" : {"age" : { "from" : 10, "to" : 20 }}}
  • Safe! Expressions cannot escape the sandbox client-side or inside of ElasticSearch.
  • Predictable! Because users can't define loops or recursive functions, you know you won't be left hanging.

Get it

10 second tutorial (using ElasticSearchClient)

// A search filter
var expression = 'transactions <= 5 and profit > 20.5';

// Compile expression to executable function
var myQuery = filtres.compile(expression);

// Execute query
esc.search("filtres", "accounts", myQuery, function (err, accounts) {
    ...
});

Expressions

There are only 2 types: numbers and strings. Numbers may be floating point or integers. Boolean logic is applied on the truthy value of values (e.g. any non-zero number is true, any non-empty string is true, otherwise false).

Values Description
43, -1.234 Numbers
"hello" String
foo, a.b.c External data variable defined by application (may be numbers or strings)
Comparisons Description
x == y Equals
x != y Not equals
x ~= "y" Matched to y evaluated as a RegExp
x ~!= "y" Not matched to y evaluated as a RegExp
x < y Less than
x <= y Less than or equal to
x > y Greater than
x >= y Greater than or equal to
Boolean logic Description
x or y Boolean or
x and y Boolean and
not x Boolean not
( x ) Explicit operator precedence

Operator precedence follows that of any sane language.

FAQ

Why the name?

Because it was originally built for FILTeR EXpressions then ported to ElasticSearch (i.e. ES).

What's Jison?

Jison is bundled with FiltrES - it's a JavaScript parser generator that does the underlying hard work of understanding the expression. It's based on Flex and Bison.

License?

MIT

Tests?

Here!

What happens if the expression is malformed?

Calling filters.compile() with a malformed expression will throw an exception. You can catch that and display feedback to the user. A good UI pattern is to attempt to compile on each keystroke and continuously indicate whether the expression is valid.

And follow @abeisgreat (FiltrES) and follow @joewalnes (Filtrex)!

filtres.js's People

Contributors

abeisgoat avatar alexbeletsky avatar

Stargazers

Andrew Johnson avatar Radu Topala avatar Tejaswi Kumar Goudru avatar  avatar ypcpy avatar Mikal avatar Jie Li avatar ty000 avatar 攀攀 avatar Adam Walsh avatar Jose RF Junior avatar Ciro Cesar Maciel avatar sharkshark avatar 于鹏飞 avatar Silvio Greuel avatar  avatar Sven Anders Robbestad avatar Basti Becker avatar Matthew avatar Sergii Bondarenko avatar ks avatar Lorenzo Mangani avatar Andrei avatar Paul Wilton avatar Robert avatar Chris Frohoff avatar Serhii Shyman avatar Rohit Agarwal avatar Angus H. avatar Jean-Baptiste Pionnier avatar Nikita Bakaev avatar Kefeng Deng avatar Randall Leeds avatar  avatar Rob Bygrave avatar Tom Wilson avatar Manuel avatar  avatar Greg Edwards avatar Chance avatar Hernan (Yidi) Ke avatar Marc Hadfield avatar Santiago Gaviria avatar Piotrek Majewski avatar Gray Zhang avatar Rakesh Sankar avatar Garrett Wilson avatar Itay Weinberger avatar Eric Candino avatar Giovanni Kock Bonetti avatar François VANTOMME avatar Marius Miliunas avatar Arturs Girons avatar Michael Mente avatar  avatar Joel Trost avatar Edward Smit avatar  avatar Eak Netpanya avatar Hiroki Sato avatar  avatar April Johnson avatar Omer Shelef avatar  avatar Andrey Subbotin avatar DarkMan avatar Ronaldo Sousa avatar Jay Swan avatar BJR Matos avatar Anibal avatar Jeremy Barthe avatar Loïc Mahieu avatar Alexander Khlebnikov avatar Sina Sharafzadeh avatar  avatar Son avatar Simon avatar Cory Armbrecht avatar Jason Jung avatar Han Wang avatar Jeongbu Yoon avatar Florent Bourgeois avatar C.J. Winslow avatar Yuya Saito avatar Sandro Pasquali avatar Konstantin Burykin avatar Chris Gansen avatar Chris Kim avatar Eugene Kostrikov avatar PG Herveou avatar Max Irwin avatar Allan Marques Baptista avatar Erin McKean avatar Dor Tzur avatar Dieter Oberkofler avatar Régis Gaidot avatar  avatar Nate Todd avatar Daniel Ménard avatar Legal avatar

Watchers

Imran Ansari avatar Rob Bygrave avatar Richard Hess avatar Franck Michaud avatar James Cloos avatar daewon avatar damienbod avatar Ricardo Montoya avatar Sergii Bondarenko avatar

filtres.js's Issues

Support for other DBs?

I'm intrigued by this project, but am interested in your thoughts on ways it might be made to support other datastores. Looking at the code, it seems like it might be possible to write drivers for different backends like mongodb, etc.

So here are my questions:

Do you think this would be possible? Would you be interested in integrating back PRs to add this?

What kind of guidance would you give me if I were to try to implement a driver for mongodb?

Do you think it would make sense to parse the query language to some intermediate format that drivers would then generate queries from vs. having to specify a grammar for each driver? Or would it just be easier to specify a grammar per-driver?

Readme makes false statements about unit tests

In the README.md you mentioned that this project has unit tests. Actually the file filtres-test.js doesn’t contain a single unit test. The tests are only working with a running elasticsearch instance. That said, I think the right term here is integration tests instead of unit tests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.