see613

elastic_indexer

Jun 2nd, 2014
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.92 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Elastic;
  4. use Elastica;
  5.  
  6.  
  7. class Indexer extends Base {
  8.     public $batchSize = 100;
  9.     public $indexSettings = array(
  10.         'analysis' => array(
  11.             'analyzer' => array(
  12.                 self::INDEX_ANALYZER => array(
  13.                     'type' => 'custom',
  14.                     'tokenizer' => 'standard',
  15.                     'filter' => array('lowercase', 'russian_morphology', 'english_morphology'),
  16.                     'char_filter' => array('html_strip')
  17.                 ),
  18.                 self::SEARCH_ANALYZER => array(
  19.                     'type' => 'custom',
  20.                     'tokenizer' => 'standard',
  21.                     'filter' => array('lowercase', 'russian_morphology', 'english_morphology')
  22.                 ),
  23.                 self::SEARCH_STRICT_ANALYZER => array(
  24.                     'type' => 'custom',
  25.                     'tokenizer' => 'standard',
  26.                     'filter' => array('lowercase')
  27.                 )
  28.             )
  29.         )
  30.     );
  31.     public $mappingSettings = array(
  32.         /*'_all'=>array(
  33.             'analyzer'=>self::INDEX_ANALYZER,
  34.             'type'=>'string',
  35.         ),*/
  36.         'title'=>array(
  37.             'type'=>'string',
  38.             'include_in_all'=>true,
  39.             'analyzer'=>self::INDEX_ANALYZER,
  40.             'boost'=>100
  41.         ),
  42.         'content'=>array(
  43.             'type'=>'string',
  44.             'include_in_all'=>true,
  45.             'analyzer'=>self::INDEX_ANALYZER,
  46.             'boost'=>1
  47.         ),
  48.         'url'=>array(
  49.             'type'=>'string',
  50.             "index"=>"not_analyzed",
  51.             'include_in_all'=>false,
  52.         ),
  53.     );
  54.  
  55.  
  56.     function initServer($host, $port, $indexName, $typeName)
  57.     {
  58.         parent::initServer($host, $port, $indexName, $typeName);
  59.  
  60.         $this->index = $this->client->getIndex($indexName);
  61.         $this->index->create($this->indexSettings, true);
  62.  
  63.         $this->type = $this->index->getType($typeName);
  64.     }
  65.  
  66.     function initSettings($batchSize) {
  67.         $this->batchSize = $batchSize;
  68.  
  69.         $this->initMapping();
  70.     }
  71.  
  72.     /**
  73.      * @param $docsOrClosure - if is array we are just adding it but if is closure we are calling it and add docs from result <br>
  74.      *
  75.      * closure = function($offset, $limit)  return $docs
  76.      */
  77.     function run($docsOrClosure) {
  78.         // array
  79.         if (is_array($docsOrClosure)) {
  80.             $this->addDocsFromArray($docsOrClosure);
  81.         }
  82.         // closure
  83.         else {
  84.             $this->addDocsByPackages($docsOrClosure);
  85.         }
  86.  
  87.         $this->index->refresh();
  88.     }
  89.  
  90.     function delete() {
  91.         $this->index->delete();
  92.     }
  93.  
  94.     protected function addDocsByPackages($closure) {
  95.         $offset = 0;
  96.  
  97.         while (true) {
  98.             $docs = $closure($offset, $this->batchSize);
  99.             $docsNumber = count($docs);
  100.             $offset += $docsNumber;
  101.  
  102.             if ($docsNumber < 1) {
  103.                 break;
  104.             }
  105.  
  106.             $this->addDocsFromArray($docs);
  107.         }
  108.     }
  109.  
  110.     protected function addDocsFromArray($array) {
  111.         $docs = array();
  112.         $fields = $this->getFields();
  113.  
  114.         foreach ($array as $item) {
  115.             $data = array();
  116.  
  117.             foreach ($fields as $name=>$field) {
  118.                 $data[$name] = $item[$name];
  119.             }
  120.  
  121.             $docs[] = new Elastica\Document('', $data);
  122.         }
  123.  
  124.         $this->addDocs($docs);
  125.     }
  126.  
  127.     protected function addDocs($docs) {
  128.         $this->type->addDocuments($docs);
  129.     }
  130.  
  131.     protected function initMapping() {
  132.         $mapping = new Elastica\Type\Mapping();
  133.         $mapping->setType( $this->type );
  134.         $mapping->setProperties( $this->mappingSettings );
  135.  
  136.         $this->type->setMapping($mapping);
  137.     }
  138.  
  139.     protected function getFields() {
  140.         $fields = $this->mappingSettings;
  141.         unset($fields['_all']);
  142.  
  143.         return $fields;
  144.     }
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment