Ojs\CoreBundle\Service\Search\NativeQueryGenerator::generateNativeQuery PHP Метод

generateNativeQuery() публичный Метод

native query builder base router
public generateNativeQuery ( $section, boolean $setupAggs = true ) : array | boolean | mixed | null
$section
$setupAggs boolean if we want to only result data for caculate result count you can pass false
Результат array | boolean | mixed | null
    public function generateNativeQuery($section, $setupAggs = true)
    {
        $this->setupAggs = $setupAggs;
        // decide if query has special query route to own route
        if (preg_match('/journal:/', $this->getQuery())) {
            $this->nativeQuery = $this->journalQueryGenerator($section);
        } elseif (preg_match('/advanced:/', $this->getQuery())) {
            $this->nativeQuery = $this->advancedQueryGenerator($section);
        } elseif (preg_match('/tag:/', $this->getQuery())) {
            $this->nativeQuery = $this->tagQueryGenerator($section);
        } else {
            $this->nativeQuery = $this->basicQueryGenerator($section);
        }
        return $this->nativeQuery;
    }

Usage Example

Пример #1
0
 /**
  * @return $this
  */
 public function setupQueryResultSet()
 {
     $results = [];
     foreach ($this->getSectionList() as $section) {
         $setupAggs = $section == $this->getSection() ? true : false;
         $nativeQuery = $this->nativeQueryGenerator->generateNativeQuery($section, $setupAggs);
         if ($nativeQuery === false) {
             continue;
         }
         /** @var \Elastica\ResultSet $resultData */
         $resultData = $this->container->get('fos_elastica.index.search.' . $section)->search($nativeQuery);
         if ($resultData->getTotalHits() < 1) {
             continue;
         }
         $this->setTotalHit($this->getTotalHit() + $resultData->getTotalHits());
         if ($section !== $this->getSection()) {
             $results[$section]['total_item'] = $resultData->getTotalHits();
             $results[$section]['type'] = $this->translator->trans($section);
             continue;
         }
         $this->setCurrectSectionHit($resultData->getTotalHits());
         /**
          * @var Result $object
          */
         foreach ($resultData as $resultObject) {
             $objectDetail = $this->getObjectDetail($resultObject);
             $results[$section]['total_item'] = $resultData->getTotalHits();
             $results[$section]['type'] = $this->translator->trans($section);
             $result['detail'] = $objectDetail;
             $result['source'] = $resultObject->getSource();
             $results[$section]['data'][] = $result;
         }
         $resultAggs = $resultData->getAggregations();
         foreach ($resultAggs as $aggKey => $agg) {
             if (count($agg['buckets']) < 1) {
                 unset($resultAggs[$aggKey]);
             }
         }
         $this->setAggs($resultAggs);
     }
     $this->setResultSet($results);
     return $this;
 }