From 8d58b4391a796ce936688d27910a9e8b16a98dc5 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 12 Apr 2020 16:38:12 +0200 Subject: [PATCH] Change default operator in query string queries to AND #464 --- app/models/concerns/indexable.rb | 34 +----------- app/models/doi.rb | 2 +- .../returns_dois_with_full_name.yml | 55 +++++++++++++++++++ spec/requests/dois_spec.rb | 24 +++++--- 4 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/dois/GET_/dois_with_query/returns_dois_with_full_name.yml diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 07f2d8c09..552271d8c 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -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? @@ -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? @@ -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? diff --git a/app/models/doi.rb b/app/models/doi.rb index b06dc734c..152776fc5 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -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? diff --git a/spec/fixtures/vcr_cassettes/dois/GET_/dois_with_query/returns_dois_with_full_name.yml b/spec/fixtures/vcr_cassettes/dois/GET_/dois_with_query/returns_dois_with_full_name.yml new file mode 100644 index 000000000..73673d148 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/dois/GET_/dois_with_query/returns_dois_with_full_name.yml @@ -0,0 +1,55 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.ror.org/organizations?query=%22https://ror.org/046ak2485%22 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.6.1; mailto:info@datacite.org) + Accept: + - text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5 + Accept-Encoding: + - gzip,deflate + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json + Content-Length: + - '868' + Connection: + - keep-alive + Date: + - Sun, 12 Apr 2020 14:21:15 GMT + Status: + - 200 OK + X-Frame-Options: + - SAMEORIGIN + Allow: + - GET, HEAD, OPTIONS + X-Powered-By: + - Phusion Passenger 6.0.4 + Server: + - nginx/1.17.3 + Phusion Passenger 6.0.4 + Vary: + - Cookie,Origin + X-Cache: + - Miss from cloudfront + Via: + - 1.1 8033f9c6b87a03b2eca7c2db5157e10e.cloudfront.net (CloudFront) + X-Amz-Cf-Pop: + - DUS51-C1 + X-Amz-Cf-Id: + - sHgEk88Ic9Qt5aw9O0Ol7u8WPxkafGW1t3yfdMa8GH-qrUKDy32Ggg== + body: + encoding: ASCII-8BIT + string: !binary |- + eyJudW1iZXJfb2ZfcmVzdWx0cyI6MSwidGltZV90YWtlbiI6MzgsIml0ZW1zIjpbeyJpZCI6Imh0dHBzOi8vcm9yLm9yZy8wNDZhazI0ODUiLCJuYW1lIjoiRnJlaWUgVW5pdmVyc2l0w6R0IEJlcmxpbiIsInR5cGVzIjpbIkVkdWNhdGlvbiJdLCJsaW5rcyI6WyJodHRwOi8vd3d3LmZ1LWJlcmxpbi5kZS9lbi8iXSwiYWxpYXNlcyI6WyJGVSBCZXJsaW4iXSwiYWNyb255bXMiOlsiRlUiXSwic3RhdHVzIjoiYWN0aXZlIiwid2lraXBlZGlhX3VybCI6Imh0dHA6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvRnJlZV9Vbml2ZXJzaXR5X29mX0JlcmxpbiIsImxhYmVscyI6W3sibGFiZWwiOiJGcmVlIFVuaXZlcnNpdHkgb2YgQmVybGluIiwiaXNvNjM5IjoiZW4ifV0sImNvdW50cnkiOnsiY291bnRyeV9uYW1lIjoiR2VybWFueSIsImNvdW50cnlfY29kZSI6IkRFIn0sImV4dGVybmFsX2lkcyI6eyJJU05JIjp7InByZWZlcnJlZCI6bnVsbCwiYWxsIjpbIjAwMDAgMDAwMCA5MTE2IDQ4MzYiXX0sIkZ1bmRSZWYiOnsicHJlZmVycmVkIjpudWxsLCJhbGwiOlsiNTAxMTAwMDAxNjQ0IiwiNTAxMTAwMDAyOTcxIl19LCJPcmdSZWYiOnsicHJlZmVycmVkIjpudWxsLCJhbGwiOlsiMzE1Mjc1Il19LCJXaWtpZGF0YSI6eyJwcmVmZXJyZWQiOm51bGwsImFsbCI6WyJRMTUzMDA2Il19LCJHUklEIjp7InByZWZlcnJlZCI6ImdyaWQuMTQwOTUuMzkiLCJhbGwiOiJncmlkLjE0MDk1LjM5In19fV0sIm1ldGEiOnsidHlwZXMiOlt7ImlkIjoiZWR1Y2F0aW9uIiwidGl0bGUiOiJFZHVjYXRpb24iLCJjb3VudCI6MX1dLCJjb3VudHJpZXMiOlt7ImlkIjoiZGUiLCJ0aXRsZSI6IkZlZGVyYWwgUmVwdWJsaWMgb2YgR2VybWFueSIsImNvdW50IjoxfV19fQ== + http_version: null + recorded_at: Sun, 12 Apr 2020 14:21:15 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index ce855e441..b3d4131a6 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -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": [ @@ -85,7 +85,7 @@ 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 @@ -93,7 +93,7 @@ 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 @@ -101,7 +101,7 @@ 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 @@ -125,7 +125,7 @@ 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 @@ -133,7 +133,7 @@ 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 @@ -141,7 +141,7 @@ 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 @@ -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