Recently I wrote a post introducing the Elastic Search engine and how to install it. Now it’s time to move on and start doing some funny things with this powerfull tool.
If you follow the Part 1 of Elastify your Search post you are able to:
- Start ES engine by typing elasticsearch on the terminal
- Get a JSON response with basic ES information when accessing localhost:9200 on the browser
- Access Head plugin by typing localhost:9200/_plugin/head
Now let jump into create and search for data in Elastic Search
Create the First Index
- Open the head plugin page and access the option “Any Request”
- Make a POST request to http://localhost:9200/customer
If everything’s fine, you just created an Index with the name “customer”
- Go to “General View” separator on top of Head plugin interface and check if you have a structure like this:
This means that we have a cluster named “Bible John” with only one index: “customer”
Note that the cluster name was given randomly by ES (it is possible to change this name by defining it on the yml config file present on config directory of ES installation path).
You can also use another plugin or tool instead of Head Plugin to access Elastic Search Api, remember that what we are doing is just simple Http requests.
Add a DOCUMENT
- It is very simple to add/create a document inside an Index. Just perform another POST request to ES Api
- POST http://localhost:9200/customer/internal/3
In the request URL we specify the Index, the Type and Id of the Customer that we are about to create. In the body we can set all the properties of this new Customer.
Once the request is performed, we receive the response result from the Api.
SEARCH FOR SOME DATA
Elastic Search Api also allow us to get data from the indexes. Here are some examples:
- GET http://localhost:9200/customer/internal/3
- This request retrieves the document with Id 3 present on INTERNAL type of CUSTOMER index.
- It’s also possible to explicitly set the method we want to execute against ES
- POST http://localhost:9200/customer/internal/_search/
- In this case we are invoking the SEARCH method of the Api. Since there’s any filter defined on request, the Elastic Search will retrieve all documents on INTERNAL type of CUSTOMER index.
- Query to get all internal customers with a matching firstName
- The previous request will perform a request to get all the documents which has a firstName equals to “Mario”
FULL TEXT SEARCH
- Search for a document with some keyword
- This request search for any document on CUSTOMER index that has the keyword lisbon (on any field)
- It is also possible to specify the field to search for the keyword
- In this case the ES will search for any document which has “lisbon” on the city field
I hope this brief introduction to Elastic Search was explanatory to the big potential of the technology.
The previous examples were very simple and only had the goal of introducing the ES Api. Please refer to the ES documentation to get more detailed info about the whole engine: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html