Getting started with Ruby on RailsRoutingActiveRecordViewsActiveRecord MigrationsRails Best PracticesNaming ConventionsActionCableActiveModelUser Authentication in RailsActiveRecord AssociationsActiveRecord ValidationsActiveRecord Query InterfaceActionMailerRails generate commandsConfigurationI18n - InternationalizationUsing GoogleMaps with RailsFile UploadsCachingActionControllerConfigurationSafe ConstantizeRails 5Authorization with CanCanMongoidGemsChange default timezoneAsset PipelineUpgrading RailsActiveRecord LockingDebuggingConfigure Angular with RailsRails loggerPrawn PDFRails APIDeploying a Rails app on HerokuActiveSupportForm HelpersActiveRecord TransactionsRSpec and Ruby on RailsDecorator patternElasticsearchReact with Rails using react-rails gemRails Cookbook - Advanced rails recipes/learnings and coding techniquesMultipurpose ActiveRecord columnsClass OrganizationShallow RoutingModel states: AASMRails 5 API AutheticationTesting Rails ApplicationsActive JobsRails frameworks over the yearsAdd Admin PanelNested form in Ruby on RailsFactory GirlImport whole CSV files from specific folderTools for Ruby on Rails code optimization and cleanupActiveJobActive Model SerializersRails Engine - Modular RailsSingle Table InheritanceActiveRecord TransactionsTurbolinksFriendly IDSecurely storing authentication keysAuthenticate Api using DeviseIntegrating React.js with Rails Using HyperloopChange a default Rails application enviornmentReserved WordsRails -EnginesAdding an Amazon RDS to your rails applicationPayment feature in railsRails on docker

Elasticsearch

Other topics

Installation and testing

The first thing you want to do for local development is install ElasticSearch in your machine and test it to see if it is running. It requires Java to be installed. The installation is pretty straightforward:

  • Mac OS X: brew install elasticsearch
  • Ubuntu: sudo apt-get install elasticsearch

Then start it:

  • Mac OS X: brew services start elasticsearch
  • Ubuntu: sudo service elasticsearch start

For testing it, the easiest way is with curl. It might take a few seconds for it to start, so don't panic if you don't get any response at first.

curl localhost:9200

Example response:

{
  "name" : "Hydro-Man",
  "cluster_name" : "elasticsearch_gkbonetti",
  "version" : {
    "number" : "2.3.5",
    "build_hash" : "90f439ff60a3c0f497f91663701e64ccd01edbb4",
    "build_timestamp" : "2016-07-27T10:36:52Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

Setting up tools for development

When you are getting started with ElasticSearch (ES) it might be good to have a graphical tool that helps you explore your data. A plugin called elasticsearch-head does just that. To install it, do the following:

  • Find out in which folder ES is installed: ls -l $(which elasticsearch)
  • cd into this folder and run the plugin installation binary: elasticsearch/bin/plugin -install mobz/elasticsearch-head
  • Open http://localhost:9200/_plugin/head/ in your browser

If everything worked as expected you should be seeing a nice GUI where you can explore your data.

Introduction

ElasticSearch has a well-documented JSON API, but you'll probably want to use some libraries that handle that for you:

  • Elasticsearch - the official low level wrapper for the HTTP API

  • Elasticsearch-rails - the official high level Rails integration that helps you to connect your Rails models with ElasticSearch using either ActiveRecord or Repository pattern

  • Chewy - An alternative, non-official high level Rails integration that is very popular and arguably has better documentation

Let's use the first option for testing the connection:

gem install elasticsearch

Then fire up the ruby terminal and try it out:

require 'elasticsearch'

client = Elasticsearch::Client.new log: true
# by default it connects to http://localhost:9200

client.transport.reload_connections!
client.cluster.health

client.search q: 'test'

Searchkick

If you want to setup quickly elasticsearch you can use the searchkick gem :

gem 'searchkick'

Add searchkick to models you want to search.

class Product < ActiveRecord::Base
  searchkick
end

Add data to the search index.

Product.reindex

And to query, use:

products = Product.search "apples"
products.each do |product|
  puts product.name
end

Pretty quick, elasticsearch knowledge not required ;-)

More information here : https://github.com/ankane/searchkick

Contributors

Topic Id: 6500

Example Ids: 22309,22310,22311,22821

This site is not affiliated with any of the contributors.