Skip to content

Commit 8d3fcbf

Browse files
committed
so far making progress
1 parent a62177a commit 8d3fcbf

3 files changed

Lines changed: 100 additions & 15 deletions

File tree

backend/routes/search.py

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,11 @@ def text_search():
309309
q_page = args.get("page", 1, type=int)
310310
q_per_page = args.get("per_page", 20, type=int)
311311
query = args.get("query", None, type=str)
312+
location = args.get("location", None, type=str)
313+
312314
params = {
313315
"query": query,
316+
"location": location,
314317
"page": q_page,
315318
"per_page": q_per_page
316319
}
@@ -341,26 +344,76 @@ def text_search():
341344
if total_results < (q_page - 1) * q_per_page:
342345
return jsonify({"message": "Page number exceeds total results"}), 400
343346

344-
# Query Everything
345-
cypher = """
346-
CALL () {
347+
cypher = """"""
348+
if location is None:
349+
# Query Everything
350+
cypher = """
351+
CALL () {
352+
CALL db.index.fulltext.queryNodes('officerNames', $query)
353+
YIELD node, score
354+
RETURN node, score
355+
UNION ALL
356+
CALL db.index.fulltext.queryNodes('agencyNames', $query)
357+
YIELD node, score
358+
RETURN node, score
359+
UNION ALL
360+
CALL db.index.fulltext.queryNodes('unitNames', $query)
361+
YIELD node, score
362+
RETURN node, score
363+
}
364+
RETURN node, score
365+
ORDER BY score DESC
366+
SKIP $per_page * ($page - 1)
367+
LIMIT $per_page
368+
"""
369+
else:
370+
cypher = """
371+
OPTIONAL MATCH (city:CityNode {name: $location})
372+
-[]-(:CountyNode)-[]-(:StateNode {name: $location})
373+
374+
CALL {
375+
WITH city
376+
377+
// Officers: (Officer)-[]-(Unit)-[]-(Agency)-[]-(CityNode)
347378
CALL db.index.fulltext.queryNodes('officerNames', $query)
348379
YIELD node, score
349-
RETURN node, score
380+
WHERE city IS NOT NULL
381+
AND EXISTS {
382+
(node:Officer)-[]-(:Unit)-[]-(:Agency)-[]-(city)
383+
}
384+
RETURN node, score
385+
350386
UNION ALL
387+
388+
// Agencies: (Agency)-[]-(CityNode)
389+
WITH city
351390
CALL db.index.fulltext.queryNodes('agencyNames', $query)
352391
YIELD node, score
353-
RETURN node, score
392+
WHERE city IS NOT NULL
393+
AND EXISTS {
394+
(node:Agency)-[]-(city)
395+
}
396+
RETURN node, score
397+
354398
UNION ALL
399+
400+
// Units: (Unit)-[]-(Agency)-[]-(CityNode)
401+
WITH city
355402
CALL db.index.fulltext.queryNodes('unitNames', $query)
356403
YIELD node, score
357-
RETURN node, score
358-
}
359-
RETURN node, score
360-
ORDER BY score DESC
361-
SKIP $per_page * ($page - 1)
362-
LIMIT $per_page
363-
"""
404+
WHERE city IS NOT NULL
405+
AND EXISTS {
406+
(node:Unit)-[]-(:Agency)-[]-(city)
407+
}
408+
RETURN node, score
409+
}
410+
411+
RETURN node, score
412+
ORDER BY score DESC
413+
SKIP $per_page * ($page - 1)
414+
LIMIT $per_page
415+
"""
416+
364417

365418
results, meta = db.cypher_query(cypher, params)
366419
if not results:

frontend/app/search/Filter.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
Typography
1010
} from "@mui/material"
1111
import { Search } from "@mui/icons-material"
12+
import { KeyboardEvent, useState, ChangeEvent } from "react"
13+
import { useSearch } from "@/providers/SearchProvider"
14+
import { useSearchParams } from "next/navigation"
1215

1316
const FILTER_GROUP_1 = [
1417
{ id: 1, title: "All the locations", count: 56 },
@@ -22,19 +25,44 @@ const FILTER_GROUP_2 = [
2225
]
2326

2427
const Filter = () => {
28+
const [localLocationInputState, setLocalLocationInputState] = useState('')
29+
const { searchAll } = useSearch()
30+
const searchParams = useSearchParams()
31+
function changeLocalSearchVal(e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
32+
const changeVal = e.target.value
33+
// TODO: this is causing the other search box to refresh as wll
34+
setLocalLocationInputState(changeVal)
35+
}
36+
async function locationFilterSearch(e: KeyboardEvent<HTMLDivElement>) {
37+
if (e.key === 'Enter') {
38+
const currentQuery = searchParams.get('query') ?? ''
39+
await searchAll({ query: currentQuery, location: localLocationInputState })
40+
}
41+
42+
}
2543
return (
2644
<section className={styles.filterWrapper}>
2745
<h3 className={styles.filterTitleText}>Filters</h3>
2846
<div className={styles.filterContentsWrapper}>
29-
<FilterGroup withSearch filters={FILTER_GROUP_1} title="Location" />
47+
<FilterGroup
48+
onChangeHandler={changeLocalSearchVal}
49+
searchHandler={locationFilterSearch}
50+
value={localLocationInputState}
51+
withSearch
52+
filters={FILTER_GROUP_1}
53+
title="Location"
54+
/>
3055
<FilterGroup filters={FILTER_GROUP_2} title="Data Source" />
3156
</div>
3257
</section>
3358
)
3459
}
3560

3661
type FilterGroupProps = {
62+
searchHandler?: (e: KeyboardEvent<HTMLDivElement>) => void
3763
withSearch?: boolean
64+
value?: string
65+
onChangeHandler?: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void
3866
filters: FilterItem[]
3967
title: string
4068
}
@@ -45,17 +73,20 @@ type FilterItem = {
4573
count: number
4674
}
4775

48-
const FilterGroup = ({ withSearch = false, filters = [], title }: FilterGroupProps) => {
76+
const FilterGroup = ({ withSearch = false, filters = [], title, searchHandler, value, onChangeHandler }: FilterGroupProps) => {
4977
return (
5078
<FormGroup sx={{ marginBottom: "1.5rem" }}>
5179
<Typography variant="subtitle1" sx={{ marginBlockEnd: "0.5rem", fontWeight: "600" }}>
5280
{title}
5381
</Typography>
54-
{withSearch && (
82+
{(withSearch && searchHandler !== null) && (
5583
<TextField
5684
id="search"
5785
variant="outlined"
5886
fullWidth
87+
onKeyDown={searchHandler}
88+
onChange={onChangeHandler}
89+
value={value}
5990
sx={{
6091
marginBottom: "1rem",
6192
"& .MuiInputBase-root": {

frontend/components/SearchBar/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const SearchBar = () => {
1515

1616
const handleSearch = async (query: string) => {
1717
try {
18+
console.log("the query", query)
1819
await searchAll({ query })
1920
} catch (error) {
2021
// If it's an authentication error, redirect to login

0 commit comments

Comments
 (0)