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
40 changes: 40 additions & 0 deletions backend/app/api/search/nearby/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import esClient from "lib/elasticsearch";
import { NextResponse } from "next/server";

export async function POST(request: Request) {
const { lat, lon, distance = '20km' } = await request.json();

if (!lat || !lon) {
return NextResponse.json(
{ error: 'Latitude and longitude are required' },
{ status: 400 }
);
}

const result = await esClient.search({
index: 'pets',
query: {
bool: {
filter: [
{
geo_distance: {
distance,
location: { lat, lon }
}
}
]
}
},
sort: [
{
_geo_distance: {
location: { lat, lon },
order: 'asc'
}
}
]
});

return NextResponse.json(result.hits.hits);
}

36 changes: 36 additions & 0 deletions backend/app/api/search/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import esClient from 'lib/elasticsearch';
import { NextResponse } from 'next/server';


export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const query = searchParams.get('q');
const breed = searchParams.get('breed');

const result = await esClient.search({
index: 'pets',
query: {
bool: {
must: [
{
multi_match: {
query: query ?? '',
fields: ['name^3', 'description', 'breed^2'],
fuzziness: 'AUTO'
}
}
],
filter: breed
? [{ term: { 'breed.keyword': breed } }]
: []
}
},
aggs: {
breed_facets: {
terms: { field: 'breed.keyword' }
}
}
});

return NextResponse.json(result.hits.hits);
}
8 changes: 8 additions & 0 deletions backend/lib/elasticsearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Client } from '@elastic/elasticsearch';

const esClient = new Client({
node: process.env.ELASTICSEARCH_URL,
auth: { apiKey: process.env.ELASTICSEARCH_API_KEY! }
});

export default esClient;
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}