Part 2 - Queries & Operations in ElasticSearch - Indexes & Documents
The overall blog is divided into three parts
Part 2: Queries & Operations in ElasticSearch on various Indexes & Documents (you are here…)
Part 3: Comming Soon…
In part 1, you must have familiarized yourself with basic terms like indexes, documents etc and at this point your ElasticSearch & Kibana must be up and running.
If not then please check out the Part 1 of this editorial before continuing to this.
Awesome then… Lets play with data
Enough talk, lets open the Kibana Dev tools dashboard & hit with the following queries.
- Creating a Document
- We have created a document with id 1, under index “shirts” with 4 fields: brand, color, size & price.
- After running the query, in the output tab we can see the result is successful created a document with _id 1
2. Updating a Document
Lets update the same document (with id 1). We wish to change its brand name from “guci” to “gucci”.
Here is how this can be done. The process is basically the same. (Notice the result is updated instead of created)
3. Fetching a Document
Lets say you want to fetch a document with id 1. This can be done with a simple GET request.
4. Deleting a Document
As you can see in the syntax of Deleting a Document is quite the same as that of a GET statement. (Also checkout the result in output section)
Now at this point you know how perform basic CRUD (Create, Reterive, Update & Delete) operation on data.
Quering the Data in ElasticSearch
For this part, I have created these 5 documents with id 1 to 5 in index “shirts” having different properties/fields.
You can see the information about index “shirts” including total documents etc and display some documents using _search API in elasticsearch.
For this you can query :
- Fetch all the shirts of brand ‘gucci’
The query will match the brand (field-name) of all the documents and fetches those documents whose brand matches with “gucci”
2. Fetch all “red” color shirts of “zara”
For this query we need two match statements. We can add match statements in this way:
As we can see our bool query returns all such documents whose brand must conatins “zara” and color must contain “red”.
- we can also use “must_not” property instead of “must” for the complete opposite behaviour as that of “must”.
3. Fetch all shirts whose color is either “red” or “blue”
Here is the same query but instead of “must” we have used “should” property.
As you can see from hits.total.value we got 4 documents satisfying the above property.
4. Fetch all “non-red” shirts of “zara” or “gucci”
For this type of query, we can use multiple statements in this way
Dealing with numbers
5. Find all the shirts whose price is more than 250
For this we can use “range” query like this
6. Fetch all the “medium” sized shirts whose price is between 150–350
Now you must be familiar with the General Structure of the queries in Elasticsearch. And should have got the basic idea of the operations of the ElasticSearch.
I will soon add Part 3 to this blog. So Please stay tuned & follow this space for more information & exciting stuff in ElasticSearch.
Congratulation on sucessfully completing this chapter & thanks for sticking till the end .
Please let me know about your views or queries in the comment section.