Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search frontend #505

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '2'
services:
mysql:
image: 'mysql:5.7'
restart: always
restart: unless-stopped
volumes:
- './dump/mysql:/dump'
- mysqldata:/var/lib/mysql
Expand All @@ -15,14 +15,14 @@ services:

redis:
image: 'redis:6.0.5-alpine'
restart: always
restart: unless-stopped
command: ["redis-server", "--save 900 1", "--save 300 10", "--save 60 10000"]
volumes:
- ./dump/redis:/data

elasticsearch:
image: 'elasticsearch:7.4.0'
restart: always
restart: unless-stopped
environment:
- xpack.security.enabled=false
- discovery.type=single-node
Expand All @@ -42,7 +42,7 @@ services:
build:
dockerfile: 'docker/php/Dockerfile'
context: '.'
restart: always
restart: unless-stopped
depends_on:
- redis
- mysql
Expand All @@ -58,7 +58,7 @@ services:

nginx:
image: 'nginx:1.19.1-alpine'
restart: always
restart: unless-stopped
ports:
- "127.0.0.1:${APP_PORT}:8080"
links:
Expand Down
5 changes: 3 additions & 2 deletions www/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"XMLHttpRequest": true,
"ActiveXObject": true,
"FileReader": true,
"process": true
"process": true,
"setTimeout": true
}
}
}
57 changes: 44 additions & 13 deletions www/application/classes/Controller/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,51 @@ public function action_search()
* Perform search for specified phrase using *query* pattern
*/
$query = htmlspecialchars(Arr::get($_GET, 'query', ''));
$response = $this->elastic->searchByField(
Model_Page::ELASTIC_TYPE,
self::MAX_SEARCH_RESULTS,
Model_Page::ELASTIC_SEARCH_FIELD,
$query
);

/**
* Return Model_Page[] to user
*/
$result = array_map(function ($item) {
return new Model_Page($item['_id']);
}, $response['hits']['hits']);
$response = [];
$success = 0;
$error = "";

try {
$response = $this->elastic->searchByField(
Model_Page::ELASTIC_TYPE,
self::MAX_SEARCH_RESULTS,
Model_Page::ELASTIC_SEARCH_FIELDS,
$query
);

$success = 1;
} catch (Exception $err) {
$error = $err->getMessage();
}

if ($success) {
/**
* Return Model_Page[] to user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

странный комментарий

*/
$result = array_map(function ($item) {
return new Model_Page($item['_id']);
}, $response['hits']['hits']);

/**
* Sort by date: display newest first
*/
usort($result, function($first, $second){
return $first->date < $second->date;
});

$search_result['html'] = View::factory(
'templates/pages/list',
array(
'pages' => $result,
'active_tab' => Model_Feed_Pages::ALL,
'emptyListMessage' => 'Ничего не найдено'
)
)->render();
} else {
$search_result['error'] = $error;
}

