0

I have one massive model (It basically is my entire app). I have 7 or more associations on this model, including many to many, :through => etc. This model also has a few simple attributes (title, url, and so on).

The only thing I care about indexing are those 3 or 4 simple attributes (title, url, description, category). The rest I don't care about.

Everything works perfectly when I use load: true, but as soon as I turn that off everything breaks. The only way to fix this it seems is to go in and add complex mappings, :touch, :touch callbacks and so on for every single association?

I hope I'm understanding this wrong because this would be a huge amount of code only to search through 3 or 4 simple attributes.

I have no idea what I'm talking about but could I maybe search through the elasticsearch index, but return a list of ID's, and just loop through those in the normal rails way?

Thanks!

Tallboy
  • 12,847
  • 13
  • 82
  • 173
  • What do you mean by everything breaks? Why is load: true unacceptable? – Frederick Cheung Sep 26 '12 at 08:00
  • From everywhere I read, it's great for debugging but you don't want to use it in production. For only 950 records it takes 1000-2000ms to return a result – Tallboy Sep 26 '12 at 15:04
  • That is, if I search a very common term (which people will be doing). For smaller terms it takes 100ms or so. – Tallboy Sep 26 '12 at 15:08
  • load just means that after the search results are returned the corresponding model objects are loaded from the DB. The performance cost depends only how many search results. If you're actually viewing 1000 search results at a time then that will add quite an overhead. If you showing results 20 items to a page then loading those 20 objects is no big deal. Personally I use it in production when I need the actual Active Record objects. – Frederick Cheung Sep 26 '12 at 15:17
  • Interesting.. well I have 20 per page, and pagination. I notice it creates a massive slowdown of almost 1 to 2 seconds. Could you perhaps look at this and see if anything about it looks wrong or could be causing it to go so slow? https://gist.github.com/3785450 – Tallboy Sep 26 '12 at 15:25
  • Weird, never seen it have that sort of impact before. – Frederick Cheung Sep 26 '12 at 16:21

1 Answers1

2

I don't know exactly how tire works internally but as far as I understood from the documentation when the load option is true it loads every record from the database. That's why I don't think you want to use it in production. Without that option Tire retrieves the information from elasticsearch, since you should have most of the data you want to display stored in elasticsearch. Probably you need to add more stored information to elasticsearch. You can do it configuring the field that you want to store in your mapping, otherwise you always have the source field in elasticsearch, which is exactly the JSON document you indexed.

The answer to your last question is yes. You can select which fields you want back from elasticsearch instead of getting back the whole source. In your case if I understood correctly you'd configure the only id field. I don't know how to do it with Tire but in terms of elasticsearch request you can do it either in the url like this:

curl localhost:9200/_search?fields=id -d '{
  "query" : {
    "match_all" : {}
  }
}'

or directly in your query like this:

{
    "fields" : ["id"],
    "query" : {
        "match_all" : {}
    }
}
javanna
  • 59,145
  • 14
  • 144
  • 125