Skip to content

Instantly share code, notes, and snippets.

@karmi
Created June 28, 2011 14:13
Show Gist options
  • Select an option

  • Save karmi/1051213 to your computer and use it in GitHub Desktop.

Select an option

Save karmi/1051213 to your computer and use it in GitHub Desktop.

Revisions

  1. karmi revised this gist Aug 1, 2012. 1 changed file with 29 additions and 7 deletions.
    36 changes: 29 additions & 7 deletions geo_distance_filter_in_tire.rb
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,47 @@
    require 'rubygems'
    require 'tire'

    require 'active_support/core_ext/numeric'
    require 'active_support/core_ext/time/zones'

    # Tire.configure { logger STDERR, level: 'debug' }

    class Time; DATE_FORMATS.update lucene: "%Y-%m-%dT%H:%M"; end

    Tire.index 'venues' do
    delete

    # 1) Create the index with proper mapping for the `location` property
    create :mappings => { :venue => { :properties => { :location => { :type => 'geo_point' } } } }
    create mappings: {
    venue: {
    properties: {
    location: { type: 'geo_point' },
    updated_at: { type: 'date', format: 'date_hour_minute' }
    }
    }
    }

    # 2) Store some example documents of the `venue` type
    store :venue, :name => 'One', :location => [40.01, -70.01]
    store :venue, :name => 'Two', :location => [50, 15]
    store type: 'venue', name: 'One', location: [40.1, -70.1], updated_at: 3.hours.ago.utc.to_s(:lucene)
    store type: 'venue', name: 'Two', location: [40.2, -70.2], updated_at: 2.hours.ago.utc.to_s(:lucene)
    store type: 'venue', name: 'Three', location: [50, 15], updated_at: 1.hour .ago.utc.to_s(:lucene)

    refresh
    end

    s = Tire.search 'venues' do

    # 3) Search based on geo distance
    filter 'geo_distance', :distance => '12km', :location => [40, -70]
    filter 'geo_distance', distance: '100km', location: [40, -70]

    # 4) Sort by distance to origin
    # sort { by :_geo_distance, location: [40, -70] }

    # 5) Sort by `updated_at` property
    sort { by :updated_at, 'desc' }
    end

    s.results.each do |document|
    p document
    puts s.to_curl, '-'*80

    s.results.each do |d|
    puts "* #{d.name} | location: #{d.location}, updated_at: #{d.updated_at}"
    end
  2. karmi created this gist Jun 28, 2011.
    25 changes: 25 additions & 0 deletions geo_distance_filter_in_tire.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    require 'rubygems'
    require 'tire'

    Tire.index 'venues' do
    delete

    # 1) Create the index with proper mapping for the `location` property
    create :mappings => { :venue => { :properties => { :location => { :type => 'geo_point' } } } }

    # 2) Store some example documents of the `venue` type
    store :venue, :name => 'One', :location => [40.01, -70.01]
    store :venue, :name => 'Two', :location => [50, 15]

    refresh
    end

    s = Tire.search 'venues' do

    # 3) Search based on geo distance
    filter 'geo_distance', :distance => '12km', :location => [40, -70]
    end

    s.results.each do |document|
    p document
    end