ElasticSearch

Blended Admin does not have a dedicated client to ElasticSearch, but everyting can be done with HttpClient.

Let’s start with creating a index.

function main(arg)
{
   if (arg.method == 'post')
   {
      var httpClient = new HttpClient();
      var response = httpClient.put({
         url:'http://paas:user@server/posts',
         headers: {'Content-Type':'application/json'},
         body: {'settings' : {'index' : { 'number_of_shards' : 3, 'number_of_replicas' : 2 }}}
      });
      
      if (response.statusCode == 200)
      {
        var body = JSON.parse(response.body);
        if (body.acknowledged)
          return [new HtmlView('Index "posts" was successfully created. ')]
        else
          this.errors.push(response);
      }
      else
      {
         this.errors.push(response);
      }
   }
   
   return [new FormView({method: 'post'})];
}

main(arg);

When you run the code, a form with Run button apears. When you click it, a http put request is sent to a server to create a index. If the index is created, HtmlView with a successfull message is dipslyed to a user. Otherwise, a user sees a error.

Next, we need a form to add a item to the index.

function main(arg)
{
  var title = arg.form.title;
  var author = arg.form.author;
  
  var formView = new FormView({
    method: 'post',
    controls: [
      [{name:'title', value: title}, null],
      [{name:'author', value: author}, null]
    ]
  });
   
  if (arg.method == 'post') {
      
     if (!title) {
       this.errors.push('title is required.');
       return [formView]; 
     }
  
     if (!author) {
       this.errors.push('author is required.');
       return [formView]; 
     }
    
     var httpClient = new HttpClient();
     var response = httpClient.post({
       url:'http://paas:4763eca5c8f733b03adbc66c3871ef33@gloin-eu-west-1.searchly.com/posts/_doc',
       headers: {'Content-Type':'application/json'},
       body: {'title': title, 'author':author, 'postDate': new Date()}
     }); 
    
     if (response.statusCode == 201)
     {
       return [new HtmlView('Index "posts" was successfully created. ')]
     }
     else
     {
       this.errors.push(response);
     }
  }
  
  return [formView];
}
main(arg);

ElasticSearch Tutorial - Add Post

The code creates a form with two inputs. After filling it and clicking Run, the code validates the input and sends a post request to Elastic Search server and displays the result.

Lastly, we need a way to show the content of the index.

function main(arg)
{
   var httpClient = new HttpClient();
   var matchAllItems = httpClient.post({
     url:'http://paas:user@server/posts/_search',
     headers: {'Content-Type':'application/json'},
     body: {'query': {'match_all': { }}}
   });
  var matchAllItemsBody = JSON.parse(matchAllItems.body);
  var matchAllItemsHits = matchAllItemsBody.hits.hits.map(function(a) {return a['_source'];});;
  
   var tableView = new TableView(matchAllItemsHits); 
   return [tableView];
}
main(arg); 

ElasticSearch Tutorial - List Posts