mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 06:01:26 +00:00
Add autosearch for elastic too
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Blacklight\ElasticSearchSiteSearch;
|
||||
use Blacklight\ManticoreSearch;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -9,11 +10,16 @@ use Illuminate\Support\Facades\RateLimiter;
|
||||
|
||||
class SearchSuggestController extends Controller
|
||||
{
|
||||
private ManticoreSearch $manticore;
|
||||
private ManticoreSearch|ElasticSearchSiteSearch $searchEngine;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->manticore = new ManticoreSearch;
|
||||
// Use Elasticsearch if enabled, otherwise fall back to ManticoreSearch
|
||||
if (config('nntmux.elasticsearch_enabled', false)) {
|
||||
$this->searchEngine = new ElasticSearchSiteSearch;
|
||||
} else {
|
||||
$this->searchEngine = new ManticoreSearch;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,8 +28,8 @@ class SearchSuggestController extends Controller
|
||||
public function autocomplete(Request $request): JsonResponse
|
||||
{
|
||||
$query = $request->input('q', '');
|
||||
// Use the index from ManticoreSearch config, or allow override via request
|
||||
$index = $request->input('index') ?? $this->manticore->getReleasesIndex();
|
||||
// Use the index from search engine config, or allow override via request
|
||||
$index = $request->input('index') ?? $this->searchEngine->getReleasesIndex();
|
||||
|
||||
// Rate limiting: 60 requests per minute per IP
|
||||
$key = 'autocomplete:'.$request->ip();
|
||||
@@ -35,7 +41,7 @@ class SearchSuggestController extends Controller
|
||||
}
|
||||
RateLimiter::hit($key, 60);
|
||||
|
||||
if (! $this->manticore->isAutocompleteEnabled()) {
|
||||
if (! $this->searchEngine->isAutocompleteEnabled()) {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'suggestions' => [],
|
||||
@@ -50,7 +56,7 @@ class SearchSuggestController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
$suggestions = $this->manticore->autocomplete($query, $index);
|
||||
$suggestions = $this->searchEngine->autocomplete($query, $index);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
@@ -72,7 +78,7 @@ class SearchSuggestController extends Controller
|
||||
public function suggest(Request $request): JsonResponse
|
||||
{
|
||||
$query = $request->input('q', '');
|
||||
$index = $request->input('index', 'releases_rt');
|
||||
$index = $request->input('index') ?? $this->searchEngine->getReleasesIndex();
|
||||
|
||||
// Rate limiting: 30 requests per minute per IP
|
||||
$key = 'suggest:'.$request->ip();
|
||||
@@ -84,11 +90,10 @@ class SearchSuggestController extends Controller
|
||||
}
|
||||
RateLimiter::hit($key, 60);
|
||||
|
||||
if (! $this->manticore->isSuggestEnabled()) {
|
||||
if (! $this->searchEngine->isSuggestEnabled()) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'success' => true,
|
||||
'suggestions' => [],
|
||||
'error' => 'Suggest is disabled',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -99,22 +104,29 @@ class SearchSuggestController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
$suggestions = $this->manticore->suggest($query, $index);
|
||||
try {
|
||||
$suggestions = $this->searchEngine->suggest($query, $index);
|
||||
|
||||
// Get the best suggestion (highest doc count)
|
||||
$bestSuggestion = null;
|
||||
if (! empty($suggestions)) {
|
||||
usort($suggestions, fn ($a, $b) => $b['docs'] - $a['docs']);
|
||||
$bestSuggestion = $suggestions[0]['suggest'] ?? null;
|
||||
// Get the best suggestion (highest doc count)
|
||||
$bestSuggestion = null;
|
||||
if (! empty($suggestions)) {
|
||||
usort($suggestions, fn ($a, $b) => $b['docs'] - $a['docs']);
|
||||
$bestSuggestion = $suggestions[0]['suggest'] ?? null;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'query' => $query,
|
||||
'suggestions' => array_map(fn ($s) => $s['suggest'], $suggestions),
|
||||
'best' => $bestSuggestion,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'query' => $query,
|
||||
'suggestions' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'query' => $query,
|
||||
'suggestions' => array_map(fn ($s) => $s['suggest'], $suggestions),
|
||||
'best' => $bestSuggestion,
|
||||
'details' => $suggestions,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,7 +135,7 @@ class SearchSuggestController extends Controller
|
||||
public function searchAssist(Request $request): JsonResponse
|
||||
{
|
||||
$query = $request->input('q', '');
|
||||
$index = $request->input('index', 'releases_rt');
|
||||
$index = $request->input('index') ?? $this->searchEngine->getReleasesIndex();
|
||||
|
||||
// Rate limiting
|
||||
$key = 'searchassist:'.$request->ip();
|
||||
@@ -142,19 +154,23 @@ class SearchSuggestController extends Controller
|
||||
'suggest' => null,
|
||||
];
|
||||
|
||||
// Get autocomplete suggestions
|
||||
if ($this->manticore->isAutocompleteEnabled() && strlen($query) >= 2) {
|
||||
$autocomplete = $this->manticore->autocomplete($query, $index);
|
||||
$response['autocomplete'] = array_map(fn ($s) => $s['suggest'], $autocomplete);
|
||||
}
|
||||
|
||||
// Get spell suggestion if query is complete (user stopped typing)
|
||||
if ($this->manticore->isSuggestEnabled() && ! empty($query)) {
|
||||
$suggestions = $this->manticore->suggest($query, $index);
|
||||
if (! empty($suggestions)) {
|
||||
usort($suggestions, fn ($a, $b) => $b['docs'] - $a['docs']);
|
||||
$response['suggest'] = $suggestions[0]['suggest'] ?? null;
|
||||
try {
|
||||
// Get autocomplete suggestions
|
||||
if ($this->searchEngine->isAutocompleteEnabled() && strlen($query) >= 2) {
|
||||
$autocomplete = $this->searchEngine->autocomplete($query, $index);
|
||||
$response['autocomplete'] = array_map(fn ($s) => $s['suggest'], $autocomplete);
|
||||
}
|
||||
|
||||
// Get spell suggestion if query is complete (user stopped typing)
|
||||
if ($this->searchEngine->isSuggestEnabled() && ! empty($query)) {
|
||||
$suggestions = $this->searchEngine->suggest($query, $index);
|
||||
if (! empty($suggestions)) {
|
||||
usort($suggestions, fn ($a, $b) => $b['docs'] - $a['docs']);
|
||||
$response['suggest'] = $suggestions[0]['suggest'] ?? null;
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// Return empty results on error
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
|
||||
Reference in New Issue
Block a user