Getting Started With Elasticsearch for .NET Developers - p-patel/software-engineer-knowledge-base GitHub Wiki

https://app.pluralsight.com/library/courses/elasticsearch-for-dotnet-developers

Getting Started

  • Clusters, shards and indexes
  • Documents and data types
  • Querying documents
  • .NET sample open source projects

What is it?

  • A nosql (document-based), distributed, full-text db

Why should I use it?

  • Speed - Index millions of documents, query across all and return results fast
  • Relevancy - apply scoring and relevance so best matching documents bubble to top of results
  • Stats - spot trends in your data

What should I use it for?

  • Blog search engine - return the most relevant results
  • Log analytics tool - identify trends in data
  • Document repository - with docs with many attributes or an unpredictable schema

Elasticsearch Concepts

  • No tables, no schema required. Data is stored as JSON documents
  • Documents are saved to an index (db), indexes are stored across multiple shards, shards are stored in one or more servers which are called nodes and this is considered a cluster
  • Simple clustering is what allows easy horizontal scaling of elasticsearch. When additional nodes are added the shards automatically redistribute themselves to achieve the most balanced distribution of data

Installation & Configuration

  • requires java runtime

  • ...

  • Marvel plugin (free for development use)

Data Types & Index Options

Sample Index and Basic Querying

Schemas (Mappings)

  • schema defined in JSON
(POST) http://localhost:9200/my_blog (where my_blog is the name of the index to be created)
{
"mappings": {
  "post": {
    "properties": {
      "user_id": {
        "type": "integer
      },
      "post_text": {
        "type": "string"
      },
      "post_date": {
        "type": "date"
      }
    }
  }
}
}
  • id is generated automatically by default

GET /_cat/indices get existing indices

{
  "mappings": ...
}

creates my_blog index with specified mappings

GET my_blog/_mapping gets the schema for my_blog schema

{
  "post_date": "2014-08-20",
  "post_text": "text",
  "user_id": 1
}

inserts a document that corresponds to the document

GET my_blog/post/_search a basic search

GET my_blog/post/<id> query for document with specified id

{
  "post_date": "2014-08-20",
  "post_text": "text with id 1",
  "user_id": 1,
}

creates a document with the specified id

GET my_blog/post/1 gets the document with the specified id

Data Types and Options

  • data types: string, numbers (float, double, byte, short, integer, long - java number types), boolean, date (UTC by default, multiple types can be specified at the same time), binary (good for image or blob, stored in base64 string, not indexed by default)

  • creating an index

  • settings section: index: number_of_shards