diff --git a/docker-compose.yml b/docker-compose.yml index 02cdfbd9a..6ebe77582 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 @@ -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 @@ -42,7 +42,7 @@ services: build: dockerfile: 'docker/php/Dockerfile' context: '.' - restart: always + restart: unless-stopped depends_on: - redis - mysql @@ -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: diff --git a/www/.eslintrc b/www/.eslintrc index 9f0369133..e911e5f1e 100644 --- a/www/.eslintrc +++ b/www/.eslintrc @@ -76,6 +76,7 @@ "XMLHttpRequest": true, "ActiveXObject": true, "FileReader": true, - "process": true + "process": true, + "setTimeout": true } -} \ No newline at end of file +} diff --git a/www/application/classes/Controller/Search.php b/www/application/classes/Controller/Search.php index 9713cdddd..e99fd7b13 100644 --- a/www/application/classes/Controller/Search.php +++ b/www/application/classes/Controller/Search.php @@ -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 pages search result to user + */ + $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)); } } diff --git a/www/application/classes/Model/Elastic.php b/www/application/classes/Model/Elastic.php index f31043d9e..dcc55d451 100644 --- a/www/application/classes/Model/Elastic.php +++ b/www/application/classes/Model/Elastic.php @@ -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( [ @@ -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 ] ] ] diff --git a/www/application/classes/Model/Page.php b/www/application/classes/Model/Page.php index 4916955cd..fd83fc43b 100644 --- a/www/application/classes/Model/Page.php +++ b/www/application/classes/Model/Page.php @@ -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; diff --git a/www/application/views/main.php b/www/application/views/main.php index 203b71735..658f631d1 100644 --- a/www/application/views/main.php +++ b/www/application/views/main.php @@ -1,35 +1,35 @@ - - -
- - - - - - - - - - -