Creating a real-time search engine with IndexTank and Heroku

本文将指导您使用现有云服务构建实时照片搜索引擎,包括选择数据源、解析数据流、建立Rails应用、关联Heroku应用、配置IndexTank索引等步骤。通过此教程,您可以轻松实现照片实时搜索。

In 1998 I created my first search engine. It was very simple: it would crawl sites for mp3 files and generate a search index twice every hour. It took me about six weeks to develop, and I also had to buy a Pentium server to host it. I paid $125/month to have it co-located with an ISP.

Today you could do the same thing for free and in a fraction of the time using existing cloud services. More importantly, you can get it started for free and only spend money to grow the service once it gains traction. I will show you how to build a real-time search engine using IndexTank as the back-end and Heroku as your application host.

The only requirement for this tutorial is to have a Heroku account with the capability of using add-ons (i.e. validated with your credit card). Of course, knowing Ruby and Rails will be helpful to understand what’s going on. Let’s do it!

First off, let’s choose something to search. Presumably we are interested in a real-time stream such as Twitter updates, blog content, etc. Any text stream with a public api will do, for the purpose of this example I chose Plixi, a social photo sharing application. My idea was to search the text associated with pictures that people post to the site.

You can read all about the plixi api here, for the purpose of this we are interested in the json version of the real-time photo stream:

http://api.plixi.com/api/tpapi.svc/json/photos?getuser=true

Here’s a snippet of code to parse that stream and extract some useful fields. Try it out (make sure you have ‘json’ installed in your gems):

fetcher.rb

  1. require 'rubygems'  
  2. require 'json'  
  3. require 'net/http'  
  4.   
  5. plixi_url='http://api.plixi.com/api/tpapi.svc/json/photos?getuser=true'  
  6. photos = JSON.parse(Net::HTTP.get_response(URI.parse(plixi_url)).body)  
  7. count, list = photos['Count'], photos['List']  
  8. list.each_with_index do |p, i|  
  9.     u = p['User']  
  10.     #only want photos that come with some text  
  11.     if p.has_key?('Message')  
  12.         id = p['GdAlias']  
  13.         text = p['Message']  
  14.         timestamp = Integer(p['UploadDate'])  
  15.         screen_name = u['ScreenName']  
  16.         thumbnail_url = p['ThumbnailUrl']  
  17.         printf "%s,%s,%s\n", id, screen_name, text  
  18.     end  
  19. end  

So we have an interesting data stream. How do we index it and search it? First, let’s create a new Rails app (we are using Rails 3 for this, it will be different for older versions) and associate it with a Heroku app (make sure the heroku gem is installed, if not: $ sudo gem install heroku).

$ rails new plixidemo
$ cd plixidemo

Associate this rails app with a Heroku app

$ git init
$ heroku create

Request the IndexTank add-on for this app and download the IndexTank gem for local testing

$ heroku addons:add indextank:trial
$ gem install indextank

We need to add the following lines to plixidemo/Gemfile so that our app can use the Indextank client:

gem 'indextank', '1.0.10'
gem 'json_pure', '1.4.6', :require => 'json'

Let’s create two models under app/models. This will be the heart of our application. First, a photo:

app/models/photo.rb

  1. class Photo  
  2.   def initialize(data)  
  3.     @data = data  
  4.   end  
  5.   
  6.   def id  
  7.     @data['Id']  
  8.   end  
  9.   
  10.   def screen_name  
  11.     self.user['ScreenName']  
  12.   end  
  13.   
  14.   def to_document  
  15.     {  
  16.       :plixi_id      => self.id,  
  17.       :text          => self.message,  
  18.       :timestamp     => self.upload_date.to_i,  
  19.       :screen_name   => self.screen_name,  
  20.       :thumbnail_url => self.thumbnail_url  
  21.     }  
  22.   end  
  23.   
  24.   def method_missing(name, *args, &block)  
  25.     if @data[name.to_s.classify]  
  26.       @data[name.to_s.classify]  
  27.     else  
  28.       super  
  29.     end  
  30.   end  
  31. end  

Second, a searcher that knows how to communicate with the indextank api:

app/models/photo_searcher.rb

  1. require 'open-uri'  
  2.   
  3. class PhotoSearcher  
  4.   def self.index  
  5.     @api  = IndexTank::Client.new(ENV['INDEXTANK_API_URL'] || 'http://your_api_url')  
  6.     @index ||= @api.indexes('idx')  
  7.     @index  
  8.   end  
  9.   
  10.   # retrieve photos from IndexTank  
  11.   def self.search(query)  
  12.     index.search(query, :fetch=>'text,thumbnail_url,screen_name,plixi_id,timestamp')  
  13.   end  
  14.   
  15. end  

note: ‘your_api_url’ can be found on heroku.com:

My Apps -> [your app] -> Add-ons -> IndexTank Search

or, from the command line:

$ heroku config --long|grep INDEXTANK

And one controller, app/controllers/photos_controller.rb

  1. class PhotosController < ApplicationController  
  2.   def index  
  3.     @docs = PhotoSearcher.search(params[:query]) if params[:query].present?  
  4.   end  
  5. end  

Of course, we need a view for our search page: app/views/photos/index.html.erb

  1. <%= form_tag photos_path, :method => :get do %>  
  2.   <%= text_field_tag :query %>  
  3.   <button type="submit">Search</button>  
  4. <% end %>  
  5.   
  6. <% if @docs %>  
  7.   <p id="result-count">Your search for "<%= params[:query] %>" returned <%= pluralize @docs['matches'], 'result' %></p>  
  8.   
  9.   <ul id="results">  
  10.     <% @docs['results'].each do |doc| %>  
  11.       <li>  
  12.         <%= link_to "http://plixi.com/p/#{doc['plixi_id']}" do %>  
  13.           <%= image_tag doc['thumbnail_url'] %>  
  14.           <%= doc['screen_name'] %> -  
  15.           <%= time_ago_in_words Time.at(doc['timestamp'].to_i) %> ago  
  16.   
  17.           <%= simple_format doc['text'] %>  
  18.         <% end %>  
  19.       </li>  
  20.     <% end %>  
  21.   </ul>  
  22. <% end %>  

