npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@testlio/cloudsearch-query-builder

v1.0.3

Published

Builds string queries for cloudsearch

Downloads

969

Readme

CloudSearch Query Builder

Circle CI NPM Package

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.

// Query for all Star Wars films released before the year 2000
builder.and([builder.phrase('star wars', 'title'), builder.range('year', undefined, 2000)]);
// -> (and (phrase field=star wars 'title') (range field=year {,2000]))
// Query for all Star Wars films, boosting those released before year 2000
builder.and([builder.phrase('star wars', 'title'), builder.or([builder.range('year', undefined, 2000, { boost: 4 }), builder.range('year', 2000)])]);
// -> (and (phrase field=title 'star wars') (or (range field=year boost=4 {,2000]) (range field=year [2000,})))
// Query for all Star Wars films that Harrison Ford stars in
builder.and([builder.term('Harrison Ford', 'actors'), builder.phrase('star wars', 'title')]);
// -> (and (term field=actors 'Harrison Ford') (phrase field=title 'star wars'))

With certain operators, you can also omit the field to search over all textual fields, for example:

// Search for all films that contain term 'star' in any of their textual fields
builder.term('star');
// -> (term 'star')

// Search for all films released after year 2000 that mention Harrison Ford
builder.and([builder.phrase('Harrison Ford'), builder.range('year', 2000)]);
// -> (and (term 'star') (range field=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.

// Obtain CloudSearchDomain, possibly via CloudSearch.describeDomains()
const cloudSearchDomain = ...;
const builder = require('@testlio/cloudsearch-query-builder');

// Create a query as explained above
const query = builder.and([builder.phrase('star wars', 'title'), builder.range('year', undefined, 2000)]);

// Conduct the search
cloudSearchDomain.search({
    query: query,
    queryParser: 'structured'
}, function(err, data) {
    // Handle the results
});

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:

  1. Supporting other query parsers
  2. Increasing test coverage
  3. Documentation and providing further examples