Algolia Filters Query Builder
A parser for having a structured way for querying algolia similar to well known ODMs or ORMS.
Installation
npm i algolia-search-builder
Motivation
Currently Algolia supports string format only for filtering the search results, which doesn't seem very functional for making complex dynamic queries (probably by string concatenation), which requires you to memorize the built in algolia operators. Using the algolia query builder making dynamic filter queries is easier and similar to sequelize and mongoose.
Usage
const algoliaQueryBuilder = new AlgoliaQueryBuilder({
x:{lt:3, gte:1},
y:{between:[1,2]}
});
const filterQuery = algoliaQueryBuilder.exec();
const results = algoliaIndex.search("str",{
filters: filterQuery
});
Operators
**** - #### in
Checks if a field is in the array of given values (can contain strings or number)
json { x: { in: [1,2,3] } }
-
Logical
Negates the given query
{ x: { not: { between:[ 1 , 2] } } }
returns results that satisfy at least one of the given conditions
{ or:
[
{ x: { eq:1 } },
{ y: { eq:2 } },
]
}
returns results that satisfy at all of the given conditions
{ and:
[
{ x: { eq:1 } },
{ y: { eq:2 } },
]
}
Advanced Usage Examples
const algoliaQueryBuilder1 = new AlgoliaQueryBuilder({ x:3, y: "str" });
const algoliaQueryBuilder2 = new AlgoliaQueryBuilder({
and:[
{x:3},
{or:[{y:4},{z:5}]}
]
});
const algoliaQueryBuilder2 = new AlgoliaQueryBuilder({
x:{
gt:3,
lt:6
}
});
General Notes
-
The query builder validates the query after parsing and before returning the query string the limitations and constraints of the query builder are mostly related to the limitations given by algolia
-
To use _tags
for filtering just add the a custom field with name _tags
and pass it the value
{_tags:"published"}