Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associations not bringing back results #33

Closed
melhotiby opened this issue Mar 6, 2014 · 7 comments
Closed

Associations not bringing back results #33

melhotiby opened this issue Mar 6, 2014 · 7 comments

Comments

@melhotiby
Copy link

HI @karmi

I have the following relationship

class Discussion < ActiveRecord::Base
  has_many :replies, as: :replyable, dependent: :destroy
  ...
  ...

  def as_indexed_json(options={})
    self.as_json( include: { replies: { only: :body }} )
  end
end
class Reply < ActiveRecord::Base
  belongs_to :discussion
end

We also have multiple indexes which you gave use a solution from #30

require 'elasticsearch/model'

class Find
  def self.search(q)
    response = Elasticsearch::Model.search(q)
    if response.response.empty?
      []
    else
      response.results.map { |r| r['_type'].classify.constantize.where(id: r['_id']).to_a }.flatten
    end
  end
end


class MultipleModels < Array
  def client
    Elasticsearch::Model.client
  end

  def ancestors
    []
  end

  def default_per_page
    100
  end
end

module Elasticsearch::Model
  # Search multiple models
  #
  def search(query_or_payload, models=[], options={})
    models = [Article, Series, Discussion]

    models = MultipleModels.new(models)

    index_names    = models.map { |c| c.index_name }
    document_types = models.map { |c| c.document_type }

    search   = Searching::SearchRequest.new(
      models,
      query_or_payload,
      {index: index_names, type: document_types}.merge(options)
    )

    Response::Response.new(models, search)
  end

  module_function :search
end

It appears we have it set up right

Discussion.first.__elasticsearch__.as_indexed_json
=> {"title"=>"Some title",
 "updated_at"=>Thu, 24 Oct 2013 14:20:09 UTC +00:00,
 "class_name"=>"discussion",
 "replies"=>[{"body"=>"<p>Apples</p>"}, {"body"=>"<p>Orange</p>"}]}

but searching for Apples or Oranges gives us no results.

@karmi
Copy link
Contributor

karmi commented Mar 6, 2014

Can you gist/pastie the full recreation with Curl commands (trace:true)? It's hard to say what's up here.


Discussion.first.__elasticsearch__.as_indexed_json -- since you define your own as_indexed_json, just call Discussion.first.as_indexed_json.

@melhotiby
Copy link
Author

@karmi - Not sure the best way to do that...is that provided in the gem

@karmi
Copy link
Contributor

karmi commented Mar 6, 2014

@melhotiby
Copy link
Author

@karmi - the only problem I see is that Elasticsearch::Client.new trace: true and since we are not calling a client directly, im not sure where the trace goes

@karmi
Copy link
Contributor

karmi commented Mar 6, 2014

@railsdevmatt I'm getting more and more confused. Have you actually checked https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-model#the-elasticsearch-client, have you tried it? It will print to STDERR by default. The other link has a very specific advice on how to put the log/trace output into a file.

@melhotiby
Copy link
Author

@karmi - my bad. I misread that the settings needed to go in an initializer

here is the curl

curl -X GET 'http://localhost:9200/articles_development_index,series_development_index,discussions_development_index/article,series,discussion/_search?pretty' -d '{"from":0,"size":20,"filter":{"and":[{"missing":{"field":"deleted_at","existence":true,"null_value":true}},{"or":[{"not":{"exists":{"field":"published"}}},{"term":{"published":true}}]}]},"query":{"query_string":{"query":"orange","default_operator":"AND"}}}'
 |
 | # 2014-03-06T10:45:44-05:00 [200] (0.006s)
 | #
 | # {"took":1,"timed_out":false,"_shards":{"total":3,"successful":3,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}

and here is the log

|   Parameters: {"utf8"=>"✓", "q"=>"orange"}
| 2014-03-06 10:47:23 -0500: GET http://localhost:9200/articles_development_index,series_development_index,discussions_development_index/article,series,discussion/_search [status:200, request:0.006s, query:0.000s]
| 2014-03-06 10:47:23 -0500: > {"from":0,"size":20,"filter":{"and":[{"missing":{"field":"deleted_at","existence":true,"null_value":true}},{"or":[{"not":{"exists":{"field":"published"}}},{"term":{"published":true}}]}]},"query":{"query_string":{"query":"orange","default_operator":"AND"}}}
| 2014-03-06 10:47:23 -0500: < {"took":0,"timed_out":false,"_shards":{"total":3,"successful":3,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

@melhotiby
Copy link
Author

@karmi - just realized the import

Discussion.import

fixed the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants