Elasticsearch requires*all* associations to be mapped?

一个人想着一个人 提交于 2019-12-13 04:27:43

问题


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!


回答1:


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" : {}
    }
}


来源:https://stackoverflow.com/questions/12593405/elasticsearch-requiresall-associations-to-be-mapped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!