Elastic Search With Node.JS Part 3
Posted By : Hotam Singh | 17-Feb-2018
In the previous articles, I covered about brief introduction about Elasticsearch and index, creating an index, deleting an index, adding documents to an index and deleting documents from an index.
In this article, we will be discussing search API of Elasticsearch in detail.
Searching in Elasticsearch :
The search API of Elasticsearch allows you to execute a search based on your query and get back the results as hits(row/s) that match the query. The query can either be provided using the following ways:
- Using query string as a parameter
- Using a request body
Elasticsearch Head:
Elasticsearch Head is a Chrome extension. Download this and add this extension to your chrome browser and start executing queries. It is a good option if you are a beginner. I will use the same throughout the article.
Search:
Create a new file search.js and paste the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
var client = require('./connection.js');
client.search({
index: 'mem_accounts',
type: 'mem_accounts',
body: {
query: {
match: { "address": "4995063339468361088E" }
},
}
},function (error, response,status) {
if (error){
console.log("search error: "+error)
}
else {
console.log("--- Response ---");
console.log(response);
console.log("--- Hits ---");
response.hits.hits.forEach(function(hit){
console.log(hit);
})
}
});
|
Save this file and run using node, you will get following output:
Now, let's move to the Elasticsearch Head to run simple queries and their outputs based upon queries:
Match All in Elasticsearch:
Elasticsearch provides match_all property that fetch all records from an index. See below query example and its result in right:
Multi-field search in Elasticsearch:
Elasticsearch provides multi_match property that allows us to search based on multi fields. See below query and example.
Limit in Elasticsearch:
Limits can be set in Elasticsearch using the from
and size
parameters.
- The
from
parameter defines the offset from where the records to be returned. - The
size
parameter allows you to fetch number of records to be returned.
Basically from(defaults to 0)
and size(defaults to 10)
can be set as request parameters, they can also be set within the search body.
let us see an example:
Open Elasticsearch Head extension and click Any Request [+] tab you will see upper section and set query as below image:
And click Request button as shown in above image and you will get response something like below:
You can see the response as per your choice i.e. table/JSON/CSV. We can see above result in table also. Just go to Structured Query [+] button just left to Any Request [+] button.
You need to set queris as per your choice. By default, Elasticsearch fetch 10 records. see below image in structured format:
Sorting in Elasticsearch:
Sorting in Elasticsearch is as simple as MongoDB search based on sorting. We will sort above image's response based on height. Query structure will be like the below image:
This is not an end. I tried to cover basic search operations and you can do more with searching. Happy coding.
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Hotam Singh
Hotam has 1.5 years of experience in Node.JS. He has worked on Front End, JavaScript, Jquery, Backbone.JS, Database: MySQL, MongoDB. His hobbies are playing Sudoku/Puzzles and watching movies.