Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/algolia/facebook-search

Facebook Feed Search powered by Algolia
https://github.com/algolia/facebook-search

Last synced: 7 days ago
JSON representation

Facebook Feed Search powered by Algolia

Lists

README

        

Facebook Search
===============

This is the Rails 4 application providing [Facebook Search](http://facebook.algolia.com). It's based on [algoliasearch-client-ruby](https://github.com/algolia/algoliasearch-client-ruby), [omniauth-facebook](https://github.com/mkdynamic/omniauth-facebook) and [koala](https://github.com/arsduo/koala).

Index settings
----------------------

```ruby
Algolia.init application_id: ENV['ALGOLIA_APPLICATION_ID'], api_key: ENV['ALGOLIA_API_KEY']
INDEX = Algolia::Index.new("facebook_#{Rails.env}")
INDEX.set_settings({
attributesToIndex: ['unordered(description)', 'unordered(name)', 'unordered(message)', 'unordered(comments.data.message)', 'unordered(story)'],
customRanking: ['desc(likes_count)']
})
```

Record definition
------------------

```ruby
BATCH_SIZE = 1000
def crawl_feed!(token)
@graph = Koala::Facebook::API.new(token)
objects = []
feed = @graph.get_connections("me", "feed")
loop do
feed.each do |f|
objects << build_algolia_object(f)
if objects.length == BATCH_SIZE
INDEX.add_objects(objects)
objects = []
end
end
feed = feed.next_page
break if feed.nil? || feed.empty?
end
INDEX.add_objects(objects) unless objects.empty?
end

private
def build_algolia_object(f)
f.merge objectID: "#{uid}_#{f['id']}",
_tags: [ uid, f['type'] ],
created_time: DateTime.parse(f['created_time']).to_i,
likes_count: f['likes'].try(:[], 'data').try(:length).to_i
end
```

Secure indexing
----------------

Each record (item) is tagged with its owner id (Your Facebook UID) and we use a per-user [generated secured API key](http://www.algolia.com/doc#SecurityUser) to call Algolia's REST API.

```ruby
@secured_api_key = Algolia.generate_secured_api_key(ENV['ALGOLIA_API_KEY_SEARCH_ONLY'], current_user.uid)
```

```js
var algolia = new AlgoliaSearch('#{ENV['ALGOLIA_APPLICATION_ID']}', '#{@secured_api_key}');
algolia.setSecurityTags('#{current_user.uid}');
```