-
rails new blog
-
cd blog
-
rails g model Article title text:text
-
rails db:migrate
-
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
- Confirm ElasticSearch is running on port 9200: http://localhost:9200/
- View existing indexes: http://localhost:9200/_cat/indices?v
- Add
gem 'elasticsearch-model'
to Gemfile - Add
include Elasticsearch::Model
toapp/models/article.rb
- Create index for articles:
$ Article.__elasticsearch__.create_index!
=> {"acknowledged"=>true, "shards_acknowledged"=>true}
- Confirm index created at http://localhost:9200/_cat/indices?v but there are no documents yet:
docs.count == 0
- 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')
- Notice this document has not been indexed: http://localhost:9200/_cat/indices?v
- Add document to index:
Article.import
Article Load (0.1ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT ? [["LIMIT", 1000]]
=> 0
- Notice there is now a document in the articles index
- 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">]