A javascript utility to autocomplete and convert queries written in simplified Lucene-like query syntax into a OData or FIQL syntax.
Simple sample
A converter can be initilized in javascript like this :
converter = NaturalQuery(
"#naturalQuery",
[
{
naturalName: "artist",
possibleValues: [
"daft punk",
"moriarty",
"the rolling stones",
"abba" ]
}]).process();
This code inserts an auto-complete input in the div #naturalQuery.
To convert the natural query typed by the user into a REST query (OData format by default), use this code :
converter.convert();
It returns a REST query.
For instance, with the example above, the user types that :
artist:abba artist:"daft punk"
And the conversion returns this :
artist eq 'abba' and artist eq 'daft punk'
More complete sample
A more complete converter can be initilized in javascript like this :
converter = NaturalQuery(
"#naturalQuery", // the selector of a div
// where the input will be inserted
[
{
naturalName: "title" // the name of a filter
// attribute, without any value
}, {
naturalName: "year" // the name of another attribute,
// without any value
}, {
naturalName: "artist", // the name of a filter
// attribute, with a list of
// hard-coded possible values
possibleValues: [
"daft punk",
"moriarty",
"the rolling stones",
"abba" ]
}, {
naturalName: "country" , // the name of a filter
// attribute, with a list of
// possible values given by a rest API
restAPIUrl: "https://restcountries.eu/rest/v2/", // the url for
// the rest API
restMapperCallback: function ( countryJson ) { // a callback function
// called to map the
// API's response into
// a list of possible values
return countryJson.name ;
}
}, {
naturalName: "style" , // the name of a filter attribute,
// with a list of key/values. The values are
// displayed to the user, the keys are used in the
// converted query.
possibleValues: [
{key: 1, value: "rock"},
{key: 2, value: "jazz"},
{key: 3, value: "musette"},
{key: 4, value: "classic"},
{key: 5, value: "electro"}
] ,
mappedValues: true
}]).process();
This code inserts an auto-complete input in the div #naturalQuery.
To convert the natural query typed by the user into a REST query (OData format by default), use this code :
converter.convert();
It returns a REST query.
For instance, with the example above, the user types that :
title:"around the world" artist:"daft punk" year:[2000 2017] style:electro
And the conversion returns this :
title eq 'around the world' and artist eq 'daft punk' and year gt '2000' and year lt '2017' and style eq '5'
Running samples
Try it localy with the examples given in the sample folder in this repo. Or or try it online on jsfiddle.net :
- simple example
- example with key/value values
- example with FIQL format
- example with rest API for possible values
Features implemented
- parsing of a simplified Lucene query syntax including the following patterns :
name:jean
(simple equal filter, with no white space)band:"daft punk"
(equal filter with white spaces)name:georges,"jean seb",bob
(multivalue, interpreted with “or” operator)quantity>3
(greater or equal)quantity<3
(lower or equal)quantity:[3 10]
(range of values)
- autocompletion with values given in an array (with or without white spaces)
- conversion into odata filter format
- conversion into FIQL filter format
- autocompletion with values given from REST url
TODOs
See GitHub issue tracker.
References
Query languages supported by CXF
Research paper “User friendly querying of weakly structured data”, by Pavel KÁCHA