Advertisement
see613

elastic_search

Jun 2nd, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Elastic;
  4. use Elastica;
  5.  
  6.  
  7. class Search extends Base {
  8.     public $offset;
  9.     public $limit;
  10.     public $strict;
  11.     public $result;
  12.     public $phrase;
  13.     public $phraseInOtherLang;
  14.     public $docsNumber;
  15.     public $docsNumberInOtherLang;
  16.  
  17.     public $suggestSmoothing = 0.4;
  18.     public $highlightPrefix = '*';
  19.     public $highlightPostfix = '*';
  20.     /** fields for search */
  21.     public $fields = array(
  22.         'title'=>array(
  23.             'boost'=>100
  24.         ),
  25.         'content'=>array(
  26.             'boost'=>1
  27.         )
  28.     );
  29.     public $highlightSettings = array(
  30.         'fields' => array(
  31.             'content' => array(
  32.                 'fragment_size' => 100,
  33.                 'number_of_fragments' => 5,
  34.             ),
  35.         ),
  36.     );
  37.  
  38.  
  39.     function __construct()
  40.     {
  41.         parent::__construct();
  42.  
  43.         $this->highlightSettings['pre_tags'] = array($this->highlightPrefix);
  44.         $this->highlightSettings['post_tags'] = array($this->highlightPostfix);
  45.     }
  46.  
  47.     protected function setPhrase($phrase) {
  48.         $phrase = str_replace('/', '-', $phrase);
  49.  
  50.         $this->phrase = \StringHelper::cleanSearchPhrase( $phrase );
  51.         // если раскладка клавиатуры неправильная
  52.         $this->phraseInOtherLang = \StringHelper::cleanSearchPhrase( \StringHelper::switchKeyboardLang( $phrase ) );
  53.     }
  54.  
  55.     function initServer($host, $port, $indexName, $typeName)
  56.     {
  57.         parent::initServer($host, $port, $indexName, $typeName);
  58.  
  59.         $this->index = $this->client->getIndex($indexName);
  60.         $this->type = $this->index->getType($typeName);
  61.     }
  62.  
  63.     function initSettings($phrase, $strict, $offset, $limit = null) {
  64.         $this->strict = $strict;
  65.         $this->offset = $offset;
  66.         $this->limit = $limit;
  67.  
  68.         $this->setPhrase($phrase);
  69.     }
  70.  
  71.     protected function addActualQuery($phrase) {
  72.         if ($this->strict) {
  73.             return $this->addQuery( $this->addMatchPhrase($phrase) );
  74.         }
  75.         else {
  76.             return $this->addQuery( $this->addQueryString($phrase) );
  77.         }
  78.     }
  79.  
  80.     protected function count($phrase) {
  81.         return $this->_count( $this->addActualQuery($phrase) );
  82.     }
  83.  
  84.     protected function search($phrase) {
  85.         return $this->_search( $this->addActualQuery($phrase), $this->getSearchOptions() );
  86.     }
  87.  
  88.     protected function getSearchOptions() {
  89.         $options = array(
  90.             'from'=>$this->offset
  91.         );
  92.  
  93.         if ( $this->limit !== null ) {
  94.             $options['size'] = $this->limit;
  95.         }
  96.  
  97.         return $options;
  98.     }
  99.  
  100.  
  101.  
  102.     function run() {
  103.         // to count
  104.         $this->docsNumber = $this->count($this->phrase);
  105.         // to count in other language
  106.         $this->docsNumberInOtherLang = $this->count($this->phraseInOtherLang);
  107.         // search docs
  108.         $this->result = $this->search($this->phrase);
  109.     }
  110.  
  111.     function runSuggest() {
  112.         $suggestPhrases = array();
  113.  
  114.         foreach ($this->fields as $name=>$value) {
  115.             $suggestPhrases[] = $this->addSuggestPhrase($name, $this->phrase);
  116.         }
  117.  
  118.         $suggest = $this->addSuggest( $suggestPhrases );
  119.         $result = $this->_search( $suggest, $this->getSearchOptions() );
  120.         $this->result = $result->getSuggests();
  121.     }
  122.  
  123.  
  124.  
  125.     protected function addSuggestPhrase($fieldName, $phrase) {
  126.         $suggestPhrase = new Elastica\Suggest\Phrase( $this->getSuggestName($fieldName), $fieldName );
  127.  
  128.         $suggestPhrase->setText($phrase)
  129.                        ->setAnalyzer( $this->getAnalyzer() )
  130.                        ->setHighlight( $this->highlightPrefix, $this->highlightPostfix )
  131.                        ->setStupidBackoffSmoothing( $this->suggestSmoothing )
  132.                        ->addCandidateGenerator( new Elastica\Suggest\CandidateGenerator\DirectGenerator($fieldName) );
  133.  
  134.         return $suggestPhrase;
  135.     }
  136.  
  137.     protected function addSuggest($suggestPhrases) {
  138.         $suggest = new Elastica\Suggest();
  139.  
  140.         foreach ($suggestPhrases as $suggestPhrase) {
  141.             $suggest->addSuggestion($suggestPhrase);
  142.         }
  143.  
  144.         return $suggest;
  145.     }
  146.  
  147.     protected function getSuggestName($fieldName) {
  148.         return 'suggest'.ucfirst($fieldName);
  149.     }
  150.  
  151.     protected function addQueryString($phrase) {
  152.         $query = new Elastica\Query\QueryString($phrase);
  153.         $query->setAnalyzer( $this->getAnalyzer() )
  154.               ->setFields( $this->getFieldsWithBoost() );
  155.  
  156.         return $query;
  157.     }
  158.  
  159.     protected function addMatch($name, $phrase) {
  160.         $field = $this->getField($name);
  161.  
  162.         $query = new Elastica\Query\Match();
  163.         $query->setFieldQuery($name, $phrase)
  164.                 ->setFieldType($name, 'phrase')
  165.                 ->setFieldBoost($name, $field['boost']);
  166.  
  167.         return $query;
  168.     }
  169.  
  170.     protected function addBool($queries) {
  171.         $boolQuery = new Elastica\Query\Bool();
  172.  
  173.         foreach ($queries as $query) {
  174.             $boolQuery->addShould($query);
  175.         }
  176.  
  177.         return $boolQuery;
  178.     }
  179.  
  180.     protected function addMatchPhrase($phrase) {
  181.         $queries = array();
  182.  
  183.         foreach ($this->fields as $name=>$value) {
  184.             $queries[] = $this->addMatch($name, $phrase);
  185.         }
  186.  
  187.         return $this->addBool($queries);
  188.     }
  189.  
  190.     protected function addQuery($subQuery, $highlight = true) {
  191.         $query = new Elastica\Query();
  192.         $query->setQuery($subQuery);
  193.  
  194.         if ($highlight) {
  195.             $query->setHighlight($this->highlightSettings);
  196.         }
  197.  
  198.         return $query;
  199.     }
  200.  
  201.     protected function _search($query, $options = null) {
  202.         return $this->type->search($query, $options);
  203.     }
  204.  
  205.     protected function _count($query) {
  206.         return $this->type->count($query);
  207.     }
  208.  
  209.     protected function getAnalyzer() {
  210.         return $this->strict ? self::SEARCH_STRICT_ANALYZER : self::SEARCH_ANALYZER;
  211.     }
  212.  
  213.     protected function getFieldWithBoost($name) {
  214.         $field = $this->getField($name);
  215.  
  216.         return $name.'^'.$field['boost'];
  217.     }
  218.  
  219.     protected function getFieldsWithBoost() {
  220.         $result = array();
  221.  
  222.         foreach ($this->fields as $name=>$value) {
  223.             $result[] = $this->getFieldWithBoost($name);
  224.         }
  225.  
  226.         return $result;
  227.     }
  228.  
  229.     protected function getField($name) {
  230.         return $this->fields[$name];
  231.     }
  232.  
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement