Elasticsearch (Setup)

We're using Elasticsearch to generate the index views for the various entities (Productions, Shows, etc.) and to search them from that same index view. Therefore, you need to setup Elasticsearch locally, too.

Setup

  1. Install Elasticsearch 6.5.x and Java JDK 1.8
    • On macOS: brew install elasticsearch and you should be done
    • Ubuntu etc:
      • https://www.elastic.co/guide/en/elasticsearch/reference/6.5/_installation.html
      • https://stackoverflow.com/questions/14788345/how-to-install-the-jdk-on-ubuntu-linux
      • https://www.elastic.co/guide/en/elasticsearch/reference/current/deb.html
      • If you're using Vagrant, in /etc/elasticsearch/elasticsearch.yml you can bind the network.host to 0.0.0.0, so within Vagrant Elastic is available on localhost, and on your host machine on the external ip (e.g. 192.168.3.3)
  2. ElasticSearch should now be running at http://localhost:9200 (or the IP of your Vagrant VM)
  3. Create the initial indices (for each model that needs an index)
    • bundle exec rake environment elasticsearch:import:model CLASS='Production'
    • Check bundle exec rake -D elasticsearch for all possible commands
    • From Rails console you can als do Production.import(force: true)
  4. Test if it works
    • From the command line
    curl -XGET "http://localhost:9200/productions/_search?q=liefde&pretty"
    
    • From Rails console
    # .records returns actual ActiveRecord records
    Production.search_published("liefde").records.map(&:title)
    # .results returns the actual Elasticsearch document (quicker, because doesn't need SQL query)
    Production.search_published("liefde").results.map(&:title)
    

Updating the index

For a more comprehensive guide, see Elasticsearch (usage)

In case you want to add a new field to an Elasticsearch document and be able to search on it, you will need to do the following:

  1. Add field to settings do block in Model
  2. Add field to as_indexed_json method in Model
  3. Update mapping in Elasticsearch (see code sample below)
  4. Run Model.import (without force: true!) from the Rails console
# This example shows what to do when you want to add a field "created_by" to the index of Productions,
# so you can search productions by their creator
new_mapping = {
  index: 'productions', # the index name
  type: '_doc',
  body: {
    properties: {
      created_by: {     # name of the field you want to add
        type: 'integer' # type and other properties for the field
      }
    }
}
Production.__elasticsearch__.client.indices.put_mapping(new_mapping)
Production.import
Last Updated: 3/28/2019, 1:25:17 PM