Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamT/799458148b9df0127b4dcb97297e144a to your computer and use it in GitHub Desktop.
Save AdamT/799458148b9df0127b4dcb97297e144a to your computer and use it in GitHub Desktop.
ElasticSearching Rails models

Up and running with ElasticSearch in Rails

  1. rails new blog

  2. cd blog

  3. rails g model Article title text:text

  4. rails db:migrate

  5. Run ElasticSearch:

docker run -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:5.2.2
  1. Confirm ElasticSearch is running on port 9200: http://localhost:9200/
  2. View existing indexes: http://localhost:9200/_cat/indices?v
  3. Add gem 'elasticsearch-model' to Gemfile
  4. Add include Elasticsearch::Model to app/models/article.rb
  5. Create index for articles:
$ Article.__elasticsearch__.create_index!
 => {"acknowledged"=>true, "shards_acknowledged"=>true}
  1. Confirm index created at http://localhost:9200/_cat/indices?v but there are no documents yet: docs.count == 0
  2. Create some articles:
Article.create(title: 'a great title', text: 'some amazing text')
Article.create(title: 'an okay title', text: 'some okay text')
Article.create(title: 'zzz', text: 'yyy')
  1. Notice this document has not been indexed: http://localhost:9200/_cat/indices?v
  2. Add document to index:
Article.import
  Article Load (0.1ms)  SELECT  "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ?  [["LIMIT", 1000]]
 => 0
  1. Notice there is now a document in the articles index
  2. To search using elasticsearch:
# Article.__elasticsearch__.search('great').results.total
Article.search('great').results.total
=> 1

# Article.__elasticsearch__.search('not found').results.total
Article.search('not found').results.total
=> 0

# Article.__elasticsearch__.search('not found').results
Article.search('not found').results
=> #<Elasticsearch::Model::Response::Results:0x007fcb02b25f00 @klass=[PROXY]
Article(id: integer, title: string, text: text, created_at: datetime, updated_at: datetime),
@response=#<Elasticsearch::Model::Response::Response:0x007fcb02b25ff0 @klass=[PROXY]
Article(id: integer, title: string, text: text, created_at: datetime, updated_at: datetime),
@search=#<Elasticsearch::Model::Searching::SearchRequest:0x007fcb02b262e8 @klass=[PROXY]
Article(id: integer, title: string, text: text, created_at: datetime, updated_at: datetime),
@options={}, @definition={:index=>"articles", :type=>"article", :q=>"not found"}>,
@results=#<Elasticsearch::Model::Response::Results:0x007fcb02b25f00 ...>>>

Article.search(query: {query_string: {query: 'zzz OR great'}}).results.total
=> 2

Article.search(query: {simple_query_string: {query: 'yyy', fields: ['text']}}).results.total
=> 1

Article.search(query: {simple_query_string: {query: 'yyy', fields: ['text']}}).first
=> #<Elasticsearch::Model::Response::Result:0x007fca2ec071c8 @result=#<Elasticsearch::Model::HashWrapper _id="3" _index="articles" _score=0.2876821 _source=#<Elasticsearch::Model::HashWrapper created_at="2017-06-08T06:31:03.153Z" id=3 text="yyy" title="zzz" updated_at="2017-06-08T06:31:03.153Z"> _type="article">>

Article.search(query: {simple_query_string: {query: 'yyy', fields: ['text']}}).to_a.first
 => #<Elasticsearch::Model::Response::Result:0x007fca2bce1060 @result=#<Elasticsearch::Model::HashWrapper _id="3" _index="articles" _score=0.2876821 _source=#<Elasticsearch::Model::HashWrapper created_at="2017-06-08T06:31:03.153Z" id=3 text="yyy" title="zzz" updated_at="2017-06-08T06:31:03.153Z"> _type="article">>
 
 Article.search(query: {simple_query_string: {query: 'yyy', fields: ['text']}}).records.first
 => #<Article id: 3, title: "zzz", text: "yyy", created_at: "2017-06-08 06:31:03", updated_at: "2017-06-08 06:31:03">

Article.search(query: {simple_query_string: {query: 'yyy', fields: ['text']}}).records.to_a
 => [#<Article id: 3, title: "zzz", text: "yyy", created_at: "2017-06-08 06:31:03", updated_at: "2017-06-08 06:31:03">]

http://localhost:9200/articles/_search

http://localhost:9200/articles/_search?q=text&size=2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment