build a-search-page-with-elastic search-and-dot-net

6
How to Build A Search Page With ElasticSearch and .Net

Upload: sonia-merchant

Post on 09-Jan-2017

25 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Build a-search-page-with-elastic search-and-dot-net

How to Build A Search Page With ElasticSearch and .Net

Page 2: Build a-search-page-with-elastic search-and-dot-net

ElasticSearch is credited to achieve fast search responses because it searches an index instead of searching the context directly.

Very much like retrieving pages in a book related to a word by scanning the index at the back of a book, as instead of searching every word in every page of the book.

This type of index is known as the inverted index because it inverts a page-centric data structure (page->words) to a keyword-centric data structure (word->pages).

This uses Apache Lucene to create and manage this inverted index.

Page 3: Build a-search-page-with-elastic search-and-dot-net

NEST

To interact with Elasticsearch, NEST 2.3.0 is used which is one of two official .NET clients for Elasticsearch. NEST is a high-level client which tracks closely to Elasticsearch API. All the request and response objects are mapped. NEST provides the alternatives of either a syntax for creating queries, which resembles the structure of raw JSON requests to API, or the use of object initializer syntax.

In order to build a web page, use Single Page Application (SPA) approach with AngularJS as MVVM framework. The client side will send AJAX requests to ASP.NET Web API 2. The Web API 2 controller will use NEST to communicate with Elasticsearch.

Page 4: Build a-search-page-with-elastic search-and-dot-net

Installation of Elasticsearch

Visit the web page, download an installer, unzip it and install in the mentioned three simple steps. It exposes an HTTP API so it is easy to use cURL to make requests but its recommended to use Sense which is Chrome extension. The Elasticsearch reference contains samples in a format called cURL.

Although Elasticsearch is able to resolve the document type and its fields at index time, field mappings can be override or use attributes on fields in order to give for more advanced usages.

In this example, the POCO class is decorated with some attributes so we need to create mappings with AutoMap.

Page 5: Build a-search-page-with-elastic search-and-dot-net

After defining our mappings and created an index, we can seed it with documents. Elasticsearch does not give any handler to import specific file formats such as XML or CSV, but because it has client libraries for different languages, it is easy to build your own importer. As StackOverflow dump is in XML format, use .NET XmlReader class to read question rows, map them to an instance of Post and add objects to the collection.

With this we conclude for today, stay connected with us for further discussion on this in our next article.

If you want to learn ASP.Net and perfect yourself in .NET training, then CRB Tech Solutions would be of great help and support for you. Join us with our updated program in ASP.Net course.

Stay tuned to CRB Tech reviews for more technical optimization and other resources.

Page 6: Build a-search-page-with-elastic search-and-dot-net

Thank You.....!