Last active
February 23, 2023 18:54
-
-
Save puuble/3d663d933c1ea88a523a6126ea0116a4 to your computer and use it in GitHub Desktop.
React for Elastic Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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