Commit 8485e099 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Système de recherche en direct depuis Elasticsearch en cours (Non fonctonnel)

parent 1a729969
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -458,6 +458,8 @@ return array(
                        'delete',
                        'fiche',
                        'index',
                        'index2',
                        'reindexOne',
                        'search',
                        'suborganization',
                        'create'
+21 −0
Original line number Diff line number Diff line
@@ -807,6 +807,23 @@ organization:
      action: index
  may_terminate: true
  child_routes:

    list2:
      type: segment
      options:
        route: '/list2'
        defaults:
          action: index2
      may_terminate: true

    reindexOne:
      type: segment
      options:
        route: '/reindex-one/:id'
        defaults:
          action: reindexOne
      may_terminate: true

    fiche:
      type: segment
      options:
@@ -822,24 +839,28 @@ organization:
        defaults:
          action: show
      may_terminate: true

    search:
      type: segment
      options:
        route: '/search'
        defaults:
          action: search

    export:
      type: segment
      options:
        route: '/export'
        defaults:
          action: exportCsv

    merge:
      type: segment
      options:
        route: '/merge'
        defaults:
          action: merge

    edit:
      type: segment
      options:
+33 −0
Original line number Diff line number Diff line
@@ -99,6 +99,39 @@ class OrganizationController extends AbstractOscarController implements UseOrgan
        ];
    }

    public function reindexOneAction()
    {
        $this->getOscarUserContextService()->check(Privileges::ORGANIZATION_EDIT);
        $id = $this->params()->fromRoute('id');
        $organization = $this->getOrganizationService()->getOrganization($id);
        $this->getOrganizationService()->getSearchEngineStrategy()->update($organization);
        return $this->redirect()->toRoute("organization/show", ['id' => $id]);
    }

    public function index2Action()
    {
        if( $this->isAjax() || $this->params()->fromQuery('f') === 'json'){
            try {
                $page = (int)$this->params()->fromQuery('page', 1);
                $search = $this->params()->fromQuery('q', '');
                $type = $this->params()->fromQuery('t', []);
                $active = $this->params()->fromQuery('active', '');
                $sort = $this->params()->fromQuery('sort', 'hit');
                $direction = $this->params()->fromQuery('direction', 'ASC');
                $view = new JsonModel();
                $filters = [];
                $filters['active'] = $active;
                $result = $this->getOrganizationService()->searchOrganizations($search, $page, $filters);
                $view->setVariables($result);
            } catch (\Exception $exception) {
                return $this->jsonError($exception->getmessage());
            }

            return $view;
        }
        return [];
    }

    /**
     * Liste des organisations.
     *
+26 −0
Original line number Diff line number Diff line
@@ -723,6 +723,32 @@ class OrganizationService implements UseOscarConfigurationService, UseEntityMana
        return new UnicaenDoctrinePaginator($qb, $page);
    }

    public function searchOrganizations(string $search, int $page, array $filter = []): array
    {
        $body = [
            '_source' => [
                'excludes' => ['projects', 'activities']
            ],
            'query' => [
                'bool' => [
                    'filter' => [
                        [ 'term' => ['close' => false] ]
                    ]
                ]
            ]
        ];
        $dt = $this->getSearchEngineStrategy()->searchRaw($search, 10000, $body);
        $organizations = [];
        foreach ($dt['hits']['hits'] as $hit) {
            $dt = $hit['_source'];
            $dt['_score'] = $hit['_score'];
            $organizations[] = $dt;
        }
        return [
            'organizations' => $organizations
        ];
    }

    /**
     * @param $search
     * @param $filter
+20 −9
Original line number Diff line number Diff line
@@ -134,7 +134,6 @@ abstract class ElasticSearchEngine
        try {
            $i = 0;
            foreach ($items as $item) {
                $this->loggerService->debug(" + prepare " . $item->getId());
                $i++;
                $params['body'][] = [
                    'index' => [
@@ -174,18 +173,28 @@ abstract class ElasticSearchEngine
     * @param int $limit
     * @return array
     */
    public function getParamsQuery(string $textSearch, int $limit = 10000): array
    public function getParamsQuery(string $textSearch, int $limit = 10000, $customBody = null): array
    {
        $search = trim($textSearch);

        if( $customBody == null ){
            $body = [
                'size' => $limit,
            ];
        } else {
            $body = $customBody;
        }
        if( array_key_exists('query', $body) ){
            $filter = $body['query']['bool']['filter'];
            $body['query'] = $this->getFieldsSearchedWeighted($search);
            $body['query']['bool']['filter'] = $filter;
        }

        $query = [
            'index' => $this->getIndex(),
            'size' => 50,
            //'type'  => $this->getType(),
            'body'  => [
                'size'  => $limit,
                "query" => $this->getFieldsSearchedWeighted($search)
            ]
            'body'  => $body
        ];

        $this->loggerService->info("----query elastic : \n" . json_encode($query['body']['query']) ."\n---- ");
@@ -196,18 +205,18 @@ abstract class ElasticSearchEngine
    /**
     * @throws OscarException
     */
    public function searchRaw(string $search, int $limit = 10000): array
    public function searchRaw(string $search, int $limit = 10000, $body = null): array
    {
        $this->loggerService->info("Search '$search' in " . $this->getIndex());
        $client = $this->getClient();
        try {
            $params = $this->getParamsQuery($search, $limit);
            $params = $this->getParamsQuery($search, $limit, $body);
            $ids = $client->search($params);
            return $ids;
        } catch (\Throwable $exception) {
            $ex = ElasticSearchEngineException::getInstance($exception);
            $this->loggerService->error($ex->getMessage());
            throw new OscarException($ex->getMessagePublic());
            throw new OscarException($ex->getMessage());
        }
    }

@@ -275,6 +284,8 @@ abstract class ElasticSearchEngine
                'doc' => $this->getIndexableDatas($item)
            ]
        ];

        error_log("REINDEX " . json_encode($params));
        try {
            return $this->getClient()->update($params);
        } catch (Missing404Exception $e) {
Loading