Skip to content

Commit 862f51b

Browse files
k-schnickmannGerit Wagner
authored andcommitted
fix search fields on compound queries
1 parent ff18da1 commit 862f51b

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

docs/source/api_search/api_search.rst

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ Using the Crossref API as an example, the following illustrates how to construct
1313
from pathlib import Path
1414
from colrev.packages.crossref.src import crossref_api
1515
from colrev.writer.write_utils import write_file
16-
16+
1717
from search_query.constants import Fields, Operators
1818
from search_query.or_query import OrQuery
1919
from search_query.search_file import SearchFile
20-
20+
2121
def to_crossref_url(query):
2222
"""Translate query into a Crossref-compatible URL."""
2323
if query.value != Operators.OR:
2424
raise ValueError("Crossref serializer only supports OR queries.")
25-
25+
2626
query_parts = []
2727
for child in query.children:
2828
if child.operator:
2929
raise ValueError("Nested operators are not supported in Crossref serializer.")
3030
if child.search_field.value != Fields.TITLE:
3131
raise ValueError(f"Only title field is supported in Crossref serializer ({child.search_field})")
3232
query_parts.append(child.value.strip())
33-
33+
3434
# Crossref uses '+' for spaces in query values
3535
query_string = "+".join(query_parts)
3636
return f"https://api.crossref.org/works?query.title={query_string}"
37-
37+
3838
if __name__ == "__main__":
39-
39+
4040
query = OrQuery(["microsourcing", "lululemon"], search_field="ti")
41-
41+
4242
url = to_crossref_url(query)
4343
api_crossref = crossref_api.CrossrefAPI(params={"url": url})
4444
records = api_crossref.get_records()
45-
45+
4646
sf = SearchFile(
4747
search_string=query.to_string(),
4848
platform="crossref",
@@ -53,9 +53,8 @@ Using the Crossref API as an example, the following illustrates how to construct
5353
description="Search for work authored by Tom Brady",
5454
tags=["microsourcing", "lululemon", "research"]
5555
)
56-
56+
5757
sf.save("test/tom_brady_search.json")
58-
58+
5959
records_dict = {record.get_value("doi"): record.get_data() for record in records}
6060
write_file(records_dict=records_dict, filename=Path("test/crossref_records.bib"))
61-

search_query/linter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import search_query.parser
99
from search_query.constants import Colors
1010
from search_query.constants import ExitCodes
11-
from search_query.search_file import load_search_file
11+
from search_query.search_file import SearchFile
1212
from search_query.utils import format_query_string_pos
1313

1414

search_query/parser_pubmed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def _parse_compound_query(self, tokens: list) -> Query:
241241
return Query(
242242
value=operator_type,
243243
operator=True,
244-
search_field=SearchField(value=Fields.ALL),
244+
search_field=None,
245245
children=list(children),
246246
position=(query_start_pos, query_end_pos),
247247
)
@@ -340,7 +340,7 @@ def _expand_combined_fields(self, query: Query, search_fields: list) -> None:
340340

341341
query.value = Operators.OR
342342
query.operator = True
343-
query.search_field = SearchField(value=Fields.ALL)
343+
query.search_field = None
344344
query.children = query_children
345345

346346
def get_query_leaves(self, query: Query) -> list:

test/test_parser_pubmed.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def test_tokenization_pubmed(query_str: str, expected_tokens: list) -> None:
6161
[
6262
(
6363
'(eHealth[Title/Abstract] OR "eHealth"[MeSH Terms]) AND Review[Publication Type]',
64-
# TODO : should the operators have search_field?
65-
'AND[all][OR[all][OR[all][eHealth[ti], eHealth[ab]], "eHealth"[mh]], Review[pt]]',
64+
'AND[OR[OR[eHealth[ti], eHealth[ab]], "eHealth"[mh]], Review[pt]]',
6665
)
6766
],
6867
)

0 commit comments

Comments
 (0)