3

i am working on my bachelor degree project and i have to use the Ballerina integrator middle-ware.

Now i need to make a service which will be able to index data coming to ballerina into Elasticsearch.Is there a way to communicate with Elasticsearch using only ballerina (without using Log stash or File beat..) just like we were communicating to a SQL database?

enter image description here

Under_Ice
  • 147
  • 5

1 Answers1

2

If there is someone looking for the same thing, i just found a way to communicate with Elastic and it's working very well

---here is the code---

import ballerina/http;
import ballerina/log;


listener http:Listener httpListener = new(9090);

http:Client elasticDB = new("http://localhost:9200/");

@http:ServiceConfig{
    basePath: "/elastic"
}

service GetElasticSource on httpListener{
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/{index}/_source/{id}"
    }

    resource function retrieveElasticIndexById(http:Caller httpCaller,   http:Request httpRequest, string index, string id){
        http:Response resp = new;

        var data = elasticDB->get(<@untained> ("/"+index+"/_source/"+id));

        if(data is http:Response){
            resp = data;
            }

        var respRet = httpCaller->respond(resp);
        if(respRet is error){
            log:printError("error responding to the client", err = respRet);
        }
    }
}
Under_Ice
  • 147
  • 5