Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Elastic;
- use Elastica;
- class Indexer extends Base {
- public $batchSize = 100;
- public $indexSettings = array(
- 'analysis' => array(
- 'analyzer' => array(
- self::INDEX_ANALYZER => array(
- 'type' => 'custom',
- 'tokenizer' => 'standard',
- 'filter' => array('lowercase', 'russian_morphology', 'english_morphology'),
- 'char_filter' => array('html_strip')
- ),
- self::SEARCH_ANALYZER => array(
- 'type' => 'custom',
- 'tokenizer' => 'standard',
- 'filter' => array('lowercase', 'russian_morphology', 'english_morphology')
- ),
- self::SEARCH_STRICT_ANALYZER => array(
- 'type' => 'custom',
- 'tokenizer' => 'standard',
- 'filter' => array('lowercase')
- )
- )
- )
- );
- public $mappingSettings = array(
- /*'_all'=>array(
- 'analyzer'=>self::INDEX_ANALYZER,
- 'type'=>'string',
- ),*/
- 'title'=>array(
- 'type'=>'string',
- 'include_in_all'=>true,
- 'analyzer'=>self::INDEX_ANALYZER,
- 'boost'=>100
- ),
- 'content'=>array(
- 'type'=>'string',
- 'include_in_all'=>true,
- 'analyzer'=>self::INDEX_ANALYZER,
- 'boost'=>1
- ),
- 'url'=>array(
- 'type'=>'string',
- "index"=>"not_analyzed",
- 'include_in_all'=>false,
- ),
- );
- function initServer($host, $port, $indexName, $typeName)
- {
- parent::initServer($host, $port, $indexName, $typeName);
- $this->index = $this->client->getIndex($indexName);
- $this->index->create($this->indexSettings, true);
- $this->type = $this->index->getType($typeName);
- }
- function initSettings($batchSize) {
- $this->batchSize = $batchSize;
- $this->initMapping();
- }
- /**
- * @param $docsOrClosure - if is array we are just adding it but if is closure we are calling it and add docs from result <br>
- *
- * closure = function($offset, $limit) return $docs
- */
- function run($docsOrClosure) {
- // array
- if (is_array($docsOrClosure)) {
- $this->addDocsFromArray($docsOrClosure);
- }
- // closure
- else {
- $this->addDocsByPackages($docsOrClosure);
- }
- $this->index->refresh();
- }
- function delete() {
- $this->index->delete();
- }
- protected function addDocsByPackages($closure) {
- $offset = 0;
- while (true) {
- $docs = $closure($offset, $this->batchSize);
- $docsNumber = count($docs);
- $offset += $docsNumber;
- if ($docsNumber < 1) {
- break;
- }
- $this->addDocsFromArray($docs);
- }
- }
- protected function addDocsFromArray($array) {
- $docs = array();
- $fields = $this->getFields();
- foreach ($array as $item) {
- $data = array();
- foreach ($fields as $name=>$field) {
- $data[$name] = $item[$name];
- }
- $docs[] = new Elastica\Document('', $data);
- }
- $this->addDocs($docs);
- }
- protected function addDocs($docs) {
- $this->type->addDocuments($docs);
- }
- protected function initMapping() {
- $mapping = new Elastica\Type\Mapping();
- $mapping->setType( $this->type );
- $mapping->setProperties( $this->mappingSettings );
- $this->type->setMapping($mapping);
- }
- protected function getFields() {
- $fields = $this->mappingSettings;
- unset($fields['_all']);
- return $fields;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment