hello , I am developing backend using strapi , and i am going to store some data inside elasticsearch , and i would like to be able to consume the data, and fetch them.
here is the different configuration files
routes:
routes: [
{
method: 'GET',
path: '/energy-consumption',
handler: 'energy.energyConsumption',
config: {
policies: [],
middlewares: [],
},
},
{
method: 'POST',
path: '/energy-consumption',
handler: 'energy.energyConsumption',
config: {
policies: [],
middlewares: [],
},
},
],
};
here is the controllers file :
/**
* A set of functions called "actions" for `energy`
*/
module.exports = {
energyConsumption: async (ctx) => {
try {
console.log('Energy consumption action called');
const data = await strapi.service('api::energy.energy').find();
console.log('Retrieved energy consumption data:', data);
ctx.send(data);
} catch (err) {
console.error('Error retrieving energy consumption:', err);
ctx.send({ error: err.message }, 500);
}
}
};
and here is the service file :
'use strict';
const { createCoreService } = require('@strapi/strapi').factories;
module.exports = createCoreService('api::energy.energy', ({ strapi }) => ({
async find(...args) {
const { connector, testConn } = require('../../../../helpers/client_elastic.js');
const client = connector();
testConn(client);
console.log('Connection to Elasticsearch successful');
const searchParams = {
index: 'index', // Replace with the appropriate index name
body: {
query: {
match_all: {} // Retrieve all documents
}
}
};
try {
console.log('Sending search request to Elasticsearch');
const response = await client.search(searchParams);
console.log('Elasticsearch response:', response);
const body = response.body || {};
console.log('Received response from Elasticsearch:', body);
const hits = body.hits?.hits || [];
console.log('Total hits:', hits.length);
const mappedData = hits.map((hit) => {
const source = hit._source;
return {
name: source.name,
measuringPoint: source.measuringPoint,
current: source.current,
voltage: source.voltage,
activePower: source.activePower,
cosphi: source.cosphi,
powerFactory: source.powerFactory,
date: source.createdAt
};
});
console.log('Transformed data:');
mappedData.forEach((data) => {
console.log(data);
});
return mappedData;
} catch (error) {
console.error('Error retrieving energy consumption data:', error);
throw error;
}
}
}));
and here is the schem.json file :
"kind": "collectionType",
"collectionName": "energies",
"info": {
"singularName": "energy",
"pluralName": "energies",
"displayName": "Energy",
"description": ""
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"name": {
"type": "string"
},
"measuringPoint": {
"type": "string"
},
"current": {
"type": "float"
},
"voltage": {
"type": "float"
},
"activePower": {
"type": "float"
}
}
}
now , here is the docker file configuration , as well the logstach configuration file :
Docker file:
version: "2.0"
services:
# Elasticsearch service
elasticsearch:
# Use the official Elasticsearch image from Docker Hub
image: docker.elastic.co/elasticsearch/elasticsearch:8.6.2
container_name: elasticsearch
# Set the Environment variables for Elasticsearch
environment:
# Set discovery type to single node
- discovery.type=single-node
- node.name=elasticsearch
# Lock the process memory to prevent swapping
- bootstrap.memory_lock=true
# Set java memory heap size
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
# Disable security features (otherwise it won't work)
- xpack.security.enabled=false
# Expose ports for Elasticsearch
ports:
- 9200:9200
- 9300:9300
# Set memory limits for Elasticsearch
ulimits:
memlock:
soft: -1
hard: -1
# Health check for Elasticsearch (check the cluster health)
healthcheck:
test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
interval: 30s
timeout: 10s
retries: 5
# Set network for Elasticsearch
networks:
- default
# Logstash service
logstash:
# Use the official (and latest)logstash image from Docker Hub
image: docker.elastic.co/logstash/logstash:8.6.2
container_name: logstash
# Mount Logstash configuration file
volumes:
- ./logstash-config:/usr/share/logstash/pipeline
# set the Environment variables for Logstash
environment:
# Set Elasticsearch hosts
- "ES_HOSTS=http://elasticsearch:9200"
# Expose ports for Logstash
ports:
- 5000:5000
# Set network for Logstash
networks:
- default
logstach config file:
# Logstash configuration file
input {
# Listen for HTTP input on all network interfaces on port 5000
http {
host => "0.0.0.0"
port => 5000
}
}
filter {
# Parse the incoming JSON messages
json {
source => "message"
}
# Remove the original message field
mutate {
remove_field => ["message"]
}
# Parse the createAt field as a date
date {
match => ["createAt", "ISO8601"]
target => "createAt"
}
}
output {
# Send the modified data to Elasticsearch
elasticsearch {
# Set Elasticsearch hosts
hosts => ["http://elasticsearch:9200"]
# Set index name from incoming headers
index => "index"
}
}
the problem is when i sned data , to the index , and query it , from postman , or the teminal , I am able to query it , here is an example
**sending post request to this url : "http ://localhost: 9200/index/_search ", I have this result
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 12,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "index",
"_id": "npYOLIgBnRgggzy5jhUE",
"_score": 1.0,
"_source": {
"url": {
"domain": "localhost",
"port": 5000,
"path": "/test"
},
"name": "phase2",
"current": 26.112,
"voltage": 234.449,
"host": {
"ip": "172.20.0.1"
},
"measuringPoint": "MY_SENSOR0",
"@timestamp": "2023-05-17T23:32:11.276864499Z",
"activePower": 22517.918,
"@version": "1",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "431"
}
}
}
}
},
{
"_index": "index",
"_id": "nJYOLIgBnRgggzy5jhUE",
"_score": 1.0,
"_source": {
"url": {
"domain": "localhost",
"port": 5000,
"path": "/test"
},
"name": "phase3",
"current": 116.696,
"voltage": 225.73,
"host": {
"ip": "172.20.0.1"
},
"measuringPoint": "MY_SENSOR0",
"@timestamp": "2023-05-17T23:32:11.276885058Z",
"activePower": 26858.449,
"@version": "1",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "431"
}
}
}
}
},
{
"_index": "index",
"_id": "nZYOLIgBnRgggzy5jhUE",
"_score": 1.0,
"_source": {
"url": {
"domain": "localhost",
"port": 5000,
"path": "/test"
},
"name": "phase1",
"current": 45.439,
"voltage": 243.47,
"host": {
"ip": "172.20.0.1"
},
"measuringPoint": "MY_SENSOR0",
"@timestamp": "2023-05-17T23:32:11.276828600Z",
"activePower": 17125.657,
"@version": "1",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "431"
}
}
}
}
},
{
"_index": "index",
"_id": "YpTEIIgBN1v-cV69M0xX",
"_score": 1.0,
"_source": {
"activePower": 22517.918,
"@timestamp": "2023-05-15T18:55:09.022156533Z",
"url": {
"domain": "localhost",
"port": 5000,
"path": "/index"
},
"name": "phase2",
"cosphi": 0.895,
"measuringPoint": "MY_SENSOR0",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"createAt": "2023-05-10T15:48:00.542Z",
"host": {
"ip": "172.20.0.1"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "2443"
}
}
},
"@version": "1",
"powerFactory": 52.639,
"current": 26.112,
"voltage": 234.449
}
},
{
"_index": "index",
"_id": "ZpTEIIgBN1v-cV69M0xa",
"_score": 1.0,
"_source": {
"activePower": 26858.449,
"@timestamp": "2023-05-15T18:55:09.022172968Z",
"url": {
"domain": "localhost",
"port": 5000,
"path": "/index"
},
"name": "phase3",
"cosphi": 1.103,
"measuringPoint": "MY_SENSOR0",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"createAt": "2023-05-10T15:48:00.542Z",
"host": {
"ip": "172.20.0.1"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "2443"
}
}
},
"@version": "1",
"powerFactory": 49.435,
"current": 116.696,
"voltage": 225.73
}
},
{
"_index": "index",
"_id": "YJTEIIgBN1v-cV69M0xX",
"_score": 1.0,
"_source": {
"activePower": 20896.89,
"@timestamp": "2023-05-15T18:55:09.022195587Z",
"url": {
"domain": "localhost",
"port": 5000,
"path": "/index"
},
"name": "phase2",
"cosphi": 1.632,
"measuringPoint": "MY_SENSOR1",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"createAt": "2023-05-10T15:48:00.566Z",
"host": {
"ip": "172.20.0.1"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "2443"
}
}
},
"@version": "1",
"powerFactory": 48.576,
"current": 91.742,
"voltage": 226.565
}
},
{
"_index": "index",
"_id": "X5TEIIgBN1v-cV69M0xV",
"_score": 1.0,
"_source": {
"activePower": 26032.483,
"@timestamp": "2023-05-15T18:55:09.022206027Z",
"url": {
"domain": "localhost",
"port": 5000,
"path": "/index"
},
"name": "phase3",
"cosphi": 0.78,
"measuringPoint": "MY_SENSOR1",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"createAt": "2023-05-10T15:48:00.566Z",
"host": {
"ip": "172.20.0.1"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "2443"
}
}
},
"@version": "1",
"powerFactory": 48.433,
"current": 27.94,
"voltage": 235.424
}
},
{
"_index": "index",
"_id": "Z5TEIIgBN1v-cV69M0xa",
"_score": 1.0,
"_source": {
"activePower": 27142.946,
"@timestamp": "2023-05-15T18:55:09.022238002Z",
"url": {
"domain": "localhost",
"port": 5000,
"path": "/index"
},
"name": "phase3",
"cosphi": 0.737,
"measuringPoint": "MY_SENSOR2",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"createAt": "2023-05-10T15:48:00.569Z",
"host": {
"ip": "172.20.0.1"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "2443"
}
}
},
"@version": "1",
"powerFactory": 49.227,
"current": 115.652,
"voltage": 239.214
}
},
{
"_index": "index",
"_id": "ZZTEIIgBN1v-cV69M0xY",
"_score": 1.0,
"_source": {
"activePower": 24469.064,
"@timestamp": "2023-05-15T18:55:09.022184780Z",
"url": {
"domain": "localhost",
"port": 5000,
"path": "/index"
},
"name": "phase1",
"cosphi": 0.239,
"measuringPoint": "MY_SENSOR1",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"createAt": "2023-05-10T15:48:00.566Z",
"host": {
"ip": "172.20.0.1"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "2443"
}
}
},
"@version": "1",
"powerFactory": 48.159,
"current": 102.288,
"voltage": 223.414
}
},
{
"_index": "index",
"_id": "YZTEIIgBN1v-cV69M0xW",
"_score": 1.0,
"_source": {
"activePower": 17125.657,
"@timestamp": "2023-05-15T18:55:09.022128396Z",
"url": {
"domain": "localhost",
"port": 5000,
"path": "/index"
},
"name": "phase1",
"cosphi": 0.6,
"measuringPoint": "MY_SENSOR0",
"user_agent": {
"original": "PostmanRuntime/7.32.2"
},
"createAt": "2023-05-10T15:48:00.542Z",
"host": {
"ip": "172.20.0.1"
},
"http": {
"version": "HTTP/1.1",
"method": "POST",
"request": {
"mime_type": "application/json",
"body": {
"bytes": "2443"
}
}
},
"@version": "1",
"powerFactory": 51.718,
"current": 45.439,
"voltage": 243.47
}
}
]
}
}
whereas, when i try to run my server , i recieve an empty repsonse ,
Energy consumption action called
Connection to Elasticsearch successful
Sending search request to Elasticsearch
{
name: 'elasticsearch',
cluster_name: 'docker-cluster',
cluster_uuid: '0vMVTgPxSnWS-qmeJoGj1A',
version: {
number: '8.6.2',
build_flavor: 'default',
build_type: 'docker',
build_hash: '2d58d0f136141f03239816a4e360a8d17b6d8f29',
build_date: '2023-02-13T09:35:20.314882762Z',
build_snapshot: false,
lucene_version: '9.4.2',
minimum_wire_compatibility_version: '7.17.0',
minimum_index_compatibility_version: '7.0.0'
},
tagline: 'You Know, for Search'
}
Elasticsearch response: {
took: 2,
timed_out: false,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
hits: {
total: { value: 12, relation: 'eq' },
max_score: 1,
hits: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
]
}
}
Received response from Elasticsearch: {}
Total hits: 0
Transformed data:
Retrieved energy consumption data: []
[2023-05-18 00:48:07.432] http: GET /api/energy-consumption (105 ms) 200
how to solve this issue , ? I will be really greatful for any tips for solving such an errror