Skip to content

tallpsmith/elasticshell

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

elasticshell - a shell for elasticsearch

The elasticshell is a javascript shell written in Java. It allows to interact with a running elasticsearch cluster using the Java API.

Getting Started

Installation

  • Download and unzip the elasticshell distribution
  • Run bin/elasticshell on unix, or bin/elasticshell.bat on windows

Help

Use the help() command to have a look at the elasticshell help. Every command is exposed as a javascript function. If you want to get help for a specific command, just type its name without the curly brackets.

Auto-suggestions

Have a look at the auto-suggestions through the tab key to see the available commands and variables. JSON is native within the elasticshell, thus auto-suggestions are available within JSON objects too.

Connecting to a cluster

The elasticshell will automatically try to create a new transport client connected to a node running on localhost:9300. That default transport client will be registered with the es variable name, same result as the following command:

var es = transportClient('localhost:9300');

You can manually connect to a running elasticsearch cluster using the following commands: transportClient('hostname:port') creates a new transport client. You can provide a list of addresses too.

nodeClient('clusterName') creates a new node client.

Let's index a document

var jsonDoc = {
   "user": "kimchy",
   "postDate": "2009-11-15T13:12:00",
   "message": "Trying out Elastic Search, so far so good?"
}

es.index('twitter','tweet','1', jsonDoc);

We can also use the available index builder, which allows to use all the options available when indexing a document:

es.indexBuilder().index('twitter').type('tweet').id('1').source(jsonDoc).execute();

Interact with a specific index or type

You can easily execute operations on a specific index or type like this:

es.<index>.<type>.search();

If the elasticshell does not accept the name of the index or type, for instance if the name contains a space or starts with a number, you can use the following alternate syntax:

es['index name'].search();

Let's retrieve a document

es.twitter.tweet.get('1');

The above command retrieves the previously indexed document using the get API.

Let's search

var termQuery = {
    "query" : {
        "term" : { "user": "kimchy" }
    }
}
es.search(termQuery);

We can also use the search builder: es.searchBuilder().query(termQuery);

We can also make use of the elasticsearch query builders like this:

es.searchBuilder().query(QueryBuilders.termQuery('user','kimchy')).execute();

Let's add a facet to the previous query

es.searchBuilder()
    .query(QueryBuilders.termQuery('user','kimchy'))
    .facet(FacetBuilders.termsFacet('user').field('user')).execute();

All the elasticsearch API are exposed through the elasticshell. Remember that the elasticshell is a javascript shell, thus you can have fun with javascript code. On the other hand, the elasticshell has been built on top of the Rhino engine, which means that you can execute Java code too.

License

This software is licensed under the Apache 2 license, quoted below.

Copyright 2013 Luca Cavanna

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

About

A javascript shell for elasticsearch

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 99.9%
  • Shell 0.1%