And add the following to your config/routes.rb

  1. resources :photos:only => [:index]  
  2. root :to => 'photos#index'  

Remember to remove public/index.html and launch your app. If you go to it with your browser to http://localhost:3000, you should see a search box. Your index is empty, so all queries should return zero results.

It’s time to index some documents. Let’s go back to fetcher.rb from the beginning of the tutorial and add the following lines:

After rubygems:

  1. require ‘indextank’  

After the plixi_url line:

  1. api = IndexTank::Client.new(ENV['INDEXTANK_API_URL'] || '<API_URL>')  
  2. index = api.indexes 'idx'  

[note: you can find out your <API_URL> by selecting IndexTank Search from the add-ons pull-down menu for your Heroku app]

And the most important line of all, after the printf statement:

  1. index.document(i.to_s).add({:plixi_id => id, :text => text, :timestamp => timestamp, :screen_name => screen_name, :thumbnail_url => thumbnail_url})  

Now, run fetcher.rb and all the output you see will be indexed. Go back to your app and search for anything you just saw, it will show up in the search results right away!

We are ready to upload this to Heroku.

git add .
git commit -a
git push heroku master

You now have a real-time photo search engine running at [your_app].heroku.com! Next steps:

  • Change the fetcher to keep updating your index (remember that you can index up to 5000 documents). Be polite and do not hit Plixi more than a few times per minute!
  • Learn how to use our auto-complete service and build a pretty web app.

Check out our demo app here:

http://plixitank.heroku.com/

豌豆代理(又称豌豆 IP)是一款一站式国内代理 IP 服务平台,主打高匿名、低延迟、高可用的 IP 资源,支持 HTTP/HTTPS/SOCKS5 协议,适配 Windows、Mac、Android、iOS 多平台。 多类型 IP 资源与高覆盖节点 提供动态住宅 IP、静态独享 IP、数据中心 IP,覆盖全国 200 + 城市,可用率 99%+;支持省市精准选择或全国混拨,适配不同业务合规与稳定性需求。 使用:在客户端 “节点 / 线路” 页,按城市 / 类型筛选,一键连接目标 IP,适合爬虫、电商多账号运营等场景。 秒级 IP 切换与灵活调度 支持手动一键切换、秒级动态切换(切换速度低至 100ms)、定时切换(自定义时长),并自动过滤重复 IP,避免重复使用导致风险。 使用:在 “设置” 中开启 “自动切换” 并设时间间隔,或按 Ctrl+Q 快捷键一键换 IP,适配反爬虫、批量测试等高频切换场景。 全协议支持与多端适配 兼容 HTTP/HTTPS/SOCKS5 主流代理协议,可对接浏览器、爬虫脚本、客户端软件;支持 Windows、Mac、安卓、iOS 多端同步使用,跨设备无缝切换。 使用:在客户端 “协议设置” 选择对应协议,生成代理地址与端口,直接填入目标软件即可生效。 隐私安全与数据加密 自研传输加密技术保护数据传输,搭配高匿名 IP 隐藏真实地址,同时支持自动清除 Cookie / 缓存,降低隐私泄露与追踪风险。 使用:在 “安全设置” 中开启 “数据加密” 与 “自动清理缓存”,公共 WiFi 环境下优先启用,提升隐私防护等级。 智能筛选与稳定网络优化 系统自动筛选低延迟、高可用 IP,过滤失效 / 重复地址;依托自建纯净机房与独享带宽,搭配 BGP 多线接入,保障连接稳定性与速度。 使用:无需手动配置,客户端默认智能匹配合适节点,复杂网络环境可在 “网络
在网络高速发展的时代,众多的软件被开发出来,给用户带来了很大的选择余地,而且人们越来越追求更个性的需求。在这种时代背景下,商家只能以用户为导向,以商品的持续创新作为商家最重要的事项。 在新发展的时代,人们对幼儿资源互助共享平台越来越重视,才能实现幼儿资源互助共享平台的有效发挥,本文将通过幼儿资源互助共享平台的信息,分析在日常生活中对幼儿资源互助共享平台存在哪些问题探讨出进一步提升效率,管理能力的对策。 系统采用了Java技术,将所有模块采用以浏览器交互的模式,选择MySQL作为系统的数据库,来进行系统的设计。基本实现了幼儿资源互助共享平台应有的主要功能模块,本系统有管理员:首页、个人中心、用户管理、卖家管理、咨询师管理、萌宝信息管理、幼儿知识管理、保姆推荐管理、音频资源管理、二手商品管理、商品分类管理、资源分类管理、交流论坛、系统管理,用户;首页、个人中心、萌宝信息管理、保姆推荐管理、音频资源管理,卖家;首页、个人中心、二手商品管理、订单管理,咨询师;首页、个人中心、幼儿知识管理,前台首页;首页、萌宝信息、幼儿知识、保姆推荐、音频资源、二手商品、交流论坛、个人中心、后台管理、购物车等功能。 对系统进行测试后,改善了程序逻辑和代码。同时确保系统中所有的程序都能正常运行,所有的功能都能操作,本系统的开发获取幼儿资源互助共享平台信息能够更加方便快捷,同时也使幼儿资源互助共享平台信息变的更加系统化、有序化。系统界面较友好,易于操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值