Skip to content

Commit

Permalink
Change default operator in query string queries to AND #464
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 12, 2020
1 parent 3ba2d44 commit 8d58b43
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
34 changes: 3 additions & 31 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def query(query, options={})

# filters for some classes
if self.name == "Provider"
must << { query_string: { query: query, fields: query_fields }} if query.present?
must << { query_string: { query: query, fields: query_fields, default_operator: "AND" }} if query.present?
must << { range: { created: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { range: { updated: { gte: "#{options[:from_date]}||/d" }}} if options[:from_date].present?
must << { range: { updated: { lte: "#{options[:until_date]}||/d" }}} if options[:until_date].present?
Expand All @@ -230,7 +230,7 @@ def query(query, options={})
must_not << { term: { role_name: "ROLE_ADMIN" }}
end
elsif self.name == "Client"
must << { query_string: { query: query, fields: query_fields }} if query.present?
must << { query_string: { query: query, fields: query_fields, default_operator: "AND" }} if query.present?
must << { range: { created: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { range: { updated: { gte: "#{options[:from_date]}||/d" }}} if options[:from_date].present?
must << { range: { updated: { lte: "#{options[:until_date]}||/d" }}} if options[:until_date].present?
Expand All @@ -244,36 +244,8 @@ def query(query, options={})
must << { term: { client_type: options[:client_type] }} if options[:client_type].present?
must_not << { exists: { field: "deleted_at" }} unless options[:include_deleted]
must_not << { terms: { uid: %w(crossref.citations medra.citations jalc.citations kisti.citations op.citations) }} if options[:exclude_registration_agencies]
elsif self.name == "Doi"
must << { query_string: { query: query, fields: query_fields }} if query.present?
must << { term: { "types.resourceTypeGeneral": options[:resource_type_id].underscore.camelize }} if options[:resource_type_id].present?
must << { terms: { provider_id: options[:provider_id].split(",") }} if options[:provider_id].present?
must << { terms: { client_id: options[:client_id].to_s.split(",") }} if options[:client_id].present?
must << { term: { uid: options[:uid] }} if options[:uid].present?
must << { range: { created: { gte: "#{options[:created].split(",").min}||/y", lte: "#{options[:created].split(",").max}||/y", format: "yyyy" }}} if options[:created].present?
must << { terms: { prefix: options[:prefix].to_s.split(",") }} if options[:prefix].present?
must << { terms: { aasm_state: options[:state].to_s.split(",") }} if options[:state].present?
must << { range: { registered: { gte: "#{options[:registered].split(",").min}||/y", lte: "#{options[:registered].split(",").max}||/y", format: "yyyy" }}} if options[:registered].present?
must << { term: { "creators.nameIdentifiers.nameIdentifier" => "https://orcid.org/#{options[:user_id]}" }} if options[:user_id].present?
must << { term: { "creators.affiliation.affiliationIdentifier" => URI.decode(options[:affiliation_id]) }} if options[:affiliation_id].present?
must << { term: { consortium_id: options[:consortium_id] }} if options[:consortium_id].present?
must << { term: { "client.re3data_id" => options[:re3data_id].gsub("/", '\/').upcase }} if options[:re3data_id].present?
must << { term: { "client.opendoar_id" => options[:opendoar_id] }} if options[:opendoar_id].present?
must << { terms: { "client.certificate" => options[:certificate].split(",") }} if options[:certificate].present?
must << { term: { schema_version: "http://datacite.org/schema/kernel-#{options[:schema_version]}" }} if options[:schema_version].present?
must << { terms: { "subjects.subject": options[:subject].split(",") }} if options[:subject].present?
must << { term: { source: options[:source] }} if options[:source].present?
must << { term: { "landing_page.status": options[:link_check_status] }} if options[:link_check_status].present?
must << { exists: { field: "landing_page.checked" }} if options[:link_checked].present?
must << { term: { "landing_page.hasSchemaOrg": options[:link_check_has_schema_org] }} if options[:link_check_has_schema_org].present?
must << { term: { "landing_page.bodyHasPid": options[:link_check_body_has_pid] }} if options[:link_check_body_has_pid].present?
must << { exists: { field: "landing_page.schemaOrgId" }} if options[:link_check_found_schema_org_id].present?
must << { exists: { field: "landing_page.dcIdentifier" }} if options[:link_check_found_dc_identifier].present?
must << { exists: { field: "landing_page.citationDoi" }} if options[:link_check_found_citation_doi].present?
must << { range: { "landing_page.redirectCount": { "gte": options[:link_check_redirect_count_gte] } } } if options[:link_check_redirect_count_gte].present?
must_not << { terms: { "client.uid" => %w(crossref.citations medra.citations jalc.citations kisti.citations op.citations) }} if options[:exclude_registration_agencies]
elsif self.name == "Event"
must << { query_string: { query: query, fields: query_fields }} if query.present?
must << { query_string: { query: query, fields: query_fields, default_operator: "AND" }} if query.present?
must << { term: { subj_id: URI.decode(options[:subj_id]) }} if options[:subj_id].present?
must << { term: { obj_id: URI.decode(options[:obj_id]) }} if options[:obj_id].present?
must << { term: { citation_type: options[:citation_type] }} if options[:citation_type].present?
Expand Down
2 changes: 1 addition & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def self.query(query, options={})
must = []
must_not = []

must << { query_string: { query: query, fields: query_fields } } if query.present?
must << { query_string: { query: query, fields: query_fields, default_operator: "AND", phrase_slop: 1 } } if query.present?
must << { terms: { doi: options[:ids].map(&:upcase) }} if options[:ids].present?
must << { term: { "types.resourceTypeGeneral": options[:resource_type_id].underscore.camelize }} if options[:resource_type_id].present?
must << { terms: { "types.resourceType": options[:resource_type].split(",") }} if options[:resource_type].present?
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
let!(:doi) { create(:doi, client: client, aasm_state: "findable", creators:
[{
"familyName" => "Garza",
"givenName" => "Kristian",
"name" => "Garza, Kristian",
"givenName" => "Kristian J.",
"name" => "Garza, Kristian J.",
"nameIdentifiers" => [{"nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID", "schemeUri"=>"https://orcid.org"}],
"nameType" => "Personal",
"affiliation": [
Expand Down Expand Up @@ -85,23 +85,23 @@

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian", "nameType"=>"Personal", "givenName"=>"Kristian", "familyName"=>"Garza", "affiliation"=>["Freie Universität Berlin"], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian J.", "nameType"=>"Personal", "givenName"=>"Kristian J.", "familyName"=>"Garza", "affiliation"=>["Freie Universität Berlin"], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
end

it 'returns dois with orcid id', vcr: true do
get "/dois?user-id=orcid.org/0000-0003-3484-6875", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian", "nameType"=>"Personal", "givenName"=>"Kristian", "familyName"=>"Garza", "affiliation"=>["Freie Universität Berlin"], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian J.", "nameType"=>"Personal", "givenName"=>"Kristian J.", "familyName"=>"Garza", "affiliation"=>["Freie Universität Berlin"], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
end

it 'returns dois with orcid id as url', vcr: true do
get "/dois?user-id=https://orcid.org/0000-0003-3484-6875", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian", "nameType"=>"Personal", "givenName"=>"Kristian", "familyName"=>"Garza", "affiliation"=>["Freie Universität Berlin"], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian J.", "nameType"=>"Personal", "givenName"=>"Kristian J.", "familyName"=>"Garza", "affiliation"=>["Freie Universität Berlin"], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
end

it 'returns dois with crossref funder id', vcr: true do
Expand All @@ -125,23 +125,23 @@

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian", "nameType"=>"Personal", "givenName"=>"Kristian", "familyName"=>"Garza", "affiliation"=>[{"name"=>"Freie Universität Berlin", "affiliationIdentifier"=>"https://ror.org/046ak2485", "affiliationIdentifierScheme"=>"ROR"}], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian J.", "nameType"=>"Personal", "givenName"=>"Kristian J.", "familyName"=>"Garza", "affiliation"=>[{"name"=>"Freie Universität Berlin", "affiliationIdentifier"=>"https://ror.org/046ak2485", "affiliationIdentifierScheme"=>"ROR"}], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
end

it 'returns dois with ror id', vcr: true do
get "/dois?affiliation-id=ror.org/046ak2485&affiliation=true", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian", "nameType"=>"Personal", "givenName"=>"Kristian", "familyName"=>"Garza", "affiliation"=>[{"name"=>"Freie Universität Berlin", "affiliationIdentifier"=>"https://ror.org/046ak2485", "affiliationIdentifierScheme"=>"ROR"}], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian J.", "nameType"=>"Personal", "givenName"=>"Kristian J.", "familyName"=>"Garza", "affiliation"=>[{"name"=>"Freie Universität Berlin", "affiliationIdentifier"=>"https://ror.org/046ak2485", "affiliationIdentifierScheme"=>"ROR"}], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
end

it 'returns dois with ror id as url', vcr: true do
get "/dois?affiliation-id=https://ror.org/046ak2485&affiliation=true", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian", "nameType"=>"Personal", "givenName"=>"Kristian", "familyName"=>"Garza", "affiliation"=>[{"name"=>"Freie Universität Berlin", "affiliationIdentifier"=>"https://ror.org/046ak2485", "affiliationIdentifierScheme"=>"ROR"}], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian J.", "nameType"=>"Personal", "givenName"=>"Kristian J.", "familyName"=>"Garza", "affiliation"=>[{"name"=>"Freie Universität Berlin", "affiliationIdentifier"=>"https://ror.org/046ak2485", "affiliationIdentifierScheme"=>"ROR"}], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
end

it 'returns dois with re3data id', vcr: true do
Expand All @@ -159,6 +159,14 @@
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('included', 0, 'attributes', "re3data")).to eq("https://doi.org/10.17616/r3xs37")
end

it 'returns dois with full name', vcr: true do
get "/dois?query=Kristian%20Garza&affiliation=true", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig('meta', 'total')).to eq(1)
expect(json.dig('data', 0, 'attributes', 'creators')).to eq([{"name"=>"Garza, Kristian J.", "nameType"=>"Personal", "givenName"=>"Kristian J.", "familyName"=>"Garza", "affiliation"=>[{"name"=>"Freie Universität Berlin", "affiliationIdentifier"=>"https://ror.org/046ak2485", "affiliationIdentifierScheme"=>"ROR"}], "nameIdentifiers"=>[{"schemeUri"=>"https://orcid.org", "nameIdentifier"=>"https://orcid.org/0000-0003-3484-6875", "nameIdentifierScheme"=>"ORCID"}]}])
end
end

describe 'GET /dois/:id', elasticsearch: true do
Expand Down

0 comments on commit 8d58b43

Please sign in to comment.