Implement efficient article search functionality with AND/OR mode support. Update backend to include searchArticles service and enhance MongoDB indexing. Modify frontend to allow users to select search mode and improve search input layout.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import express from 'express';
|
||||
import { getNotifiedArticles, getArticleFacets } from '../services/mongodb.js';
|
||||
import { getNotifiedArticles, getArticleFacets, searchArticles } from '../services/mongodb.js';
|
||||
import { basicAuthMiddleware } from '../middlewares/auth.js';
|
||||
|
||||
const router = express.Router();
|
||||
@@ -87,50 +87,17 @@ router.get('/search', basicAuthMiddleware, async (req, res) => {
|
||||
if (req.query.worker_name) filter.worker_name = req.query.worker_name;
|
||||
if (req.query.platform) filter.platform = req.query.platform;
|
||||
|
||||
const searchTerm = query.toLowerCase().trim();
|
||||
const allArticles = await getNotifiedArticles(filter);
|
||||
// Obtener modo de búsqueda (AND u OR), por defecto AND
|
||||
const searchMode = (req.query.mode || 'AND').toUpperCase();
|
||||
|
||||
// Filtrar artículos que coincidan con la búsqueda
|
||||
const filtered = allArticles.filter(article => {
|
||||
// Buscar en título
|
||||
const title = (article.title || '').toLowerCase();
|
||||
if (title.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en descripción
|
||||
const description = (article.description || '').toLowerCase();
|
||||
if (description.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en localidad
|
||||
const location = (article.location || '').toLowerCase();
|
||||
if (location.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en precio (como número o texto)
|
||||
const price = String(article.price || '').toLowerCase();
|
||||
if (price.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en plataforma
|
||||
const platform = (article.platform || '').toLowerCase();
|
||||
if (platform.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en ID
|
||||
const id = String(article.id || '').toLowerCase();
|
||||
if (id.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en username
|
||||
const username = (article.username || '').toLowerCase();
|
||||
if (username.includes(searchTerm)) return true;
|
||||
|
||||
// Buscar en worker_name
|
||||
const worker_name = (article.worker_name || '').toLowerCase();
|
||||
if (worker_name.includes(searchTerm)) return true;
|
||||
|
||||
return false;
|
||||
});
|
||||
// Usar la función de búsqueda eficiente por palabras
|
||||
const articles = await searchArticles(query, filter, searchMode);
|
||||
|
||||
res.json({
|
||||
articles: filtered,
|
||||
total: filtered.length,
|
||||
articles: articles,
|
||||
total: articles.length,
|
||||
query: query,
|
||||
mode: searchMode,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
|
||||
Reference in New Issue
Block a user