Skip to content
This repository was archived by the owner on Nov 7, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ db/*.db
.env
.*.env
.cfignore
cf-ssh.yml
.vagrant
.idea/
*profile*
Expand Down
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ By default, using the `_sort_` option returns records sorted into ascending orde
When the dataset includes a `location` at the root level (`location.lat` and
`location.lon`) then the documents will be indexed geographically. You can use the `_zip` and `_distance` options to narrow query results down to those within a geographic area. For example, `_zip=12345&_distance=10mi` will return only those results within 10 miles of the center of the given zip code.

**Note: Due to a bug, specifying `location` and its sub-fields in certain field or option parameters may cause an error.** This is a known problem and tracked in this GitHub issue: _[Querying for a school by name and returning location.lon does not work](https://github.com/18F/open-data-maker/issues/227)_
Additionally, you can request `location.lat` and `location.lat` in a search that includes a `_fields` filter and it will return the record(s) with respective lat and/or lon coordinates.

#### Additional Notes on Geographic Filtering

Expand Down
7 changes: 6 additions & 1 deletion lib/data_magic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def self.nested_object_type(hash)
def self.create_index(es_index_name = nil, field_types={})
logger.info "create_index field_types: #{field_types.inspect[0..500]}"
es_index_name ||= self.config.scoped_index_name
field_types['location'] = 'geo_point'
field_types['location'] = 'lat_lon' # custom lat_lon type maps to geo_point with additional field options
es_types = NestedHash.new.add(es_field_types(field_types))
nested_object_type(es_types)
begin
Expand Down Expand Up @@ -234,6 +234,11 @@ def self.es_field_types(field_types)
index_analyzer: 'autocomplete_index',
search_analyzer: 'autocomplete_search'
},
'lat_lon' => { type: 'geo_point',
lat_lon: true,
store: true

}
}
field_types.each_with_object({}) do |(key, type), result|
result[key] = custom_type[type]
Expand Down
1 change: 0 additions & 1 deletion lib/data_magic/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ def field_types
logger.info "field_types #{fields.inspect}"
fields.each do |field_name, info|
type = info['type'] || "string"
type = nil if field_name == 'location.lat' || field_name == 'location.lon'
#logger.info "field #{field_name}: #{type.inspect}"
@field_types[field_name] = type unless type.nil?
if type == 'name' || type == 'autocomplete'
Expand Down
4 changes: 2 additions & 2 deletions spec/features/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@

expect(result.length).to eq 2

expect(result[0]).to eq %w(id code name state population area.land area.water location.lat location.lon)
expect(result[1]).to eq %w(1714000 00428803 Chicago IL 2695598 227.635 6.479 41.837551 -87.681844)
expect(result[0]).to eq %w(id code name state population location.lat location.lon area.land area.water)
expect(result[1]).to eq %w(1714000 00428803 Chicago IL 2695598 41.837551 -87.681844 227.635 6.479 )
end
end

Expand Down
16 changes: 10 additions & 6 deletions spec/lib/data_magic/config_field_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@
end
end

it "supports special case for location fields as nil" do
# special case for location in create_index
it "supports location.lat and location.lon fields" do
allow(config).to receive(:file_config).and_return([{'name' => 'one.csv'}])
allow(config).to receive(:dictionary).and_return(
IndifferentHash.new 'location.lat': {source:'LAT_COLUMN'},
'location.lon': {source:'LON_COLUMN'}

IndifferentHash.new 'location.lat': {source:'LAT_COLUMN', type: 'float'},
'location.lon': {source:'LON_COLUMN', type: 'float'}
)
expect(config.field_types).to eq(
{
'location.lat'=>'float',
'location.lon'=>'float'
}
)
expect(config.field_types).to eq({})
end
end
12 changes: 12 additions & 0 deletions spec/lib/data_magic/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@
expected['metadata']["total"] = expected["results"].length
expect(result).to eq(expected)
end

it "#search with a fields filter can return location.lat and location.lon values" do
sf_location = { lat: 37.727239, lon: -123.032229 }
DataMagic.logger.debug "sfo_location[:lat] #{sf_location[:lat].class} #{sf_location[:lat].inspect}"
response = DataMagic.search({city: "San Francisco"}, {:fields => ["location.lat", "location.lon"]})
result = response["results"][0]
expect(result.keys.length).to eq(2)
expect(result).to include("location.lat")
expect(result).to include("location.lat")
expect(result["location.lat"]).to eq sf_location[:lat]
expect(result["location.lon"]).to eq sf_location[:lon]
end
end

describe "with sample-data" do
Expand Down