<?php
 namespace Module\Question\Util; use ModStart\Core\Util\ArrayUtil; use ModStart\Core\Util\HtmlUtil; use Module\Vendor\ES\ESBaseUtil; class ESUtil extends ESBaseUtil { static $tableProperties = [ 'question' => [ 'id' => ['type' => 'long'], 'questionText' => ['type' => 'text'], ], ]; public static function allIndexes() { return [ 'question' => '问题库', ]; } public static function syncQuestions($questions) { if (empty($questions)) { return; } $records = []; foreach ($questions as $question) { $records[] = [ 'id' => $question['id'], 'questionText' => HtmlUtil::text($question['question']), ]; } self::saveBulk('question', $records); } public static function paginateQuestion($page, $pageSize, $option) { $must = []; foreach ($option['search'] as $searchItem) { foreach ($searchItem as $k => $v) { switch ($k) { case 'keywords': $must[] = ['match' => ['questionText' => $v['like'],]]; break; } } } $params = [ 'index' => 'question', 'type' => 'question', 'body' => [ 'query' => [ 'bool' => [ 'must' => $must, ], ], 'from' => ($page - 1) * $pageSize, 'size' => $pageSize, ] ]; $results = self::client()->search($params); $paginateData = []; $paginateData['total'] = $results['hits']['total']; $paginateData['records'] = ArrayUtil::flatItemsByKey($results['hits']['hits'], '_source'); return $paginateData; } } 