Monday 20 June 2016

Searching data in Elasticsearch using C#

To help beginners I decided to write the article with step by step approach using Elasticsearch with C#, since it is a NOSQL, distributed full text database. Which means that this database is document based instead of using tables or schema, we use documents… lots and lots of documents. I have written this article specially focusing on newcomers and anyone new wants to learn or thinking of using ES in their .NET program. This sample illustrates a way to let user search data from Elasticsearch from their app.


Work In Progress.

Overview:

This article covers 3 major aspects
·         What is Elasticseach?
·         Getting started with ES in Visual Studio
·         Perform some search operation using C#

What is Elasticsearch?

http://www.elasticsearch.org/
ElasticSearch is a distributed RESTful search engine built for the cloud. Features include:
·         Distributed and Highly Available Search Engine.
·         Each index is fully sharded with a configurable number of shards.
·         Each shard can have one or more replicas.
·         Read / Search operations performed on either one of the replica shard.
·         Multi Tenant with Multi Types.
·         Support for more than one index.
·         Support for more than one type per index.
·         Index level configuration (number of shards, index storage, …).
·         Various set of APIs
·         HTTP RESTful API
·         Native Java API.
·         All APIs perform automatic node operation rerouting.
·         Document oriented
·         No need for upfront schema definition.
·         Schema can be defined per type for customization of the indexing process.
·         Reliable, Asynchronous Write Behind for long term persistency.
·         (Near) Real Time Search.
·         Built on top of Lucene
·         Each shard is a fully functional Lucene index
·         All the power of Lucene easily exposed through simple configuration / plugins.
·         Per operation consistency
·         Single document level operations are atomic, consistent, isolated and durable.
·         Open Source under Apache 2 License.

Installing Elasticsearch

Installing Elastic Search on a Windows server

·         Pre-requisites:
·         Java Runtime Environment (JRE)http://www.oracle.com/technetwork/java/javase/downloads/index.html
·         After installing JRE, do yourself a favor and set the JAVA___HOME environment variable
·         My Computer -> Properties -> Advanced tab -> Environment variables button
·         Under System Variables, click Add New
·         Name: JAVA___HOME
·         Path: c:\progra~1\Java\jre7
·         Assumes Java JRE is installed at c:\program files\Java\jre[X]
·         You can download the Elasticsearch runtime from the Elasticsearch website and follow the standard installation instructions (it's really quite simple)
·         OR, if you'd like the Elasticsearch engine to run as a Windows service, you can download an installer from here: http://ruilopes.com/elasticsearch-setup/
·         Basically the installer just unzips the Elasticsearch package, then creates a Windows service wrapping the engine
·         Install to [DRIVE]:\Elasticsearch (avoid installing to the Program Files directory to help avoid UAC issues)
·         An Elasticsearch service will be created, but you may need to start the service manually (Administrative tools -> Services) and set it to startup automatically
·         Confirm Elasticsearch is running by browsing to: http://localhost:9200/_cluster/health
·         If you receive a JSON response with a property named "status" that has a value of "green" or "yellow", all systems are go.
·         By default, Elasticsearch listens on port 9200.
·         I like the Kibana for cluster/node/index monitoring - https://www.elastic.co/products/kibana
·         I also like the Sense plugin for Google Chrome for testing queries -https://chrome.google.com/webstore/detail/sense/doinijnbnggojdlcjifpdckfokbbfpbo
·         Another great client elasticsearch-head also good for monitoring cluster -https://mobz.github.io/elasticsearch-head/
·         Several other options/tools available here: http://www.elasticsearch.org/guide/clients/
·         Scroll to the "Health and Performance Monitoring" and "Front Ends" sections

ElasticSearch-with-Csharp

ElasticSearch with C#
Example project for using Elasticsearch with C#. This is a autosearch feature demonstration in Elasticsearch using C#.NET. User cansearch any name by typing its first character. Example for searching user "Abby" jusy type A and it will list all the name that has A in it.

Getting Started

Download NEST package using the below command and then run the program. You can learn more about Elasticsearch.NET & NEST here.
PM> Install-Package NEST

Sample Data

You could use the attached data in the project folder and do a bulk insert into ES or you can use your own data. If you use you own data please change the index name in the Form1.cs code to your index name and type to your type name. Also you need to update the getter and setter.

Sample code will be look like as follows

C#
using Nest; 
using System; 
using System.Windows.Forms; 
 
namespace ESsearchTest 
    public partial class Form1 : Form 
    { 
        ConnectionSettings connectionSettings; 
        ElasticClient elasticClient; 
 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            //Establishing connection with ES 
            connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200/"));
 //local PC             
            elasticClient = new ElasticClient(connectionSettings); 
        } 
 
        //Get suggestion under search textbox 
        private void tbxName_TextChanged(object sender, EventArgs e) 
        { 
            //Search query to retrieve info 
            var response = elasticClient.Search<disney>(s => s 
                .Index("disney"
                .Type("character"
                .Query(q => q.QueryString(qs => qs.Query(tbxName.Text + "*")))); 
 
            AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); 
            foreach (var hit in response.Hits) 
            { 
                collection.Add(hit.Source.name); 
            } 
 
            tbxName.AutoCompleteSource = AutoCompleteSource.CustomSource; 
            tbxName.AutoCompleteMode = AutoCompleteMode.Suggest; 
            tbxName.AutoCompleteCustomSource = collection; 
 
 
 
            if (tbxName.Text == ""
            { 
                rtxSearchResult.Clear(); 
            } 
        } 
 
        private void tbxName_KeyPress(object sender, KeyPressEventArgs e) 
        { 
            if (e.KeyChar == '\b'
            { 
                rtxSearchResult.Clear(); 
            } 
        } 
    } 




Technicalities:

The application retrieves all names from Elasticsearch. That means that if a name didn't create in the index that information will not show.
Note
·         For more about query DSL please see this page Query DSL
·         More about Elasticsearch visit website


No comments:

Post a Comment

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...