Skip to content

Commit

Permalink
Query field with dynamic name
Browse files Browse the repository at this point in the history
  • Loading branch information
syabruk committed Nov 13, 2023
1 parent 348eb4b commit b4ba460
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand All @@ -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)
Expand All @@ -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 })]
Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/query/request/edismax_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions spec/query/request/query_field_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/query/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b4ba460

Please sign in to comment.