-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocomplete.php
72 lines (60 loc) · 2.13 KB
/
autocomplete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* @category Bubble
* @package Bubble_Elasticsearch
* @version 4.1.2
* @copyright Copyright (c) 2016 BubbleShop (https://www.bubbleshop.net)
*/
define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
if (isset($_SERVER['SCRIPT_FILENAME']) && is_link($_SERVER['SCRIPT_FILENAME'])) {
define('BP', dirname($_SERVER['SCRIPT_FILENAME']));
} else {
define('BP', dirname(__FILE__));
}
// Configure include path
$paths = array();
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
$paths[] = BP . DS . 'lib';
$appPath = implode(PS, $paths);
set_include_path($appPath . PS . get_include_path());
// Register autoload
spl_autoload_register(function($class) {
$classFile = str_replace('\\', '/', $class, $count);
if (!$count) {
$classFile = str_replace(' ', DS, ucwords(str_replace('_', ' ', $class)));
}
$classFile .= '.php';
include $classFile;
});
header('Content-Type: text/html; charset=UTF-8');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
$html = '';
$q = isset($_GET['q']) ? $_GET['q'] : '';
$found = false;
if ('' !== $q) {
$store = isset($_GET['store']) ? $_GET['store'] : '';
$config = new Bubble_Elasticsearch_Config($store);
try {
if (!$config->getData()) {
throw new Exception('Could not find config for autocomplete');
}
$client = new Bubble_Elasticsearch_Client($config->getClientConfig());
$index = new Bubble_Elasticsearch_Index($client, $client->getIndexAlias($store));
$index->setAnalyzers($config->getAnalyzers());
$autocomplete = new Bubble_Elasticsearch_Autocomplete($config);
$html = $autocomplete->search($q, $index);
$found = true;
} catch (Exception $e) {
if (isset($_GET['fallback_url'])) {
$url = $_GET['fallback_url'] . '?q=' . $q;
$html = @file_get_contents($url);
}
}
}
header('Fast-Autocomplete: ' . ($found ? 'HIT' : 'MISS'));
echo $html;
exit;