November 6, 2017

Building a Product Catalog with Elasticsearch

What is Elasticsearch?

  • Search engine based on Lucene
  • NoSQL database
  • Communicates over HTTP

Terminology

  • Index: (equivalent to a SQL database)
  • Type: (equivalent to a SQL table)
  • Mapping:
    • Specifies what fields we expect each document to have
  • Document:

Installing Elasticsearch

brew install elasticsearch && brew info elasticsearch

Defining the Index and Mapping

(Technically an Optional Section: Defining the mapping is actually optional because Elasticsearch is schema free by default, but it is better to be more explicit about what is being stored.)

Creating an Index

PUT localhost:9200/catalog

Creating a Type

PUT localhost:9200/catalog/product/_mapping

{
  "product": {
    "properties": {
      "name": {"type": "string"},
      "description": {"type": "string"},
      "price": {"type": "long"},
    },
  }
}

Verifying the Mapping

GET localhost:9200/catalog/_mapping

Adding Data

Can also skip the section above and an index will be auto-created:

POST localhost:9200/catalog/product

{"title": "Another fly", "name": {"first": "C", "last": "D"}}

OR

PUT localhost:9200/catalog/product

{"title": "Another fly", "name": {"first": "C", "last": "D"}}

Change the data (complete update)

PUT localhost:9200/catalog/product/1

{"title": "A fly on the wall Part 2", "name": : "B"}}

Change the data (partial update)

POST locahost:9200/catalog/product/1/_update'

{"title": "A fly on the wall Part 2", "name": : "B"}}

Bulk Update

PUT localhost:9200/catalog/product/_bulk

{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{
   "name": "Blue dress",
   "description": "This is a blue dress"
   "price" : "$1.00",
}

Searching (Find all the documents)

GET localhost:9200/catalog/product/_search?pretty'

Query and search syntax

Filtering

Analysis & Tokenization

Aggregation