$this->response->body(@json_encode($result));
$this->response->body(@json_encode($search_result));
}
}
9 changes: 5 additions & 4 deletions www/application/classes/Model/Elastic.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public function get($type, $id)
/**
* @param $type - entity type (table in elastic db)
* @param $size - maximum search results to return
* @param $field - in what entity field to search
* @param $fields - in what entity fields to search
* @param $query - what occurrence to search
*
* @return array - search result
*/
public function searchByField($type, $size, $field, $query)
public function searchByField($type, $size, $fields, $query)
{
return $this->client->search(
[
Expand All @@ -70,8 +70,9 @@ public function searchByField($type, $size, $field, $query)
'type' => $type,
'body' => [
'query' => [
'match' => [
$field => '*' . $query . '*'
'simple_query_string' => [
'query' => $query . '*',
'fields' => $fields
]
]
]
Expand Down
2 changes: 1 addition & 1 deletion www/application/classes/Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Model_Page extends Model
/**
* Field in elastic db used to store searchable page content: paragraphs, headings and lists
*/
const ELASTIC_SEARCH_FIELD = 'text';
const ELASTIC_SEARCH_FIELDS = ['text', 'title'];

private $modelCacheKey;

Expand Down
191 changes: 96 additions & 95 deletions www/application/views/main.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="language" content="<?= I18n::$lang ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="<?= Arr::get($site_info, 'title', 'CodeX Media') ?>" />
<title>
<!DOCTYPE html>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="language" content="<?= I18n::$lang ?>">

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="<?= Arr::get($site_info, 'title', 'CodeX Media') ?>" />

<title>
<?= $title ?: Arr::get($site_info, 'title', 'CodeX Media') . ': ' . Arr::get($site_info, 'description', '') ?>
</title>
<base href="/" />
<!-- <link href="https://fonts.googleapis.com/css?family=Lora:400,400i,700&subset=cyrillic" rel="stylesheet"> -->
<link rel="stylesheet" type="text/css" media="all" href="/public/build/bundle.css?v=<?= filemtime('public/build/bundle.css'); ?>">
<link rel="icon" type="image/png" href="/favicon.png">
</title>

<base href="/" />

<!-- <link href="https://fonts.googleapis.com/css?family=Lora:400,400i,700&subset=cyrillic" rel="stylesheet"> -->

<link rel="stylesheet" type="text/css" media="all" href="/public/build/bundle.css?v=<?= filemtime('public/build/bundle.css'); ?>">
<link rel="icon" type="image/png" href="/favicon.png">

<? if (!empty($site_info['meta_image'])): ?>
<meta name="image" property="og:image" content="<?= $site_info['meta_image'] ?>" />
<link rel="image_src" href="<?= $site_info['meta_image'] ?>" />
<meta name="image" property="og:image" content="<?= $site_info['meta_image'] ?>" />
<link rel="image_src" href="<?= $site_info['meta_image'] ?>" />
<? endif; ?>
<script src="/public/build/main.bundle.js?v=<?= filemtime('public/build/main.bundle.js'); ?>" onload="codex.init({uploadMaxSize : <?= UPLOAD_MAX_SIZE ?>})"></script>
</head>

<script src="/public/build/main.bundle.js?v=<?= filemtime('public/build/main.bundle.js'); ?>" onload="codex.init({uploadMaxSize : <?= UPLOAD_MAX_SIZE ?>})"></script>

</head>
<?
$bodyModifiers = [];

Expand All @@ -44,94 +44,95 @@
}
}
?>
<body class="<?= implode(' ', $bodyModifiers) ?>">
<body class="<?= implode(' ', $bodyModifiers) ?>">

<?= View::factory('templates/components/header')->render(); ?>

<? if (Arr::get($_SERVER, 'ENABLE_GOV_SITE_WIDGET')): ?>
<?= View::factory('templates/components/esir_navigator')->render(); ?>
<?= View::factory('templates/components/search')->render(); ?>
<? endif ?>

<? if (empty($hideBranding)): ?>
<?= View::factory('templates/components/branding')->render(); ?>
<? endif; ?>
<div class="center-col" id="js-layout-holder" data-module="layout">
<div class="grid-cols-wrapper">

<div class="center-col" id="js-layout-holder" data-module="layout">

<div class="grid-cols-wrapper">

<? /* Left */ ?>
<div class="grid-col grid-col--left">
<div class="grid-col grid-col--left">
<? if (!empty($aside)): ?>
<?= $aside ?>
<? else: ?>
<?= View::factory('templates/components/aside')->render(); ?>
<? endif; ?>
</div>
</div>

<? /* Main block for page */ ?>
<div class="grid-content <?= !empty($contentOnly) ? 'grid-content--stretched' : '' ?>">
<div class="grid-content <?= !empty($contentOnly) ? 'grid-content--stretched' : '' ?>">
<?= $content ?>
</div>
</div>

<? /* Right col */ ?>
<?= View::factory('templates/components/right_col')->render(); ?>
</div>
</div>
<script>
window.csrf = '<?= Security::token(); ?>';
</script>

</div>

</div>


<script>

window.csrf = '<?= Security::token(); ?>';

</script>

<? if (!empty($_SERVER['HAWK_TOKEN'])): ?>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/hawk.min.js" onload="hawk.init('<?= $_SERVER['HAWK_TOKEN'] ?>');"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/hawk.min.js" onload="hawk.init('<?= $_SERVER['HAWK_TOKEN'] ?>');"></script>
<? endif; ?>
<script src="/public/extensions/emoji-parser/specc-emoji.js?v=<?= filemtime('public/extensions/emoji-parser/specc-emoji.js') ?>" onload="Emoji.parse()"></script>

<script src="/public/extensions/emoji-parser/specc-emoji.js?v=<?= filemtime('public/extensions/emoji-parser/specc-emoji.js') ?>" onload="Emoji.parse()"></script>

<? if (Kohana::$environment === Kohana::PRODUCTION): ?>

<? if (!empty($_SERVER['ENABLE_GOV_SITE_WIDGET']) && $_SERVER['ENABLE_GOV_SITE_WIDGET']): ?>
<script type="text/javascript" src="https://esir.gov.spb.ru/static/widget/js/widget.js" charset="utf-8"></script>
<script type="text/javascript" src="https://esir.gov.spb.ru/static/widget/js/widget.js" charset="utf-8"></script>
<? endif; ?>

<? if (!empty($_SERVER['YANDEX_METRIKA_ID'])): ?>
<!-- Yandex.Metrika counter -->
<script type="text/javascript">
(function (d, w, c) {
(w[c] = w[c] || []).push(function() {
try {
w.yaCounter<?= $_SERVER['YANDEX_METRIKA_ID'] ?> = new Ya.Metrika({
id:<?= $_SERVER['YANDEX_METRIKA_ID'] ?>,
clickmap:true,
trackLinks:true,
accurateTrackBounce:true
});
} catch(e) { }
});
var n = d.getElementsByTagName("script")[0],
s = d.createElement("script"),
f = function () { n.parentNode.insertBefore(s, n); };
s.type = "text/javascript";
s.async = true;
s.src = "https://mc.yandex.ru/metrika/watch.js";
if (w.opera == "[object Opera]") {
d.addEventListener("DOMContentLoaded", f, false);
} else { f(); }
})(document, window, "yandex_metrika_callbacks");
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/<?= $_SERVER['YANDEX_METRIKA_ID'] ?>" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->
<!-- Yandex.Metrika counter -->
<script type="text/javascript">
(function (d, w, c) {
(w[c] = w[c] || []).push(function() {
try {
w.yaCounter<?= $_SERVER['YANDEX_METRIKA_ID'] ?> = new Ya.Metrika({
id:<?= $_SERVER['YANDEX_METRIKA_ID'] ?>,
clickmap:true,
trackLinks:true,
accurateTrackBounce:true
});
} catch(e) { }
});

var n = d.getElementsByTagName("script")[0],
s = d.createElement("script"),
f = function () { n.parentNode.insertBefore(s, n); };
s.type = "text/javascript";
s.async = true;
s.src = "https://mc.yandex.ru/metrika/watch.js";

if (w.opera == "[object Opera]") {
d.addEventListener("DOMContentLoaded", f, false);
} else { f(); }
})(document, window, "yandex_metrika_callbacks");
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/<?= $_SERVER['YANDEX_METRIKA_ID'] ?>" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->
<? endif; ?>

<? endif; ?>
</body>
</html>

</body>
</html>
Loading