In previous articles (Here you can read: part-1 and part-2) we have seen that how CRUD queries work on Elasticsearch engine via terminal. I hope you understood it thoroughly. Now let’s discuss in detail about queries. So in this article, we will see how Elasticsearch engine filter records and all.

Search Content.

For search the result, we just get via curl and append _search at the end and method is GET. And we can get all the data from an engine, execute the following command:

From all indexes.

curl -XGET "http://localhost:9200/_search?q=*&pretty"

This will return all the data from Elasticsearch engine, even if it is from a different index because we are not passing any index in the URL.

From a particular index.

We will use POST method, and we are passing the query parameter. We can also search by using the Elasticsearch domain specific language, which is passed in as a document that looks like standard JSON. Execute the following command:

curl -XPOST 'localhost:9200/first_index/car/_search?pretty' -d '{ "query" : { "match_all": { } } }'

The result will be:

{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0}, "hits" : { "total" : 4, "max_score" : 1.0, "hits" : [ { "_index" : "first_index", "_type" : "car", "_id" : "5", "_score" : 1.0, "_source" : { "manufacturer" : "Audi", "model" : "TT" } }, { "_index" : "first_index", "_type" : "car", "_id" : "4", "_score" : 1.0, "_source" : { "manufacturer" : "ABCD", "model" : "AB" } },
{ "_index" : "first_index", "_type" : "car", "_id" : "AWHRhEf1R_zLEbMGbfpK", "_score" : 1.0, "_source" : { "manufacturer" : "Nissan", "model" : "GT-R" } }, { "_index" : "first_index", "_type" : "car", "_id" : "1", "_score" : 1.0, "_source" : { "manufacturer" : "Porsche", "model" : "911" } } ] } }

Search via particular attribute data.

We will use GET method with ‘_search‘. Let’s search the data:

curl -XGET "http://localhost:9200/_search?q=model:AB"

The result will be:

{ "took" : 19, "timed_out" : false, "_shards": { "total" : 35, "successful" : 35, "failed" : 0}, "hits" : { "total" : 1, "max_score" : 0.80259144, "hits" :[ { "_index" : "first_index", "_type" : "car", "_id" : "4", "_score" : 0.80259144, "_source" : { "manufacturer" : "ABCD", "model" : "AB" } } ] } }

Add a column in a row.

In this query, actually we are going to update the row or we can say the table. So append _update at the end and use POST method to perform this operation. Let’s try:

curl -XPOST "http://localhost:9200/first_index/car/1/_update" -d '{ "doc": { "type":"4 wheeler" } }'

In this command, we are going to add type column, for that we had selected particular row from the data via id and then update it. The result will be:

{ "_index" : "first_index", "_type" : "car", "_id" : "1", "_version" : 4, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0} }

It tells us the result is updated.

Let’s check the data:
curl -XGET "http://localhost:9200/first_index/car/1?pretty"

We will get:

{ "_index" : "first_index", "_type" : "car", "_id" : "1", "_version" : 4, "found" : true, "_source" : { "manufacturer" : "Porsche", "model" : "911", "type" : "4 wheeler" } }

Read-only particular columns.

In this we will read a particular column, so we will use GET method to read that particular data. If we want just data of model and type then a command is:

curl -XGET "http://localhost:9200/first_index/car/1?_source=model,type"

The result will be:

{ "_index" : "first_index", "_type" : "car", "_id" : "1", "_version" : 4, "found" : true, "_source" : { "model" : "911", "type" : "4 wheeler" } }

We got data from only two columns ( model and type ) from the car table.

Remove specific column.

In this, actually, we are going to update the table. So we will use _update and method will be POST. We will remove type column from the car table then a command is:

curl -XPOST "http://localhost:9200/first_index/car/1/_update" -d ' { "script": "ctx._source.remove(\"type\")" }'

The result will be:

{ "_index" : "first_index", "_type" : "car", "_id" : "1","_version" : 5, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } }

Delete index data.

If we want to delete all data from a particular index, then execute the following command:

curl -XDELETE 'localhost:9200/caption-index'

I have caption-index and I am going to delete data from that index using DELETE method. And the result will be:

{"acknowledged":true}

If we want to delete rows with some condition, if we have add type in car table, then execute the following command:

curl - XPOST "http://localhost:9200/first_index/car/_delete_by_query?pretty" -d ' { "query" : { "match":{ "type":"2 wheeler" } } }'

The result will be:

{"took" : 330, "timed_out" : false, "total" : 2, "deleted" : 2, "batches" : 1, "version_conflicts" : 0, "noops" : 0, "retries" : { "bulk" : 0, "search" : 0 }, "throttled_millis" : 0,
"requests_per_second" : -1.0, "throttled_until_millis" : 0, "failures" : [ ] }

This type we can play with Elasticsearch engine with different types of queries and get the results.

Click here for more blogs…


At BoTree Technologies, we build enterprise applications with our Django team of 20+ engineers.

We also specialize in RPA, AI, Python, Ruby on Rails, JavaScript and ReactJS.

Consulting is free – let us help you grow!