Skip to content

Instantly share code, notes, and snippets.

@puuble
Last active February 23, 2023 18:54
Show Gist options
  • Save puuble/3d663d933c1ea88a523a6126ea0116a4 to your computer and use it in GitHub Desktop.
Save puuble/3d663d933c1ea88a523a6126ea0116a4 to your computer and use it in GitHub Desktop.
React for Elastic Search
/// npm install @elastic/elasticsearch
import { Client } from '@elastic/elasticsearch';
const client = new Client({
node: 'https://your-elastic-search-endpoint',
auth: {
username: 'your-username',
password: 'your-password'
}
});
const searchResponse = await client.search({
index: 'your-index-name',
body: {
query: {
match: {
title: 'your-search-query'
}
}
}
});
const hits = searchResponse.hits.hits;
const totalHits = searchResponse.hits.total.value;
const { Client } = require('@elastic/elasticsearch');
const elasticUrl = 'http://localhost:9200';
const indexName = 'financial_data';
const elasticClient = new Client({ node: elasticUrl });
async function search() {
const { body } = await elasticClient.search({
index: indexName,
body: {
query: {
range: {
timestamp: {
gte: 'now-30d/d',
lt: 'now/d'
}
}
},
aggs: {
transactions_by_day: {
date_histogram: {
field: 'timestamp',
calendar_interval: 'day'
},
aggs: {
total_value: {
sum: {
field: 'value'
}
}
}
}
}
}
});
return body.aggregations.transactions_by_day.buckets;
}
import React, { useState, useEffect } from 'react';
function TimeSeriesChart() {
const [data, setData] = useState([]);
useEffect(() => {
async function fetchTimeSeriesData() {
const timeSeriesData = await search();
setData(timeSeriesData);
}
fetchTimeSeriesData();
}, []);
return (
<div>
<h2>Financial Transactions in the last 30 days</h2>
<ul>
{data.map(bucket => (
<li key={bucket.key_as_string}>
<span>{bucket.key_as_string}: </span>
<span>{bucket.total_value.value}</span>
</li>
))}
</ul>
</div>
);
}
async function searchTrades() {
const client = new Client({ node: 'http://localhost:9200' });
const { body } = await client.search({
index: 'trades',
body: {
size: 0,
aggs: {
trades_over_time: {
date_histogram: {
field: 'timestamp',
calendar_interval: 'minute'
},
aggs: {
total_buy_quantity: {
sum: {
field: 'quantity',
script: {
lang: 'painless',
source: "doc['type'].value == 'buy' ? doc['quantity'].value : 0"
}
}
},
total_sell_quantity: {
sum: {
field: 'quantity',
script: {
lang: 'painless',
source: "doc['type'].value == 'sell' ? doc['quantity'].value : 0"
}
}
},
avg_buy_price: {
avg: {
field: 'price',
script: {
lang: 'painless',
source: "doc['type'].value == 'buy' ? doc['price'].value : null"
}
}
},
avg_sell_price: {
avg: {
field: 'price',
script: {
lang: 'painless',
source: "doc['type'].value == 'sell' ? doc['price'].value : null"
}
}
},
larger_average: {
bucket_selector: {
buckets_path: {
avgBuy: 'avg_buy_price',
avgSell: 'avg_sell_price'
},
script: 'params.avgBuy > params.avgSell'
}
}
}
}
}
}
});
return body.aggregations.trades_over_time.buckets;
}
async function searchTrades() {
const client = new Client({ node: 'http://localhost:9200' });
const { body } = await client.search({
index: 'trades',
body: {
size: 0,
aggs: {
trades_over_time: {
date_histogram: {
field: 'timestamp',
calendar_interval: 'hour'
},
aggs: {
total_buy_quantity: {
sum: {
field: 'quantity',
script: {
lang: 'painless',
source: "doc['type'].value == 'buy' ? doc['quantity'].value : 0"
}
}
},
total_sell_quantity: {
sum: {
field: 'quantity',
script: {
lang: 'painless',
source: "doc['type'].value == 'sell' ? doc['quantity'].value : 0"
}
}
}
}
}
}
}
});
return body.aggregations.trades_over_time.buckets;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment