Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
elasticmagic
Advanced tools
This is alpha. Api may/will change.
JS/Typescript DSL for Elasticsearch
This lib is a port of original library written in python
by @anti-social
Supports Elasticsearch version 6.x
Install elasticmagic-js using npm
:
npm install elasticmagic-js
Let's get started by writing a simple query.
query builder
and container for data from elasticimport { Client } from '@elastic/elasticsearch';
import {
Cluster,
Field,
IntegerType,
Doc,
Bool,
} from "elasticmagic-js";
enum OrderStatus {
new = 1,
paid = 2,
handled = 3,
canceled = 4,
}
enum OrderSource {
desktop = 1,
mobile = 2,
}
class OrderDoc extends Doc {
public static docType: string = 'order';
public static userId = new Field(DateType, 'user_id', OrderDoc);
public userId?: number;
public static status = new Field(DateType, 'status', OrderDoc);
public status?: number;
public static source = new Field(DateType, 'source', OrderDoc);
public source?: number;
public static price = new Field(DateType, 'price', OrderDoc);
public price?: number;
public static dateCreated = new Field(DateType, 'date_created', OrderDoc);
public dateCreated?: Date;
}
const client = new Client({ node: 'http://es6-test:9200' });
const cluster = new Cluster(client, 'test_order_index');
const query = cluster.searchQuery({ routing: 1 })
.source(false)
.filter(
Bool.must(
OrderDoc.user_id.in([1]),
OrderDoc.status.in([OrderStatus.new, OrderStatus.paid]),
OrderDoc.source.not(OrderSource.mobile),
)
)
.limit(0);
console.log(query.toJSON()) // or console.log(query.body)
It will print:
{
query: {
bool: {
filter: {
bool: {
must: [
{terms: {user_id: [1]}},
{terms: {status: [1, 2]}},
{bool: {
must_not: [
{term: {source: 2}}
]
}}
]
}
}
}
},
_source: false,
size: 0
}
const result = await query.getResult<OrderDoc>();
const query = searchQuery
.source(false)
.filter(
Bool.must(
OrderDoc.userId.in([1]),
OrderDoc.status.in([OrderStatus.new, OrderStatus.handled, OrderStatus.paid]),
OrderDoc.source.not(OrderSource.mobile),
OrderDoc.dateCreated.lte(new Date().toISOString()),
)
)
.aggregations({
usersOrders: new agg.Terms({
field: OrderDoc.userId,
size: 1,
aggs: {
total: new agg.Filter({
filter: OrderDoc.conditionSourceDesktop(),
aggs: {
selled: new agg.Filter({
filter: Bool.must(
OrderDoc.status.in([OrderStatus.paid, OrderStatus.handled]),
),
aggs: {
paid: new agg.Filter({
filter: OrderDoc.status.eq(OrderStatus.paid)
}),
handled: new agg.Filter({
filter: OrderDoc.status.eq(OrderStatus.handled)
}),
}
}),
canceled: new agg.Filter({
filter: OrderDoc.status.eq(OrderStatus.canceled),
}),
new: new agg.Filter({
filter: OrderDoc.status.eq(OrderStatus.new)
})
}
}),
lowcost: new agg.Filter({
filter: OrderDoc.conditionLowPrice()
})
}
})
});
Now you can get aggregation data
const result = await query.getResult<OrderDoc>();
const usersOrders = result.getAggregation("usersOrders");
console.log(usersOrders.buckets[0].key) // prints 1
console.log(usersOrders.buckets[0].docCount) // prints 1
const total = usersOrders.buckets[0].getAggregation("total")
console.log(total.docCount) // # prints 1
First build base image
make build #
Run all tests
make test
Run one test
make test TEST=tests/testSearchQuery.spec.ts
# or
make test TEST=testSearchQuery.spec.ts
FAQs
JS orm for elasticsearch.
The npm package elasticmagic receives a total of 0 weekly downloads. As such, elasticmagic popularity was classified as not popular.
We found that elasticmagic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.