Skip to content

Commit

Permalink
use filter instead of must
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 8, 2020
1 parent 7339e91 commit 4ea6d22
Showing 1 changed file with 94 additions and 62 deletions.
156 changes: 94 additions & 62 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,26 @@ def query(query, options={})
query = query.gsub("/", '\/')
end

must = []
must_not = []
filter = []

# filters for some classes
if self.name == "Provider"
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?
must << { term: { region: options[:region].upcase }} if options[:region].present?
must << { term: { "consortium_id.raw" => options[:consortium_id] }} if options[:consortium_id].present?
must << { term: { member_type: options[:member_type] }} if options[:member_type].present?
must << { term: { organization_type: options[:organization_type] }} if options[:organization_type].present?
must << { term: { non_profit_status: options[:non_profit_status] }} if options[:non_profit_status].present?
must << { term: { focus_area: options[:focus_area] }} if options[:focus_area].present?
if query.present?
must = [{ query_string: { query: query, fields: query_fields, default_operator: "AND", phrase_slop: 1 } }]
else
must = [{ match_all: {} }]
end

filter << { range: { created: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
filter << { range: { updated: { gte: "#{options[:from_date]}||/d" }}} if options[:from_date].present?
filter << { range: { updated: { lte: "#{options[:until_date]}||/d" }}} if options[:until_date].present?
filter << { term: { region: options[:region].upcase }} if options[:region].present?
filter << { term: { "consortium_id.raw" => options[:consortium_id] }} if options[:consortium_id].present?
filter << { term: { member_type: options[:member_type] }} if options[:member_type].present?
filter << { term: { organization_type: options[:organization_type] }} if options[:organization_type].present?
filter << { term: { non_profit_status: options[:non_profit_status] }} if options[:non_profit_status].present?
filter << { term: { focus_area: options[:focus_area] }} if options[:focus_area].present?

must_not << { exists: { field: "deleted_at" }} unless options[:include_deleted]
if options[:exclude_registration_agencies]
Expand All @@ -228,62 +233,88 @@ def query(query, options={})
must_not << { term: { role_name: "ROLE_ADMIN" }}
end
elsif self.name == "Client"
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?
must << { terms: { provider_id: options[:provider_id].split(",") }} if options[:provider_id].present?
must << { terms: { "software.raw" => options[:software].split(",") }} if options[:software].present?
must << { terms: { certificate: options[:certificate].split(",") }} if options[:certificate].present?
must << { terms: { repository_type: options[:repository_type].split(",") }} if options[:repository_type].present?
must << { term: { consortium_id: options[:consortium_id] }} if options[:consortium_id].present?
must << { term: { re3data_id: options[:re3data_id].gsub("/", '\/').upcase }} if options[:re3data_id].present?
must << { term: { opendoar_id: options[:opendoar_id] }} if options[:opendoar_id].present?
must << { term: { client_type: options[:client_type] }} if options[:client_type].present?
if query.present?
must = [{ query_string: { query: query, fields: query_fields, default_operator: "AND", phrase_slop: 1 } }]
else
must = [{ match_all: {} }]
end

filter << { range: { created: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
filter << { range: { updated: { gte: "#{options[:from_date]}||/d" }}} if options[:from_date].present?
filter << { range: { updated: { lte: "#{options[:until_date]}||/d" }}} if options[:until_date].present?
filter << { terms: { provider_id: options[:provider_id].split(",") }} if options[:provider_id].present?
filter << { terms: { "software.raw" => options[:software].split(",") }} if options[:software].present?
filter << { terms: { certificate: options[:certificate].split(",") }} if options[:certificate].present?
filter << { terms: { repository_type: options[:repository_type].split(",") }} if options[:repository_type].present?
filter << { term: { consortium_id: options[:consortium_id] }} if options[:consortium_id].present?
filter << { term: { re3data_id: options[:re3data_id].gsub("/", '\/').upcase }} if options[:re3data_id].present?
filter << { term: { opendoar_id: options[:opendoar_id] }} if options[:opendoar_id].present?
filter << { 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 == "Event"
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?
must << { term: { year_month: options[:year_month] }} if options[:year_month].present?
must << { range: { "subj.datePublished" => { gte: "#{options[:publication_year].split("-").min}||/y", lte: "#{options[:publication_year].split("-").max}||/y", format: "yyyy" }}} if options[:publication_year].present?
must << { range: { occurred_at: { gte: "#{options[:occurred_at].split("-").min}||/y", lte: "#{options[:occurred_at].split("-").max}||/y", format: "yyyy" }}} if options[:occurred_at].present?
must << { terms: { prefix: options[:prefix].split(",") }} if options[:prefix].present?
must << { terms: { doi: options[:doi].downcase.split(",") }} if options[:doi].present?
must << { terms: { source_doi: options[:source_doi].downcase.split(",") }} if options[:source_doi].present?
must << { terms: { target_doi: options[:target_doi].downcase.split(",") }} if options[:target_doi].present?
must << { terms: { orcid: options[:orcid].split(",") }} if options[:orcid].present?
must << { terms: { isni: options[:isni].split(",") }} if options[:isni].present?
must << { terms: { subtype: options[:subtype].split(",") }} if options[:subtype].present?
must << { terms: { source_id: options[:source_id].split(",") }} if options[:source_id].present?
must << { terms: { relation_type_id: options[:relation_type_id].split(",") }} if options[:relation_type_id].present?
must << { terms: { source_relation_type_id: options[:source_relation_type_id].split(",") }} if options[:source_relation_type_id].present?
must << { terms: { target_relation_type_id: options[:target_relation_type_id].split(",") }} if options[:target_relation_type_id].present?
must << { terms: { registrant_id: options[:registrant_id].split(",") }} if options[:registrant_id].present?
must << { terms: { registrant_id: options[:provider_id].split(",") }} if options[:provider_id].present?
must << { terms: { issn: options[:issn].split(",") }} if options[:issn].present?
if query.present?
must = [{ query_string: { query: query, fields: query_fields, default_operator: "AND", phrase_slop: 1 } }]
else
must = [{ match_all: {} }]
end

filter << { term: { subj_id: URI.decode(options[:subj_id]) }} if options[:subj_id].present?
filter << { term: { obj_id: URI.decode(options[:obj_id]) }} if options[:obj_id].present?
filter << { term: { citation_type: options[:citation_type] }} if options[:citation_type].present?
filter << { term: { year_month: options[:year_month] }} if options[:year_month].present?
filter << { range: { "subj.datePublished" => { gte: "#{options[:publication_year].split("-").min}||/y", lte: "#{options[:publication_year].split("-").max}||/y", format: "yyyy" }}} if options[:publication_year].present?
filter << { range: { occurred_at: { gte: "#{options[:occurred_at].split("-").min}||/y", lte: "#{options[:occurred_at].split("-").max}||/y", format: "yyyy" }}} if options[:occurred_at].present?
filter << { terms: { prefix: options[:prefix].split(",") }} if options[:prefix].present?
filter << { terms: { doi: options[:doi].downcase.split(",") }} if options[:doi].present?
filter << { terms: { source_doi: options[:source_doi].downcase.split(",") }} if options[:source_doi].present?
filter << { terms: { target_doi: options[:target_doi].downcase.split(",") }} if options[:target_doi].present?
filter << { terms: { orcid: options[:orcid].split(",") }} if options[:orcid].present?
filter << { terms: { isni: options[:isni].split(",") }} if options[:isni].present?
filter << { terms: { subtype: options[:subtype].split(",") }} if options[:subtype].present?
filter << { terms: { source_id: options[:source_id].split(",") }} if options[:source_id].present?
filter << { terms: { relation_type_id: options[:relation_type_id].split(",") }} if options[:relation_type_id].present?
filter << { terms: { source_relation_type_id: options[:source_relation_type_id].split(",") }} if options[:source_relation_type_id].present?
filter << { terms: { target_relation_type_id: options[:target_relation_type_id].split(",") }} if options[:target_relation_type_id].present?
filter << { terms: { registrant_id: options[:registrant_id].split(",") }} if options[:registrant_id].present?
filter << { terms: { registrant_id: options[:provider_id].split(",") }} if options[:provider_id].present?
filter << { terms: { issn: options[:issn].split(",") }} if options[:issn].present?
elsif self.name == "Prefix"
must << { prefix: { prefix: query }} if query.present?
must << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { terms: { provider_ids: options[:provider_id].split(",") }} if options[:provider_id].present?
must << { terms: { client_ids: options[:client_id].to_s.split(",") }} if options[:client_id].present?
must << { terms: { state: options[:state].to_s.split(",") }} if options[:state].present?
if query.present?
must = [{ prefix: { prefix: query }}]
else
must = [{ match_all: {} }]
end

filter << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
filter << { terms: { provider_ids: options[:provider_id].split(",") }} if options[:provider_id].present?
filter << { terms: { client_ids: options[:client_id].to_s.split(",") }} if options[:client_id].present?
filter << { terms: { state: options[:state].to_s.split(",") }} if options[:state].present?
elsif self.name == "ProviderPrefix"
must << { prefix: { "prefix.prefix" => query }} if query.present?
must << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { terms: { provider_id: options[:provider_id].split(",") }} if options[:provider_id].present?
must << { terms: { provider_id: options[:consortium_organization_id].split(",") }} if options[:consortium_organization_id].present?
must << { term: { consortium_id: options[:consortium_id] }} if options[:consortium_id].present?
must << { term: { prefix_id: options[:prefix_id] }} if options[:prefix_id].present?
must << { terms: { uid: options[:uid].to_s.split(",") }} if options[:uid].present?
must << { terms: { state: options[:state].to_s.split(",") }} if options[:state].present?
Rails.logger.warn query.inspect
if query.present?
must = [{ prefix: { prefix_id: query }}]
else
must = [{ match_all: {} }]
end

filter << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
filter << { terms: { provider_id: options[:provider_id].split(",") }} if options[:provider_id].present?
filter << { terms: { provider_id: options[:consortium_organization_id].split(",") }} if options[:consortium_organization_id].present?
filter << { term: { consortium_id: options[:consortium_id] }} if options[:consortium_id].present?
filter << { term: { prefix_id: options[:prefix_id] }} if options[:prefix_id].present?
filter << { terms: { uid: options[:uid].to_s.split(",") }} if options[:uid].present?
filter << { terms: { state: options[:state].to_s.split(",") }} if options[:state].present?
elsif self.name == "ClientPrefix"
must << { prefix: { "prefix.prefix" => query }} if query.present?
must << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { terms: { client_id: options[:client_id].split(",") }} if options[:client_id].present?
must << { term: { prefix_id: options[:prefix_id] }} if options[:prefix_id].present?
if query.present?
must = [{ prefix: { prefix_id: query }}]
else
must = [{ match_all: {} }]
end

filter << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
filter << { terms: { client_id: options[:client_id].split(",") }} if options[:client_id].present?
filter << { term: { prefix_id: options[:prefix_id] }} if options[:prefix_id].present?
end

# ES query can be optionally defined in different ways
Expand All @@ -294,7 +325,8 @@ def query(query, options={})
# The main bool query with filters
bool_query = {
must: must,
must_not: must_not
must_not: must_not,
filter: filter
}

# Function score is used to provide varying score to return different values
Expand Down

0 comments on commit 4ea6d22

Please sign in to comment.