Elastic Search With URI

Elastic Search with URI helps to perform a quick search on data without writing too much code. In this blog, we will have an overview of Elastic Search.

Elastic Search is the current number one search engine created based on Lucine. The Search API allows performing a search in two ways. The first approach is based on URL query string and the latter one supports JSON as part of the request body. The Query string-based approach is called Search Lite in Elastic. We can use this approach primarily for simple and quick work (warning - not intended for developing an enterprise application). You can perform with most of the operators like AND, OR, Aggregate, Range etc.
 

How to form the query string

 
Elastic Search supports a query string parameter "q" as part of the URL so we can use that query string to form our query. Using the query string approach, we can perform many operations using the operators like OR, AND, Aggregate and Range etc. Below, you can see how a URL will look like based on the localhost.
 
URL Format
 
http://localhost:9200/employee/_search?q=shyju
http://{DomainName/servername}:{Port}/{IndexName}/_serach?q={encodedquery}
 
The above URL will search for all the entries in the employee index for the word Shyju. It's pretty simple to form the query and use it. Usually, this works better to perform something quick in a browser or in a console program which will validate or loop through the data. The important point to remember in this approach is the encoding of the query string since it is part of the URL. For example, if we want to search for the records which match the name as Shyju, then the URL will look like the following.
 
http://localhost:9200/employee/_search?q=name%3Ashyju
name:shyju is encoded to name%3Ashyju
 

Using OR

 
In this Query String / Search Lite approach, you can use + as your OR opertor. Let us say I want to search employee index and find the employee named Shyju or department IT. So, the query string will be like the following.
 
+name:shyju+department:it encoded to %2Bname%3Ashyju%2Bdepartment%3Ait%20
http://localhost:9200/employee/_search?q=%2Bname%3Ashyju%2Bdepartment%3Ait%20
 

Using AND

 
There are two approaches to use AND. One way is by using the query string setting and the other approach is using AND in the query string. We will take same example since the last OR statement is not really wanted in that scanario. We need to find an employee whose name is Shyju and the department is IT.
 
name:shyju and department:it encoded to name%3Ashyju%20and%20department%3Ait 
http://localhost:9200/employee/_search?q=name%3Ashyju%20and%20department%3Ait
 
Another approach is to use settings. For example, we can write the same with + and setting the default operator as,
 

AND

 
name:shyju+department:it encoded to %2Bname%3Ashyju%2Bdepartment%3Ait%20
http://localhost:9200/employee/_search?default_operator=AND&q=%2Bname%3Ashyju%2Bdepartment%3Ait%20
 

Date Range

 
One of the common needs is to get some data within a specific date range so let us try to pull all the employee records updated between certain date and belong to IT department.
 
modified:["2019-05-22T03:25:19" TO "2019-05-23T18:48:50Z"] and department:it encoded to modified%3A%5B%222019-05-22T03%3A25%3A19%22%20TO%20%222019-05-23T18%3A48%3A50Z%22%5D%20%20and%20department%3Ait
http://localhost:9200/employee/_search?q=modified%3A%5B%222019-05-22T03%3A25%3A19%22%20TO%20%222019-05-23T18%3A48%3A50Z%22%5D%20%20and%20department%3Ait
 

Using C# to retrieve the Data

 
As a developer, we all will be curious to know how we can use the same in the code. The following code is written in C# which uses HttpClient to execute the URL and get the data.
  1. /// <summary>  
  2. ///  
  3. /// </summary>  
  4. /// <param name="url">http://localhost:9200/employee/_search?q=modified%3A%5B%222019-05-22T03%3A25%3A19%22%20TO%20%222019-05-23T18%3A48%3A50Z%22%5D%20%20and%20department%3Ait</param>  
  5. /// <returns></returns>  
  6. static dynamic ExecuteElasticQuerystring(string Elasticencodedurl) {  
  7.     dynamic jsonOutput = null;  
  8.     using(var client = new HttpClient()) {  
  9.         try {  
  10.             client.BaseAddress = new Uri(Elasticencodedurl);  
  11.             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  12.             HttpResponseMessage response = client.GetAsync(Elasticencodedurl).Result;  
  13.             if (response.IsSuccessStatusCode) {  
  14.                 var result = response.Content.ReadAsStringAsync().Result;  
  15.                 jsonOutput = Newtonsoft.Json.JsonConvert.DeserializeObject(result);  
  16.             }  
  17.         } catch (Exception ex) {  
  18.             //Write to log  
  19.         }  
  20.     }  
  21.     return jsonOutput;  
  22. }