A few days ago at work, we moved on to using Postman, and as we use Elastic Search and Docker quite a lot it sounded fun to put them together to allow some easy sandboxing. So here we go: how to play with Elastic Search in a sandboxed environment using Postman and Docker.
Requirements
Before we go any further, check that the docker installation is happy,
If you skipped firing up elastic in the previous steps, you need to pull down elastic with the following command,
- $ sudo docker pull docker.elastic.co/elasticsearch/elasticsearch:7.10.2
- $ sudo docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name "es_demo_app" docker.elastic.co/elasticsearch/elasticsearch:7.10.2
The 'p' flag sets the port forwarding to the container, from local to container.
The 'e' flag sets elastic to a single node mode.
The --name flag is used to give it a handy name and it will ensure we start the same container with data persistency.
- #start and stop
- docker container run es_demo_app
- docker container stop es_demo_app
Elastic Search - ES
Elasticsearch was originally released as open-source by Shay Banon in 2010 under the standard Apache 2 open source license, as a search server built on the Lucene library. It can be seen as shard-based in-memory cache technology and operates by data separation and partitioning, this what gives its extremely low response.
First lets the health of the ES cluster/clusters
To check the health of our ES clusters we can do an HTTP GET request on http://localhost:9200/ and we will get the status of our elastic search cluster;
Adding an index
To add an empty index with no mappings we could simply just do an HTTP PUT call on http://localhost:9200/new_index/, to add a mapping we would post the mapping in the body as JSON. A mapping is similar to what we call schema in the relational database world. It can define what types of attributes to expect and how to index these within ES, it gets very interesting when you start playing with tokenizers and analyzers.
Adding Data
We simply do an HTTP PUT into the products index on a document id, if the id exists it will be an "update" if not it will be a "create.
Searching
One of the most basic queries you can do in ES is a term query, it matches the full word. We can contrast the result of that with the prefix query.
Run the same as a prefix query, we get matches on "hot", or any product with a tag starting with "ho".
Searching - Aggregates
We can not aggregate on the text so we have a "keyword" subfield. We could use this for a little auto-suggest, showing the user that he matches 2 items in drinks and 1 item in food.
Deleting Data
Very similar to the single put-call we did to create and update the document, we change it to HTTP DELETE.
Getting the Mappings
Deleting an index
To delete an index we use the same URL as for the create call executed as HTTP DELETE call