nikeshn commited on
Commit
95dac73
Β·
verified Β·
1 Parent(s): 33c5890

Update src/api/search.py

Browse files
Files changed (1) hide show
  1. src/api/search.py +19 -66
src/api/search.py CHANGED
@@ -1,15 +1,13 @@
1
  """
2
- src/api/search.py β€” PRIMO + PubMed search endpoint for LibBee v3.3.
3
 
4
- Key fix: _boolean_to_primo_params() converts an LLM-generated Boolean query like
5
- ("smart city" OR "urban mobility") AND ("technocentricity" OR "citizen-centric")
6
- into PRIMO's multi-q-param format:
7
- q=any,contains,smart city OR urban mobility,AND&q=any,contains,technocentricity OR citizen-centric
8
- Sending the raw boolean string as a single q= causes PRIMO to match random docs
9
- because it treats parentheses, AND, and OR as literal search text.
10
 
11
- Fix v3.3: quotes preserved inside q= params so PRIMO performs exact phrase
12
- matching β€” "machine learning" stays as a phrase, not two separate words.
 
13
  """
14
  import re
15
  import logging
@@ -38,75 +36,30 @@ class SearchRequest(BaseModel):
38
  resource_type: str = "articles" # "articles" | "books" | "both"
39
 
40
 
41
- # ── PRIMO Boolean β†’ multi-param converter ──────────────────────────────────────
42
 
43
  def _boolean_to_primo_params(boolean_query: str) -> str:
44
  """
45
- Convert a Boolean query with AND groups + OR alternatives into PRIMO's
46
- multi-q-parameter format.
47
 
48
- PRIMO accepts:
49
- q=any,contains,smart city OR urban mobility,AND
50
- &q=any,contains,technocentricity OR citizen-centric
51
 
52
  Examples:
53
- '("smart city" OR "urban mobility") AND ("technocentricity")'
54
- β†’ 'q=any,contains,smart city OR urban mobility,AND&q=any,contains,technocentricity'
55
 
56
  '"climate change"'
57
- β†’ 'q=any,contains,climate change'
 
 
 
58
  """
59
  if not boolean_query or not boolean_query.strip():
60
  return "q=any,contains,library"
61
 
62
- has_and = bool(re.search(r'\bAND\b', boolean_query, re.IGNORECASE))
63
- has_parens = '(' in boolean_query
64
-
65
- if not (has_and and has_parens):
66
- clean = re.sub(r'["\']', '', boolean_query).strip()
67
- clean = re.sub(r'\b(AND|OR|NOT)\b', ' ', clean, flags=re.IGNORECASE)
68
- clean = re.sub(r'\s+', ' ', clean).strip()
69
- return f"q=any,contains,{quote(clean)}"
70
-
71
- groups = []
72
- depth = 0
73
- chunk = ""
74
- for char in boolean_query:
75
- if char == '(':
76
- depth += 1
77
- chunk += char
78
- elif char == ')':
79
- depth -= 1
80
- chunk += char
81
- else:
82
- chunk += char
83
- if depth == 0 and chunk.upper().endswith(' AND '):
84
- groups.append(chunk[:-5].strip())
85
- chunk = ""
86
- if chunk.strip():
87
- groups.append(chunk.strip())
88
-
89
- if not groups:
90
- clean = re.sub(r'["\']', '', boolean_query).strip()
91
- return f"q=any,contains,{quote(clean)}"
92
-
93
- primo_params = []
94
- for i, group in enumerate(groups):
95
- g = group.strip()
96
- if g.startswith('(') and g.endswith(')'):
97
- g = g[1:-1].strip()
98
- # Quotes preserved β€” PRIMO REST API supports phrase matching inside q= params
99
- g = re.sub(r'\s+OR\s+', ' OR ', g, flags=re.IGNORECASE).strip()
100
- if not g:
101
- continue
102
- suffix = ',AND' if i < len(groups) - 1 else ''
103
- primo_params.append(f"q=any,contains,{quote(g)}{suffix}")
104
-
105
- if not primo_params:
106
- clean = re.sub(r'["\']', '', boolean_query).strip()
107
- return f"q=any,contains,{quote(clean)}"
108
-
109
- return '&'.join(primo_params)
110
 
111
 
112
  # ── PRIMO helpers ──────────────────────────────────────────────────────────────
 
1
  """
2
+ src/api/search.py β€” PRIMO + PubMed search endpoint for LibBee v3.4.
3
 
4
+ Search approach: send the full boolean query as a single q=any,contains,<boolean>
5
+ parameter β€” exactly how the PRIMO web UI works. This preserves phrase quotes,
6
+ parentheses, AND/OR operators, and gives results identical to the web UI.
 
 
 
7
 
8
+ Earlier versions split the boolean into multiple q= params (one per AND group),
9
+ but PRIMO does not reliably handle OR within a single q= value in multi-param mode.
10
+ The correct approach is a single q= with the full boolean, URL-encoded.
11
  """
12
  import re
13
  import logging
 
36
  resource_type: str = "articles" # "articles" | "books" | "both"
37
 
38
 
39
+ # ── PRIMO query builder ────────────────────────────────────────────────────────
40
 
41
  def _boolean_to_primo_params(boolean_query: str) -> str:
42
  """
43
+ Convert a boolean query into a single PRIMO q= parameter.
 
44
 
45
+ Sends the full boolean as: q=any,contains,<url-encoded boolean>
46
+ This matches exactly how the PRIMO web UI constructs advanced search URLs.
 
47
 
48
  Examples:
49
+ '("machine learning" OR "deep learning") AND ("healthcare")'
50
+ β†’ 'q=any,contains,%28%22machine+learning%22+OR+%22deep+learning%22%29...'
51
 
52
  '"climate change"'
53
+ β†’ 'q=any,contains,%22climate+change%22'
54
+
55
+ 'petroleum engineering'
56
+ β†’ 'q=any,contains,petroleum+engineering'
57
  """
58
  if not boolean_query or not boolean_query.strip():
59
  return "q=any,contains,library"
60
 
61
+ # Send the full boolean as a single q= parameter β€” PRIMO parses it correctly
62
+ return f"q=any,contains,{quote(boolean_query.strip())}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
 
65
  # ── PRIMO helpers ──────────────────────────────────────────────────────────────