CloudSearch Query Builder
AWS CloudSearch supports searching over data/documents by using query strings, these query strings can contain compound query in a specific format/syntax. While this syntax is well documented, creating this string in code can be error-prone. This is where query builder comes in, allowing queries to be built using a more functional approach, and ensuring the resulting string is in the correct format.
Installation
npm install @testlio/cloudsearch-query-builder
Getting Started
Query builder mirrors the available operators, listed here, as functions that take arguments and produce a string. All of these functions are well-documented in the code itself, thus a few example cases here should do the trick.
builder.and([builder.phrase('star wars', 'title'), builder.range('year', undefined, 2000)]);
builder.and([builder.phrase('star wars', 'title'), builder.or([builder.range('year', undefined, 2000, { boost: 4 }), builder.range('year', 2000)])]);
builder.and([builder.term('Harrison Ford', 'actors'), builder.phrase('star wars', 'title')]);
With certain operators, you can also omit the field to search over all textual fields, for example:
builder.term('star');
builder.and([builder.phrase('Harrison Ford'), builder.range('year', 2000)]);
The full API is documented inline, you can go over it here.
Interfacing with AWS SDK
The resulting string from query builder should be passed along to the search
function of CloudSearchDomain in AWS SDK for Node.js. It is important to note that all strings that query builder returns rely on the queryParser
parameter to be set to structured
.
const cloudSearchDomain = ...;
const builder = require('@testlio/cloudsearch-query-builder');
const query = builder.and([builder.phrase('star wars', 'title'), builder.range('year', undefined, 2000)]);
cloudSearchDomain.search({
query: query,
queryParser: 'structured'
}, function(err, data) {
});
NOTE: Because of the CloudSearch limitations, query builder removes double quotes from the search parameters.
Contributing
Contributions to cloudsearch-query-builder are very welcome! Please make sure to follow the Contribution Guidelines. Areas that you could help out with include, but are not limited to:
- Supporting other query parsers
- Increasing test coverage
- Documentation and providing further examples