A TypeScript library for parsing search queries to identify text, field, and object filters. (Inspired by https://help.obsidian.md/plugins/search#Search+operators)
npm install string_filter_parserimport { parseQuery } from 'string_filter_parser';
// Parse your search query
const filters = parseQuery('your search query');The parser supports three types of filters:
Simple text search without any special syntax.
const query = "search this text";
const filters = parseQuery(query);
// Result: [{ type: 'text', searchText: 'search this text' }]Use the field:value syntax for field-based filtering.
- Sample field
{
owner: 'admin'
...
}Supported field types:
owner: Filter by ownerdatasource: Filter by data sourcetag: Filer by tag
Examples:
// Filter by owner
const ownerQuery = "owner:admin";
const ownerFilters = parseQuery(ownerQuery);
// Result: [{ type: 'field', fieldType: 'owner', fieldName: 'admin' }]
// Filter by datasource
const datasourceQuery = "tag:mkt";
const datasourceFilters = parseQuery(datasourceQuery);
// Result: [{ type: 'field', fieldType: 'owner', fieldName: 'mkt' }]
// Combine multiple field filters
const combinedQuery = "owner:admin tag:mkt";
const combinedFilters = parseQuery(combinedQuery);
// Result: [
// { type: 'field', fieldType: 'owner', fieldName: 'admin' },
// { type: 'field', fieldType: 'tag', fieldName: 'mkt' }
// ]Use type:object text syntax for object-base filtering
- Object sample:
Model users {
data_source: 'demodb'
dimension name {
...
}
...
}Supported object types:
modeldatasetdimensionmeasure
Examples:
// Filter by model
const modelQuery = "type:model user";
const modelFilters = parseQuery(modelQuery);
// Result: [
// { type: 'object', objectType: 'model', objectName: 'user' },
// ]
// Filter by dimension
const dimensionQuery = "type:dimension revenue";
const dimensionFilters = parseQuery(dimensionQuery);
// Result: [
// { type: 'object', objectType: 'dimension', objectName: 'revenue' },
// ]You can combine different types of filters in a single query:
const complexQuery = "type:model sales owner:admin datasource:mysql_db important";
const complexFilters = parseQuery(complexQuery);
// Result: [
// { type: 'object', objectType: 'model', objectName: 'sales' },
// { type: 'field', fieldType: 'owner', fieldName: 'admin' },
// { type: 'field', fieldType: 'datasource', fieldName: 'mysql_db' },
// { type: 'text', searchText: 'important' }
// ]- Field filters are case-sensitive
- Object type filters must be followed by a search text
- Text filters can be used anywhere in the query
- Multiple filters can be combined in any order