From e295aebc84a039f026a32584c1a75ee115092649 Mon Sep 17 00:00:00 2001 From: random-solver Date: Thu, 5 Nov 2026 02:55:03 +0530 Subject: [PATCH 1/4] feat(elasticsearch): add ElasticSearch indexing on cron, [FACETS] and [RESULTS] qtags (#58) --- .../Common/ElasticSearchEngine.class.php | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/modules/elasticsearch/classes/Common/ElasticSearchEngine.class.php diff --git a/src/modules/elasticsearch/classes/Common/ElasticSearchEngine.class.php b/src/modules/elasticsearch/classes/Common/ElasticSearchEngine.class.php new file mode 100644 index 000000000..2b456b65f --- /dev/null +++ b/src/modules/elasticsearch/classes/Common/ElasticSearchEngine.class.php @@ -0,0 +1,111 @@ +env = $env; + $this->endpoint = rtrim(getenv('QUANTA_ELASTICSEARCH_URL') ?: $endpoint, '/'); + $this->indexName = getenv('QUANTA_ELASTICSEARCH_INDEX') ?: $indexName; + } + + /** + * Indexes a single Quanta Node into ElasticSearch. + */ + public function indexNode(Node $node): bool { + if (!$node->exists) { + return FALSE; + } + $doc = [ + 'name' => $node->getName(), + 'title' => $node->getTitle(), + 'teaser' => $node->getTeaser(), + 'body' => strip_tags((string) $node->getBody()), + 'father' => $node->getFather() ? $node->getFather()->getName() : '', + 'status' => $node->getStatus(), + 'language' => $node->getLanguage(), + 'timestamp' => $node->getTimestamp(), + 'attributes' => $node->json ?? [], + ]; + $url = $this->endpoint . '/' . urlencode($this->indexName) . '/_doc/' . urlencode($node->getName()); + return $this->request('PUT', $url, $doc) !== NULL; + } + + /** + * Executes a search query with optional facet filters and returns matching hits + aggregations. + */ + public function search(string $queryText = '', array $filters = [], array $facetFields = [], int $size = 20): array { + $must = []; + if (!empty($queryText)) { + $must[] = [ + 'multi_match' => [ + 'query' => $queryText, + 'fields' => ['title^3', 'teaser^2', 'body'], + ], + ]; + } + + $filterClauses = []; + foreach ($filters as $field => $val) { + if ($val !== '' && $val !== NULL) { + $filterClauses[] = ['term' => [$field . '.keyword' => $val]]; + } + } + + $aggs = []; + foreach ($facetFields as $field) { + $aggs[$field] = [ + 'terms' => [ + 'field' => $field . '.keyword', + 'size' => 25, + ], + ]; + } + + $payload = [ + 'size' => $size, + 'query' => [ + 'bool' => [ + 'must' => !empty($must) ? $must : [['match_all' => (object) []]], + 'filter' => $filterClauses, + ], + ], + ]; + if (!empty($aggs)) { + $payload['aggs'] = $aggs; + } + + $url = $this->endpoint . '/' . urlencode($this->indexName) . '/_search'; + return $this->request('POST', $url, $payload) ?: ['hits' => ['hits' => []], 'aggregations' => []]; + } + + protected function request(string $method, string $url, array $payload): ?array { + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_RETURNTRANSFER => TRUE, + CURLOPT_HTTPHEADER => ['Content-Type: application/json'], + CURLOPT_POSTFIELDS => json_encode($payload), + CURLOPT_TIMEOUT => 5, + ]); + $raw = curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + if ($raw === FALSE || $code >= 400) { + return NULL; + } + return json_decode($raw, TRUE); + } +} From f0fb58a661441d3bd060b3f037d9b47349bdfb97 Mon Sep 17 00:00:00 2001 From: random-solver Date: Thu, 5 Nov 2026 02:55:05 +0530 Subject: [PATCH 2/4] feat(elasticsearch): add ElasticSearch indexing on cron, [FACETS] and [RESULTS] qtags (#58) --- .../classes/Qtags/Facets.qtag.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/modules/elasticsearch/classes/Qtags/Facets.qtag.php diff --git a/src/modules/elasticsearch/classes/Qtags/Facets.qtag.php b/src/modules/elasticsearch/classes/Qtags/Facets.qtag.php new file mode 100644 index 000000000..b841f6b7c --- /dev/null +++ b/src/modules/elasticsearch/classes/Qtags/Facets.qtag.php @@ -0,0 +1,34 @@ +getTarget(); + if (empty($field)) { + return ''; + } + $engine = new ElasticSearchEngine($this->env); + $query = isset($_GET['q']) ? (string) $_GET['q'] : ''; + $res = $engine->search($query, [], [$field], 0); + $buckets = $res['aggregations'][$field]['buckets'] ?? []; + + $selected = isset($_GET['facet_' . $field]) ? (string) $_GET['facet_' . $field] : ''; + $html = '
'; + $html .= '
    '; + foreach ($buckets as $bucket) { + $key = (string) ($bucket['key'] ?? ''); + $count = (int) ($bucket['doc_count'] ?? 0); + $activeClass = ($selected === $key) ? ' class="active"' : ''; + $url = '?q=' . urlencode($query) . '&facet_' . urlencode($field) . '=' . urlencode($key); + $html .= '' + . htmlspecialchars($key) . ' (' . $count . ')'; + } + $html .= '
'; + return $html; + } +} From 4a101ef8743260163f154868d24dd48e717ace29 Mon Sep 17 00:00:00 2001 From: random-solver Date: Thu, 5 Nov 2026 02:55:07 +0530 Subject: [PATCH 3/4] feat(elasticsearch): add ElasticSearch indexing on cron, [FACETS] and [RESULTS] qtags (#58) --- .../classes/Qtags/Results.qtag.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/modules/elasticsearch/classes/Qtags/Results.qtag.php diff --git a/src/modules/elasticsearch/classes/Qtags/Results.qtag.php b/src/modules/elasticsearch/classes/Qtags/Results.qtag.php new file mode 100644 index 000000000..f858ec704 --- /dev/null +++ b/src/modules/elasticsearch/classes/Qtags/Results.qtag.php @@ -0,0 +1,45 @@ +env); + $query = isset($_GET['q']) ? (string) $_GET['q'] : (string) $this->getTarget(); + $limit = (int) ($this->getAttribute('limit') ?: 20); + + $filters = []; + foreach ($_GET as $k => $v) { + if (strpos($k, 'facet_') === 0 && $v !== '') { + $filters[substr($k, 6)] = (string) $v; + } + } + + $res = $engine->search($query, $filters, [], $limit); + $hits = $res['hits']['hits'] ?? []; + + if (empty($hits)) { + return '
No matching results found.
'; + } + + $html = '
'; + foreach ($hits as $hit) { + $src = $hit['_source'] ?? []; + $name = htmlspecialchars((string) ($src['name'] ?? ''), ENT_QUOTES); + $title = htmlspecialchars((string) ($src['title'] ?? $name)); + $teaser = htmlspecialchars((string) ($src['teaser'] ?? '')); + $html .= '
'; + $html .= '

' . $title . '

'; + if (!empty($teaser)) { + $html .= '

' . $teaser . '

'; + } + $html .= '
'; + } + $html .= '
'; + return $html; + } +} From 1d025ac424eaac0244e04a9a1a07e8e0d078822e Mon Sep 17 00:00:00 2001 From: random-solver Date: Thu, 5 Nov 2026 02:55:09 +0530 Subject: [PATCH 4/4] feat(elasticsearch): add ElasticSearch indexing on cron, [FACETS] and [RESULTS] qtags (#58) --- .../hooks/elasticsearch.hook.inc | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/modules/elasticsearch/hooks/elasticsearch.hook.inc diff --git a/src/modules/elasticsearch/hooks/elasticsearch.hook.inc b/src/modules/elasticsearch/hooks/elasticsearch.hook.inc new file mode 100644 index 000000000..aea536eda --- /dev/null +++ b/src/modules/elasticsearch/hooks/elasticsearch.hook.inc @@ -0,0 +1,20 @@ +scanDirectoryDeep($env->dir['docroot'], '', [], FileObject::DIR_SUBDIRS); + foreach ($dirs as $dirPath) { + $nodeName = basename($dirPath); + if (strpos($nodeName, '_') === 0 || strpos($nodeName, '.') === 0) { + continue; + } + $node = NodeFactory::load($env, $nodeName); + if ($node && $node->exists) { + $engine->indexNode($node); + } + } +}