Skip to content
This repository was archived by the owner on Apr 5, 2021. 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
14 changes: 10 additions & 4 deletions lib/data_magic/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def search_fields_and_ranges(squery, params, config)
elsif match = /(.+)__(range|ne|not)\z/.match(param)
field, operator = match.captures.map(&:to_sym)
squery = range_query(squery, operator, field, value)
elsif field_type == "integer" && value.is_a?(String) && /,/.match(value) # list of integers
squery = integer_list_query(squery, param, value)
elsif (field_type == "string" || field_type == "integer") && value.is_a?(String) && /,/.match(value) # lists of integers or strings
squery = list_query(squery, param, value, field_type)
else # field equality
squery = squery.filter.match(param => value)
end
Expand Down Expand Up @@ -121,10 +121,16 @@ def autocomplete_query(squery, field, value)
})
end

def integer_list_query(squery, field, value)
def list_query(squery, field, value, field_type)
if ( field_type == "string")
filter = value.split(',').map(&:to_str)
else
filter = value.split(',').map(&:to_i)
end

squery.filter(
terms: {
field => value.split(',').map(&:to_i) }
field => filter }
)
end

Expand Down
9 changes: 9 additions & 0 deletions spec/lib/data_magic/query_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@
it_correctly "builds a query"
end

describe "can create a query from a list of numbers as strings with leading zeros" do
before do
allow(DataMagic.config).to receive(:field_type).with(:ope8_id).and_return("string")
end
subject { { ope8_id: '00303600,01092300,00122000' } }
let(:expected_query) { { terms: { ope8_id: ['00303600','01092300','00122000'] } } }
it_correctly "builds a query"
end

describe "can search within a location" do
subject { {} }
let(:options) { { zip: "94132", distance: "30mi" } }
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/data_magic/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@
expect(response["results"][1]['name']).to eq("Los Angeles")
expect(response["results"][2]['name']).to eq("Chicago")
end

it "can match a string field on multiple numbers with leading zeros" do
response = DataMagic.search({code: "02395220,02410877,00428803,"}, sort: "code:desc")
expect(response["results"].length).to eq(3)
expect(response["results"][0]['name']).to eq("Los Angeles")
expect(response["results"][1]['name']).to eq("New York")
expect(response["results"][2]['name']).to eq("Chicago")
end
end

describe "with null fields in the data" do
Expand Down