Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ locally.
confused.
```

- `OPENSEARCH_SOURCE_EXCLUDES` comma separated list of fields to exclude from the OpenSearch `_source` field. Leave unset to return all fields.
- recommended value: `embedding_full_record,fulltext`
- `PLATFORM_NAME`: The value set is added to the header after the MIT Libraries logo. The logic and CSS for this comes from our theme gem.
- `PREFERRED_DOMAIN` - set this to the domain you would like to to use. Any
other requests that come to the app will redirect to the root of this domain.
Expand Down
10 changes: 10 additions & 0 deletions app/models/opensearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def build_query(from)
sort:
}

# If ENV OPENSEARCH_SOURCE_EXCLUDES is set, use the values in it's comma-separated list;
# otherwise leave out the _source attribute entirely (which will return all fields in _source)
# excludes are used to prevent large fields from being returned in the search results, which can cause performance issues
# these fields are still searchable, just not returned in the search results
if ENV['OPENSEARCH_SOURCE_EXCLUDES'].present?
query_hash[:_source] = {
excludes: ENV['OPENSEARCH_SOURCE_EXCLUDES'].split(',').map(&:strip)
}
end

query_hash[:highlight] = highlight if @highlight
query_hash.to_json
end
Expand Down
18 changes: 18 additions & 0 deletions test/models/opensearch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,22 @@ class OpensearchTest < ActiveSupport::TestCase
json = JSON.parse(os.build_query(0))
assert_equal Opensearch::MAX_SIZE, json['size']
end

test 'can exclude fields from _source' do
ClimateControl.modify(OPENSEARCH_SOURCE_EXCLUDES: 'field1,field2') do
os = Opensearch.new
os.instance_variable_set(:@params, {})
json = JSON.parse(os.build_query(0))
assert_equal %w[field1 field2], json['_source']['excludes']
end
end

test 'does not include _source if OPENSEARCH_SOURCE_EXCLUDES is not set' do
ClimateControl.modify(OPENSEARCH_SOURCE_EXCLUDES: nil) do
os = Opensearch.new
os.instance_variable_set(:@params, {})
json = JSON.parse(os.build_query(0))
refute json.key?('_source')
end
end
end