Skip to content

Commit 7549d7e

Browse files
committed
Requested changes
1 parent 218ad70 commit 7549d7e

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

docs/reference/sciencedirect/ScienceDirectSearch.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pybliometrics.sciencedirect.ScienceDirectSearch
22
===============================================
33

44
`ScienceDirectSearch()` implements the `ScienceDirect Search API <https://nonprod-devportal.elsevier.com/documentation/ScienceDirectSearchAPI.wadl>`_ using the `PUT` method. It executes a query to search for documents and retrieves the resulting records.
5-
The class takes a `query` dictionary as input which has to follow this schema:
5+
The class takes a `query` string that searches through all the article's or chapter's content. You can also pass any of the following parameters as keyword arguments:
66

77
.. code-block:: text
88
@@ -44,16 +44,15 @@ Examples
4444
--------
4545

4646
The class is initialized with a search query.
47-
We can pass the field `qs`` to search for specific keywords.
47+
We can pass `date` as keyword argument to search for documents published in a specific date.
4848
Using `verbose=True` will print the progress of the download.
4949

5050
.. code-block:: python
5151
5252
>>> from pybliometrics.sciencedirect import ScienceDirectSearch, init
5353
>>> init()
54-
>>> # Retrieve documents based on the search query
55-
>>> query = query = {'qs': '"neural radiance fields" AND "3D rendering"', 'date': '2024'}
56-
>>> sds = ScienceDirectSearch(query, verbose=True)
54+
>>> # Retrieve documents based on the search query and date
55+
>>> sds = ScienceDirectSearch('"neural radiance fields" AND "3D rendering"', date='2024', verbose=True)
5756
Downloading results for query "{'qs': '"neural radiance fields" AND "3D rendering"', 'date': '2024', 'display': {'offset': 0, 'show': 100, 'sortBy': 'date'}, 'cursor': '*'}":
5857
100%|██████████| 1/1 [00:00<00:00, 3.23it/s]
5958

pybliometrics/sciencedirect/sciencedirect_search.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ def results(self) -> Optional[list]:
6565
return out or None
6666

6767
def __init__(self,
68-
query: dict,
68+
query: str = '',
6969
refresh: Union[bool, int] = False,
7070
view: Optional[str] = None,
7171
verbose: bool = False,
7272
download: bool = True,
7373
integrity_fields: Optional[Union[list[str], tuple[str, ...]]] = None,
7474
integrity_action: str = "raise",
7575
subscriber: bool = True,
76+
**kwds: str
7677
) -> None:
7778
"""
7879
Parameters
7980
----------
80-
query : dict
81-
The query to be sent to the API, e.g.,
82-
{'qs': '"Neural Networks" AND "Shapley"', 'date': '2019-2020'}
81+
query : str
82+
The query to be sent to the API, e.g. '"Neural Networks" AND "Shapley"'
8383
8484
refresh : bool or int, optional
8585
Whether to refresh the cached file. If an int is passed, the cache
@@ -107,6 +107,12 @@ def __init__(self,
107107
subscriber : bool, optional
108108
If True, cursor navigation is enabled, allowing more than 5,000 results.
109109
110+
**kwds: str
111+
Additional keyword arguments to be passed to the API. These can be any available
112+
search fields, such as `authors`, `pub-date` and `title`. For a full list of
113+
available fields, see the `ScienceDirect Search API Migration Documentation
114+
<https://dev.elsevier.com/tecdoc_sdsearch_migration.html>`__.
115+
110116
Raises
111117
------
112118
ScopusQueryError
@@ -117,8 +123,13 @@ def __init__(self,
117123
is not one of the allowed values.
118124
119125
"""
126+
# Check if the query and keyword arguments are empty
127+
if not (query or kwds):
128+
msg = "The query is empty. Please provide either a query string or keyword arguments."
129+
raise ValueError(msg)
130+
120131
if view:
121-
check_parameter_value(view, VIEWS['ScienceDirectSearch'], "view")
132+
check_parameter_value(view, VIEWS["ScienceDirectSearch"], "view")
122133
else:
123134
view = "STANDARD"
124135

@@ -133,7 +144,7 @@ def __init__(self,
133144

134145
Search.__init__(self, query=query,
135146
cursor=subscriber, download=download,
136-
verbose=verbose)
147+
verbose=verbose, **kwds)
137148

138149
def __str__(self):
139150
"""Print a summary string."""

pybliometrics/sciencedirect/tests/test_ScienceDirectSearch.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
init()
1010

11-
one_article_query = {'title': 'Assessing LLMs in malicious code deobfuscation of real-world malware campaigns',
12-
'date': '2024'}
13-
sds_standard = ScienceDirectSearch(one_article_query, refresh=30)
11+
sds_standard = ScienceDirectSearch(title='Assessing LLMs in malicious code deobfuscation of real-world malware campaigns',
12+
date='2024',
13+
refresh=30)
1414

15-
empty_query = {'title': 'Not a realistic title', 'date': '2012'}
16-
sds_empty = ScienceDirectSearch(empty_query, view="STANDARD", refresh=30)
15+
sds_empty = ScienceDirectSearch(title='Not a realistic title',
16+
date='2012',
17+
view="STANDARD", refresh=30)
1718

18-
huge_query = {'qs': 'Neural Networks', 'date': '2015-2020'}
19-
sds_huge = ScienceDirectSearch(huge_query, view="STANDARD", download=False, refresh=30)
19+
sds_huge = ScienceDirectSearch('Neural Networks',
20+
date='2015-2020',
21+
view="STANDARD", download=False, refresh=30)
2022

21-
pagination_query = {'qs': '"Neural Networks" AND "Shapley"', 'date': '2020'}
22-
sds_pagination = ScienceDirectSearch(pagination_query, view="STANDARD", refresh=30)
23+
sds_pagination = ScienceDirectSearch('"Neural Networks" AND "Shapley"',
24+
date='2020',
25+
view="STANDARD", refresh=30)
2326

2427
def test_all_fields():
2528
fields = 'authors doi loadDate openAccess first_page last_page pii publicationDate ' \
@@ -66,18 +69,19 @@ def test_empty_results():
6669

6770

6871
def test_field_consistency():
69-
am_wrong_field = ScienceDirectSearch(one_article_query,
70-
integrity_fields=["notExistingField"],
71-
integrity_action="warn",
72-
view="STANDARD",
73-
refresh=30)
72+
am_wrong_field = ScienceDirectSearch(query='',
73+
title='Assessing LLMs in malicious code deobfuscation of real-world malware campaigns',
74+
date='2024',
75+
integrity_fields=["notExistingField"],
76+
integrity_action="warn",
77+
view="STANDARD", refresh=30)
7478
with pytest.raises(ValueError):
7579
_ = am_wrong_field.results
7680

7781

7882
def test_large_results():
7983
with pytest.raises(ScopusQueryError):
80-
_ = ScienceDirectSearch(huge_query, view="STANDARD", download=True, refresh=30)
84+
_ = ScienceDirectSearch('Neural Networks', view="STANDARD", download=True, refresh=30)
8185

8286

8387
def test_length():
@@ -88,5 +92,5 @@ def test_length():
8892

8993

9094
def test_string():
91-
expected_str = "Search '{'title': 'Assessing LLMs in malicious code deobfuscation of real-world malware campaigns', 'date': '2024'}' yielded 1 document as of 2025-05-07:\n 10.1016/j.eswa.2024.124912"
92-
assert str(sds_standard) == expected_str
95+
expected_str = "Search '' yielded 1 document as of"
96+
assert str(sds_standard).startswith(expected_str)

pybliometrics/superclasses/search.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ def __init__(self,
3939
count = COUNTS[api][self._view]
4040

4141
if api == 'ScienceDirectSearch':
42-
# Add default parameters
43-
params = {**query}
42+
# Set qs, keyword arguments and add default parameters
43+
params = {'qs': query, **kwds}
44+
# Flatten query and create name
45+
flat_query = flatten_dict(params)
46+
name = "&".join(["=".join(map(str, t)) for t in zip(flat_query.keys(), flat_query.values())])
47+
# Add default parameters for pagination
4448
params.setdefault('display', {})
4549
defaults = {'offset': 0, 'show': count, 'sortBy': 'date'}
4650
for key, default in defaults.items():
4751
params['display'].setdefault(key, default)
48-
# Flatten query and create name
49-
flat_query = flatten_dict(query)
50-
name = "&".join(["=".join(map(str, t)) for t in zip(flat_query.keys(), flat_query.values())])
5152
else:
5253
params = {'count': count, 'view': self._view, **kwds}
5354

0 commit comments

Comments
 (0)