Skip to content

Instantly share code, notes, and snippets.

@demirhanaydin
Last active December 22, 2015 23:48
Show Gist options
  • Save demirhanaydin/6548883 to your computer and use it in GitHub Desktop.
Save demirhanaydin/6548883 to your computer and use it in GitHub Desktop.
Elasticsearch parent/child example with tire
require 'rubygems'
require 'tire'
Tire.configure { logger 'elasticsearch.log', :level => 'debug' }
class Document
attr_accessor :title, :content
include Tire::Model::Persistence
include Tire::Model::Search
include Tire::Model::Callbacks
indexes :title, :type => 'string'
indexes :content, :type => 'string'
def to_indexed_json
{title: self.title, content: self.content}.to_json
end
end
class Comment
attr_accessor :text, :author, :date
include Tire::Model::Persistence
include Tire::Model::Search
include Tire::Model::Callbacks
mapping :_parent => { :type => 'document' } do
indexes :text, :type => 'string'
indexes :author, :type => 'string'
indexes :date, :type => 'string'
end
def to_indexed_json
{text: self.text, author: self.author, date: self.date}.to_json
end
end
Tire.index "documents" do
create :mappings => Document.mapping_to_hash, :settings => Document.settings
Tire::Configuration.client.put(
"#{Tire::Configuration.url}/#{Document.document_type.pluralize}/#{Comment.document_type}/_mapping",
Comment.mapping_to_hash.to_json)
unless aliases.map(&:name).include? 'comments'
Tire.index('comments').delete
# create an alies from comments to documents
add_alias 'comments'
end
end
doc = Document.new(id: 1, title: "Habemus Papam", content: "In Rome, white smoke rose from the chimney atop of the Sistine Chapel.")
Document.tire.index.store(doc)
# doc.save
comment1 = Comment.new(id: 2, text: "Oh my god ...", author: "John Doe", date: "2013-03-11T13:13:13")
comment2 = Comment.new(id: 3, text: "Finally!", author: "Jane Roe", date: "2013-03-10T14:14:14")
comment3 = Comment.new(id: 4, text: "this is a comment", author: "John Del Rey", date: "2012-03-10T14:14:14")
Comment.tire.index.store(comment1, :parent => 1)
Comment.tire.index.store(comment2, :parent => 1)
Comment.tire.index.store(comment3, :parent => 1)
# comment1.save
# comment2.save
sleep 2
################################################################################
# Has child query example
parent_query = Tire::Search::Search.new(Document.tire.index.name) do
query do
match "title", "papam"
end
end
parent_facets = Tire::Search::Search.new do
facet 'title' do
terms :title
end
end
## has_child example
payload = { query: {},
filter: {
has_child: {
type: 'comment',
query: {
match: {
author: {
query: "John Doe",
operator: "and"
}
}
}
}
}
}
payload.merge!(parent_query.to_hash)
payload.update(parent_facets.to_hash)
documents = Tire.search 'documents/document', payload
# to_json output
# {"query":{"match":{"title":{"query":"papam"}}},"filter":{"has_child":{"type":"comment","query":{"match":{"author":{"query":"John Doe","operator":"and"}}}}},"facets":{"title":{"terms":{"field":"title","size":10,"all_terms":false}}}}
puts 'Query:', documents.to_curl
puts 'Results:', documents.results.to_a.inspect
################################################################################
# Has parent query example
comment_query = Tire::Search::Search.new(Comment.tire.index.name) do
query do
match "author", "john"
end
end
has_parent_payload = { query: {},
filter: {
has_parent: {
type: 'document',
query: {
match: {
title: {
query: "papam"
}
}
}
}
}
}
has_parent_payload.merge!(comment_query.to_hash)
comments = Tire.search 'documents/comment', has_parent_payload
# to_json output
# {"query":{"match":{"author":{"query":"john"}}},"filter":{"has_parent":{"type":"document","query":{"match":{"title":{"query":"papam"}}}}}}
puts 'Query:', comments.to_curl
puts 'Results:', comments.results.to_a.inspect
@apneadiving
Copy link

thank you for this gist

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