Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Elastic;
- use Elastica;
- class Search extends Base {
- public $offset;
- public $limit;
- public $strict;
- public $result;
- public $phrase;
- public $phraseInOtherLang;
- public $docsNumber;
- public $docsNumberInOtherLang;
- public $suggestSmoothing = 0.4;
- public $highlightPrefix = '*';
- public $highlightPostfix = '*';
- /** fields for search */
- public $fields = array(
- 'title'=>array(
- 'boost'=>100
- ),
- 'content'=>array(
- 'boost'=>1
- )
- );
- public $highlightSettings = array(
- 'fields' => array(
- 'content' => array(
- 'fragment_size' => 100,
- 'number_of_fragments' => 5,
- ),
- ),
- );
- function __construct()
- {
- parent::__construct();
- $this->highlightSettings['pre_tags'] = array($this->highlightPrefix);
- $this->highlightSettings['post_tags'] = array($this->highlightPostfix);
- }
- protected function setPhrase($phrase) {
- $phrase = str_replace('/', '-', $phrase);
- $this->phrase = \StringHelper::cleanSearchPhrase( $phrase );
- // если раскладка клавиатуры неправильная
- $this->phraseInOtherLang = \StringHelper::cleanSearchPhrase( \StringHelper::switchKeyboardLang( $phrase ) );
- }
- function initServer($host, $port, $indexName, $typeName)
- {
- parent::initServer($host, $port, $indexName, $typeName);
- $this->index = $this->client->getIndex($indexName);
- $this->type = $this->index->getType($typeName);
- }
- function initSettings($phrase, $strict, $offset, $limit = null) {
- $this->strict = $strict;
- $this->offset = $offset;
- $this->limit = $limit;
- $this->setPhrase($phrase);
- }
- protected function addActualQuery($phrase) {
- if ($this->strict) {
- return $this->addQuery( $this->addMatchPhrase($phrase) );
- }
- else {
- return $this->addQuery( $this->addQueryString($phrase) );
- }
- }
- protected function count($phrase) {
- return $this->_count( $this->addActualQuery($phrase) );
- }
- protected function search($phrase) {
- return $this->_search( $this->addActualQuery($phrase), $this->getSearchOptions() );
- }
- protected function getSearchOptions() {
- $options = array(
- 'from'=>$this->offset
- );
- if ( $this->limit !== null ) {
- $options['size'] = $this->limit;
- }
- return $options;
- }
- function run() {
- // to count
- $this->docsNumber = $this->count($this->phrase);
- // to count in other language
- $this->docsNumberInOtherLang = $this->count($this->phraseInOtherLang);
- // search docs
- $this->result = $this->search($this->phrase);
- }
- function runSuggest() {
- $suggestPhrases = array();
- foreach ($this->fields as $name=>$value) {
- $suggestPhrases[] = $this->addSuggestPhrase($name, $this->phrase);
- }
- $suggest = $this->addSuggest( $suggestPhrases );
- $result = $this->_search( $suggest, $this->getSearchOptions() );
- $this->result = $result->getSuggests();
- }
- protected function addSuggestPhrase($fieldName, $phrase) {
- $suggestPhrase = new Elastica\Suggest\Phrase( $this->getSuggestName($fieldName), $fieldName );
- $suggestPhrase->setText($phrase)
- ->setAnalyzer( $this->getAnalyzer() )
- ->setHighlight( $this->highlightPrefix, $this->highlightPostfix )
- ->setStupidBackoffSmoothing( $this->suggestSmoothing )
- ->addCandidateGenerator( new Elastica\Suggest\CandidateGenerator\DirectGenerator($fieldName) );
- return $suggestPhrase;
- }
- protected function addSuggest($suggestPhrases) {
- $suggest = new Elastica\Suggest();
- foreach ($suggestPhrases as $suggestPhrase) {
- $suggest->addSuggestion($suggestPhrase);
- }
- return $suggest;
- }
- protected function getSuggestName($fieldName) {
- return 'suggest'.ucfirst($fieldName);
- }
- protected function addQueryString($phrase) {
- $query = new Elastica\Query\QueryString($phrase);
- $query->setAnalyzer( $this->getAnalyzer() )
- ->setFields( $this->getFieldsWithBoost() );
- return $query;
- }
- protected function addMatch($name, $phrase) {
- $field = $this->getField($name);
- $query = new Elastica\Query\Match();
- $query->setFieldQuery($name, $phrase)
- ->setFieldType($name, 'phrase')
- ->setFieldBoost($name, $field['boost']);
- return $query;
- }
- protected function addBool($queries) {
- $boolQuery = new Elastica\Query\Bool();
- foreach ($queries as $query) {
- $boolQuery->addShould($query);
- }
- return $boolQuery;
- }
- protected function addMatchPhrase($phrase) {
- $queries = array();
- foreach ($this->fields as $name=>$value) {
- $queries[] = $this->addMatch($name, $phrase);
- }
- return $this->addBool($queries);
- }
- protected function addQuery($subQuery, $highlight = true) {
- $query = new Elastica\Query();
- $query->setQuery($subQuery);
- if ($highlight) {
- $query->setHighlight($this->highlightSettings);
- }
- return $query;
- }
- protected function _search($query, $options = null) {
- return $this->type->search($query, $options);
- }
- protected function _count($query) {
- return $this->type->count($query);
- }
- protected function getAnalyzer() {
- return $this->strict ? self::SEARCH_STRICT_ANALYZER : self::SEARCH_ANALYZER;
- }
- protected function getFieldWithBoost($name) {
- $field = $this->getField($name);
- return $name.'^'.$field['boost'];
- }
- protected function getFieldsWithBoost() {
- $result = array();
- foreach ($this->fields as $name=>$value) {
- $result[] = $this->getFieldWithBoost($name);
- }
- return $result;
- }
- protected function getField($name) {
- return $this->fields[$name];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement