From b4ba460df0af13dd24df2858f69bda8143101c54 Mon Sep 17 00:00:00 2001 From: Vladislav Syabruk Date: Mon, 13 Nov 2023 16:55:01 +0300 Subject: [PATCH] Query field with dynamic name --- README.md | 33 +++++++++---------- .../{field_with_boost.rb => query_field.rb} | 14 ++++++-- spec/query/request/edismax_adapter_spec.rb | 4 +-- spec/query/request/query_field_spec.rb | 30 +++++++++++++++++ spec/query/request_spec.rb | 2 +- 5 files changed, 61 insertions(+), 22 deletions(-) rename lib/solr/query/request/{field_with_boost.rb => query_field.rb} (51%) create mode 100644 spec/query/request/query_field_spec.rb diff --git a/README.md b/README.md index 2149ac9..ac71369 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ doc = Solr::Update::Commands::Add.new(doc: { id: 5, name: 'John' }) ## Simple Query ```ruby - query_field = Solr::Query::Request::FieldWithBoost.new(field: :name) + query_field = Solr::Query::Request::QueryField.new(field: :name) request = Solr::Query::Request.new(search_term: 'term', query_fields: [query_field]) request.run(page: 1, page_size: 10) @@ -270,8 +270,8 @@ end ```ruby query_fields = [ # Use boost_magnitude argument to apply boost to a specific field that you query - Solr::Query::Request::FieldWithBoost.new(field: :name, boost_magnitude: 16), - Solr::Query::Request::FieldWithBoost.new(field: :title) + Solr::Query::Request::QueryField.new(field: :name, boost_magnitude: 16), + Solr::Query::Request::QueryField.new(field: :title) ] request = Solr::Query::Request.new(search_term: 'term', query_fields: query_fields) request.run(page: 1, page_size: 10) @@ -281,8 +281,8 @@ end ```ruby query_fields = [ - Solr::Query::Request::FieldWithBoost.new(field: :name), - Solr::Query::Request::FieldWithBoost.new(field: :title) + Solr::Query::Request::QueryField.new(field: :name), + Solr::Query::Request::QueryField.new(field: :title) ] filters = [Solr::Query::Request::Filter.new(type: :equal, field: :title, value: 'A title')] request = Solr::Query::Request.new(search_term: 'term', query_fields: query_fields, filters: filters) @@ -312,8 +312,8 @@ end ```ruby query_fields = [ - Solr::Query::Request::FieldWithBoost.new(field: :name), - Solr::Query::Request::FieldWithBoost.new(field: :title) + Solr::Query::Request::QueryField.new(field: :name), + Solr::Query::Request::QueryField.new(field: :title) ] sort_fields = [Solr::Query::Request::Sorting::Field.new(name: :name, direction: :asc)] request = Solr::Query::Request.new(search_term: 'term', query_fields: query_fields) @@ -325,7 +325,7 @@ Default sorting logic is following: nulls last, not-nulls first. ```ruby query_fields = [ - Solr::Query::Request::FieldWithBoost.new(field: :name) + Solr::Query::Request::QueryField.new(field: :name) ] sort_fields = [ Solr::Query::Request::Sorting::Field.new(name: :is_featured, direction: :desc), @@ -340,8 +340,8 @@ Default sorting logic is following: nulls last, not-nulls first. ```ruby query_fields = [ - Solr::Query::Request::FieldWithBoost.new(field: :name), - Solr::Query::Request::FieldWithBoost.new(field: :category) + Solr::Query::Request::QueryField.new(field: :name), + Solr::Query::Request::QueryField.new(field: :category) ] request = Solr::Query::Request.new(search_term: 'term', query_fields: query_fields) request.grouping = Solr::Query::Request::Grouping.new(field: :category, limit: 10) @@ -352,8 +352,8 @@ Default sorting logic is following: nulls last, not-nulls first. ```ruby query_fields = [ - Solr::Query::Request::FieldWithBoost.new(field: :name), - Solr::Query::Request::FieldWithBoost.new(field: :category) + Solr::Query::Request::QueryField.new(field: :name), + Solr::Query::Request::QueryField.new(field: :category) ] request = Solr::Query::Request.new(search_term: 'term', query_fields: query_fields) request.facets = [Solr::Query::Request::Facet.new(type: :terms, field: :category, options: { limit: 10 })] @@ -364,8 +364,8 @@ Default sorting logic is following: nulls last, not-nulls first. ```ruby query_fields = [ - Solr::Query::Request::FieldWithBoost.new(field: :name), - Solr::Query::Request::FieldWithBoost.new(field: :category) + Solr::Query::Request::QueryField.new(field: :name), + Solr::Query::Request::QueryField.new(field: :category) ] request = Solr::Query::Request.new(search_term: 'term', query_fields: query_fields) request.boosting = Solr::Query::Request::Boosting.new( @@ -415,11 +415,10 @@ Example of usage: ## Field list - ```ruby query_fields = [ - Solr::Query::Request::FieldWithBoost.new(field: :name), - Solr::Query::Request::FieldWithBoost.new(field: :category) + Solr::Query::Request::QueryField.new(field: :name), + Solr::Query::Request::QueryField.new(field: :category) ] request = Solr::Query::Request.new(search_term: 'term', query_fields: query_fields) # Solr::Query::Request will return only :id field by default. diff --git a/lib/solr/query/request/field_with_boost.rb b/lib/solr/query/request/query_field.rb similarity index 51% rename from lib/solr/query/request/field_with_boost.rb rename to lib/solr/query/request/query_field.rb index 568c68d..ecb0197 100644 --- a/lib/solr/query/request/field_with_boost.rb +++ b/lib/solr/query/request/query_field.rb @@ -1,7 +1,9 @@ module Solr module Query class Request - class FieldWithBoost + class QueryField + include Solr::Support::SchemaHelper + attr_reader :field, :boost_magnitude def initialize(field:, boost_magnitude: Solr::Query::Request::BoostMagnitude::DEFAULT) @@ -10,7 +12,15 @@ def initialize(field:, boost_magnitude: Solr::Query::Request::BoostMagnitude::DE end def to_solr_s - "#{field}^#{boost_magnitude}" + if boost_magnitude == Solr::Query::Request::BoostMagnitude::DEFAULT + solr_field + else + "#{solr_field}^#{boost_magnitude}" + end + end + + def solr_field + solarize_field(@field) end end end diff --git a/spec/query/request/edismax_adapter_spec.rb b/spec/query/request/edismax_adapter_spec.rb index 0d65183..72c439a 100644 --- a/spec/query/request/edismax_adapter_spec.rb +++ b/spec/query/request/edismax_adapter_spec.rb @@ -31,8 +31,8 @@ context 'complex query' do let(:query_fields) do [ - Solr::Query::Request::FieldWithBoost.new(field: :field_1), - Solr::Query::Request::FieldWithBoost.new(field: :field_2, boost_magnitude: 16) + Solr::Query::Request::QueryField.new(field: :field_1), + Solr::Query::Request::QueryField.new(field: :field_2, boost_magnitude: 16) ] end diff --git a/spec/query/request/query_field_spec.rb b/spec/query/request/query_field_spec.rb new file mode 100644 index 0000000..aaf86aa --- /dev/null +++ b/spec/query/request/query_field_spec.rb @@ -0,0 +1,30 @@ +RSpec.describe Solr::Query::Request::QueryField do + describe '.to_solr_s' do + context 'when boost magnitude is not specified' do + subject { described_class.new(field: :field).to_solr_s } + + it { is_expected.to eq('field^1') } + end + + context 'when boost magnitude is specified' do + subject { described_class.new(field: :field, boost_magnitude: 5).to_solr_s } + + it { is_expected.to eq('field^5') } + end + + context 'with dynamic field' do + before do + Solr.configure do |config| + config.define_core(name: :'test-core') do |f| + f.field :title, dynamic_field: :text + f.dynamic_field :text, solr_name: '*_text' + end + end + end + + subject { described_class.new(field: :title).to_solr_s } + + it { is_expected.to eq('title_text^1') } + end + end +end diff --git a/spec/query/request_spec.rb b/spec/query/request_spec.rb index 714bd9c..0232e24 100644 --- a/spec/query/request_spec.rb +++ b/spec/query/request_spec.rb @@ -12,7 +12,7 @@ let(:search_term) { 'solrb' } let(:query_fields) do - [Solr::Query::Request::FieldWithBoost.new(field: :name_txt_en)] + [Solr::Query::Request::QueryField.new(field: :name_txt_en)] end subject do