BuildExcerpts() public method

an array of snippets on success
public BuildExcerpts ( $docs, $index, $words, $opts = [] )
 /**
  * {@inheritdoc}
  */
 public function excerpt($query, $fields, \record_adapter $record, SearchEngineOptions $options = null)
 {
     if (null === $options) {
         $options = new SearchEngineOptions();
     }
     $this->applyOptions($options);
     $index = '';
     // in this case search is done on metas
     if ($options->getFields() || $options->getBusinessFieldsOn()) {
         if ($options->isStemmed() && $options->getLocale()) {
             $index = 'metadatas' . $this->CRCdatabox($record->get_databox()) . '_stemmed_' . $options->getLocale();
         } else {
             $index = 'metadatas' . $this->CRCdatabox($record->get_databox());
         }
     } else {
         if ($options->isStemmed() && $options->getLocale()) {
             $index = 'documents' . $this->CRCdatabox($record->get_databox()) . '_stemmed_' . $options->getLocale();
         } else {
             $index = 'documents' . $this->CRCdatabox($record->get_databox());
         }
     }
     $opts = ['before_match' => "[[em]]", 'after_match' => "[[/em]]"];
     $fields_to_send = [];
     foreach ($fields as $k => $f) {
         $fields_to_send[$k] = $f['value'];
     }
     return $this->sphinx->BuildExcerpts($fields_to_send, $index, $query, $opts);
 }
Ejemplo n.º 2
0
 /** @Route("/autocomplete/{term}", name="autocomplete", options={"expose" = true}) */
 public function autocomplete($term)
 {
     if (mb_strlen($term, 'utf-8') > 2) {
         $term = $this->get('evrika.lingua_stem')->stem_string($term);
     }
     $words = explode(' ', $term);
     $query = implode('*|', $words) . '*';
     $s = new \SphinxClient();
     $s->SetServer("localhost", 9312);
     // должна быть запущена служба по порту: searchd --config /c/sphinx/shinx.cnf
     $s->SetMatchMode(SPH_MATCH_EXTENDED2);
     // SPH_MATCH_ALL will match all words in the search term
     $s->SetArrayResult(true);
     $s->SetLimits(0, 15);
     # получаем результаты поиска
     $s->SetFieldWeights(array('title' => 10, 'shortText' => 3, 'body' => 1));
     $data = $s->Query("@(title,shortText,body) {$query}", 'publication');
     $items = isset($data['matches']) ? $data['matches'] : array();
     # добавляем поле с подсветкой слов
     foreach ($items as &$item) {
         $h = $s->BuildExcerpts(array($item['attrs']['title']), 'publication', $query, array('around' => 100));
         $item['attrs']['h'] = $h[0];
     }
     return new JsonResponse($items);
 }
Ejemplo n.º 3
0
<?php

require "spec/fixtures/sphinxapi.php";
$cl = new SphinxClient();
$cl->BuildExcerpts(array('10', '20'), 'index', 'word1 word2');
Ejemplo n.º 4
0
 public function createExcerts(array $docs, $index, $words, array $opts = array())
 {
     return $this->sphinxClient->BuildExcerpts($docs, $index, $words, $opts);
 }
Ejemplo n.º 5
0
<?php

require "sphinxapi.php";
$cl = new SphinxClient();
$cl->BuildExcerpts(array('10', '20'), 'index', 'word1 word2', array('before_match' => 'before', 'after_match' => 'after', 'chunk_separator' => 'separator', 'limit' => 10));
Ejemplo n.º 6
0
    while ($row = $db->fetch_array($query)) {
        $row['lastpost'] = gmdate($dateformat . ' ' . $timeformat, $row['lastpost'] + $timeoffset * 3600);
        $key = $sp_res_order[$row['pid']];
        $postlist[$key] = $row;
        if (trim($row['ptitle']) != '') {
            $titles[$key] = $row['ptitle'];
        } else {
            $titles[$key] = 'RE:' . $row['ttitle'];
        }
        $contents[$key] = $row['content'];
    }
    //排序
    ksort($titles);
    ksort($postlist);
    ksort($contents);
    $sp_titles = $cl->BuildExcerpts($titles, $sp_hightlight_index, $sp_keyword, $sp_build_opts);
    $sp_contents = $cl->BuildExcerpts($contents, $sp_hightlight_index, $sp_keyword, $sp_build_opts);
    for ($i = 0, $l = count($contents); $i < $l; $i++) {
        $postlist[$i]['content'] = $sp_contents[$i];
        $postlist[$i]['title'] = $sp_titles[$i];
    }
    include template('search_sphinx');
} else {
    $threadlist = $titles = array();
    $query = $db->query("SELECT * FROM {$tablepre}threads WHERE tid IN ({$sp_find_ids})");
    // AND displayorder>='0'
    while ($thread = $db->fetch_array($query)) {
        $threadlist[$sp_res_order[$thread['tid']]] = procthread($thread);
        $titles[$sp_res_order[$thread['tid']]] = $thread['subject'];
    }
    $sp_titles = $cl->BuildExcerpts($titles, $sp_hightlight_index, $sp_keyword, $sp_build_opts);
Ejemplo n.º 7
0
<?php

require "spec/fixtures/sphinxapi.php";
$cl = new SphinxClient();
$cl->BuildExcerpts(array('10', '20'), 'index', 'word1 word2', array('exact_phrase' => true, 'single_passage' => true, 'use_boundaries' => true, 'weight_order' => true, 'query_mode' => true));
Ejemplo n.º 8
0
<?php

//
// $Id$
//
require "sphinxapi.php";
$docs = array("this is my test text to be highlighted, and for the sake of the testing we need to pump its length somewhat", "another test text to be highlighted, below limit", "test number three, without phrase match", "final test, not only without phrase match, but also above limit and with swapped phrase text test as well");
$words = "test text";
$index = "test1";
$opts = array("before_match" => "<b>", "after_match" => "</b>", "chunk_separator" => " ... ", "limit" => 60, "around" => 3);
foreach (array(0, 1) as $exact) {
    $opts["exact_phrase"] = $exact;
    print "exact_phrase={$exact}\n";
    $cl = new SphinxClient();
    $res = $cl->BuildExcerpts($docs, $index, $words, $opts);
    if (!$res) {
        die("ERROR: " . $cl->GetLastError() . ".\n");
    } else {
        $n = 0;
        foreach ($res as $entry) {
            $n++;
            print "n={$n}, res={$entry}\n";
        }
        print "\n";
    }
}
//
// $Id$
//
Ejemplo n.º 9
0
 /**
  * Получаем сниппеты(превью найденых элементов)
  *
  * @param string $sText	Текст
  * @param string $sIndex	Название индекса
  * @param string $sTerms	Поисковый запрос
  * @param string $before_match	Добавляемый текст перед ключом
  * @param string $after_match	Добавляемый текст после ключа
  * @return array
  */
 public function GetSnippet($sText, $sIndex, $sTerms, $before_match, $after_match)
 {
     $aReturn = $this->oSphinx->BuildExcerpts(array($sText), Config::Get('module.search.entity_prefix') . $sIndex . 'Index', $sTerms, array('before_match' => $before_match, 'after_match' => $after_match));
     return $aReturn[0];
 }
Ejemplo n.º 10
0
<?php

require "spec/fixtures/sphinxapi.php";
$cl = new SphinxClient();
$cl->BuildExcerpts(array('what the world', 'London is the capital of Great Britain'), 'test1', 'the');
Ejemplo n.º 11
0
 private function doSearch($q, $page)
 {
     global $wgOut;
     $mode = SPH_MATCH_ALL;
     $index = 'suggested_titles';
     $page_size = 20;
     $limit = 1000;
     $ranker = SPH_RANK_PROXIMITY_BM25;
     $host = 'localhost';
     $port = 9312;
     $cl = new SphinxClient();
     $cl->SetServer($host, $port);
     //$cl->SetConnectTimeout(1);
     $cl->SetSortMode(SPH_SORT_RELEVANCE);
     $cl->SetArrayResult(true);
     $cl->SetWeights(array('wst_title' => 5, 'wst_text' => 2));
     $cl->SetMatchMode($mode);
     $cl->SetRankingMode($ranker);
     $cl->SetLimits(($page - 1) * $page_size, $page_size, $limit);
     // don't search w/ leading "how to" if user added it
     $q_prime = preg_replace('@^\\s*how\\s+to\\s+@i', '', $q);
     $res = $cl->Query($q_prime, $index);
     $error = $res === false ? $cl->GetLastError() : '';
     $warning = $cl->GetLastWarning();
     /*$spelling = $this->getSpellingInfo($q);
     		if ($spelling) {
     			$res['spelling'] = $spelling;
     		} else {
     			$res['spelling'] = '';
     		}*/
     if (count($res['matches']) > 0) {
         $titles = $this->getInfo($res['matches']);
         $keys = array_keys($titles);
         $excerpts = $cl->BuildExcerpts($titles, 'suggested_titles', $q);
         foreach ($excerpts as $i => $excerpt) {
             $excerpts[$keys[$i]] = $excerpt;
             unset($excerpts[$i]);
         }
         foreach ($res['matches'] as $i => &$docinfo) {
             $id = $docinfo['id'];
             $docinfo['attrs']['excerpt'] = $excerpts[$id];
         }
     } else {
         $error = wfMsg('search-keywords-not-found', $q);
     }
     // construct paging bar
     $total = (int) ceil(1.0 * $res['total_found'] / $page_size);
     $paging = array();
     if ($page > 1) {
         $paging[] = 'prev';
     }
     if ($page > 1) {
         $paging[] = 1;
     }
     if ($page >= 5) {
         $paging[] = '...';
     }
     if ($page >= 4) {
         $paging[] = $page - 2;
     }
     if ($page >= 3) {
         $paging[] = $page - 1;
     }
     $paging[] = $page;
     if ($page < $total) {
         $paging[] = $page + 1;
     }
     if ($page + 1 < $total) {
         $paging[] = $page + 2;
     }
     if ($page + 2 < $total) {
         $paging[] = '...';
     }
     if ($page < $total) {
         $paging[] = 'next';
     }
     $vars = array('results' => $res, 'q' => $q, 'error' => $error, 'warning' => $warning, 'page' => $page, 'page_size' => $page_size, 'paging' => $paging);
     return $vars;
 }
Ejemplo n.º 12
0
function sphinx_search_action($arrSearch)
{
    global $PHORUM;
    // No pecl class, try php version
    if (!class_exists('SphinxClient')) {
        // loads from php include_path
        require_once 'sphinxapi.php';
    }
    // these are the index-names set in sphinx.conf - one for searching messages, the other for searching by authors only
    // both contain an additional index for the deltas - changes done after the last full reindex
    $index_name_msg = 'phorum5_msg_d phorum5_msg';
    $index_name_author = 'phorum5_author phorum5_author_d';
    // excerpts_index is just one index as that function only accepts one, it used for determining charsets / mapping tables, nothing more
    $excerpts_index = 'phorum5_msg';
    $index = $index_name_msg;
    if ($arrSearch['match_type'] == 'ALL') {
        $match_mode = SPH_MATCH_ALL;
    } elseif ($arrSearch['match_type'] == 'ANY') {
        $match_mode = SPH_MATCH_ANY;
    } elseif ($arrSearch['match_type'] == 'PHRASE') {
        $match_mode = SPH_MATCH_PHRASE;
    } elseif ($arrSearch['match_type'] == 'AUTHOR') {
        $match_mode = SPH_MATCH_PHRASE;
        $index = $index_name_author;
    } else {
        // Return search control to Phorum in case the search type isn't handled by the module.
        return $arrSearch;
    }
    if (empty($arrSearch['search']) && !empty($arrSearch['author'])) {
        $arrSearch['search'] = $arrSearch['author'];
        $index = $index_name_author;
    }
    $sphinx = new SphinxClient();
    $sphinx->SetServer($PHORUM['mod_sphinx_search']['hostname'], $PHORUM['mod_sphinx_search']['port']);
    $sphinx->SetMatchMode($match_mode);
    // set the limits for paging
    $sphinx->SetLimits($arrSearch['offset'], $arrSearch['length']);
    // set the timeframe to search
    if ($arrSearch['match_dates'] > 0) {
        $min_ts = time() - 86400 * $arrSearch['match_dates'];
        $max_ts = time();
        $sphinx->SetFilterRange('datestamp', $min_ts, $max_ts);
    }
    // Check what forums the active Phorum user can read.
    $allowed_forums = phorum_api_user_check_access(PHORUM_USER_ALLOW_READ, PHORUM_ACCESS_LIST);
    // If the user is not allowed to search any forum or the current
    // active forum, then return the emtpy search results array.
    if (empty($allowed_forums) || $PHORUM['forum_id'] > 0 && !in_array($PHORUM['forum_id'], $allowed_forums)) {
        $arrSearch['results'] = array();
        $arrSearch['totals'] = 0;
        $arrSearch['continue'] = 0;
        $arrSearch['raw_body'] = 1;
        return $arrSearch;
    }
    // Prepare forum_id restriction.
    $search_forums = array();
    foreach (explode(',', $arrSearch['match_forum']) as $forum_id) {
        if ($forum_id == 'ALL') {
            $search_forums = $allowed_forums;
            break;
        }
        if (isset($allowed_forums[$forum_id])) {
            $search_forums[] = $forum_id;
        }
    }
    $sphinx->SetFilter('forum_id', $search_forums);
    // set the sort-mode
    $sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'datestamp');
    // do the actual query
    $results = $sphinx->Query($arrSearch['search'], $index);
    $res = $sphinx->GetLastWarning();
    if ($res) {
        error_log("sphinx_search.php: WARNING: {$res}");
    }
    $res = $sphinx->GetLastError();
    if ($res) {
        error_log("sphinx_search.php: ERROR: {$res}");
    }
    // if no messages were found, then return empty handed.
    if (!isset($results['matches'])) {
        $arrSearch['results'] = array();
        $arrSearch['totals'] = 0;
        $arrSearch['continue'] = 0;
        $arrSearch['raw_body'] = 1;
        return $arrSearch;
    }
    $search_msg_ids = $results['matches'];
    // get the messages we found
    $found_messages = phorum_db_get_message(array_keys($search_msg_ids), 'message_id', true);
    // sort them in reverse order of the message_id to automagically sort them by date desc this way
    krsort($found_messages);
    reset($found_messages);
    // prepare the array for building highlighted excerpts
    $docs = array();
    foreach ($found_messages as $id => $data) {
        // remove hidden text in the output - only added by the hidden_msg module
        $data['body'] = preg_replace("/(\\[hide=([\\#a-z0-9]+?)\\](.+?)\\[\\/hide\\])/is", '', $data['body']);
        $docs[] = htmlspecialchars(phorum_strip_body($data['body']));
    }
    $words = '';
    if (!empty($results['words'])) {
        $words = implode(' ', array_keys($results['words']));
    }
    $opts = array('chunk_separator' => ' [...] ');
    // build highlighted excerpts
    $highlighted = $sphinx->BuildExcerpts($docs, $excerpts_index, $words, $opts);
    $res = $sphinx->GetLastWarning();
    if ($res) {
        error_log("sphinx_search.php: WARNING: {$res}");
    }
    $res = $sphinx->GetLastError();
    if ($res) {
        error_log("sphinx_search.php: ERROR: {$res}");
    }
    $cnt = 0;
    foreach ($found_messages as $id => $content) {
        $found_messages[$id]['short_body'] = $highlighted[$cnt];
        $cnt++;
    }
    $arrSearch['results'] = $found_messages;
    // we need the total results
    $arrSearch['totals'] = $results['total_found'];
    if ($arrSearch['totals'] > 1000) {
        $arrSearch['totals'] = 1000;
    }
    // don't run the default search
    $arrSearch['continue'] = 0;
    // tell it to leave the body alone
    $arrSearch['raw_body'] = 1;
    return $arrSearch;
}
Ejemplo n.º 13
0
             preg_match('/\\d+/', $k, $key);
             $key = $key[0];
             $words[$key]['docs'] = $v;
         }
     }
     $suggest = MakePhaseSuggestion($words, $term, $ln_sph);
     if ($suggest !== FALSE) {
         $app->response->redirect($app->urlFor('autocomplete', array('term' => $suggest)));
         $app->stop();
     }
     $result[] = ['label' => 'nothing found', 'url' => '#', 'type' => '', 'img' => '/skin/default/img/icon-label.png'];
 } else {
     $filterTypeMapping = array_flip($filterTypeMapping);
     $cl = new SphinxClient();
     foreach ($rows as $row) {
         $excerped = $cl->BuildExcerpts([$row['phrase']], 'slimpdautocomplete', $term);
         $filterType = $filterTypeMapping[$row['type']];
         $entry = ['label' => $excerped[0], 'url' => $filterType === 'track' ? '/searchall/page/1/sort/relevance/desc?q=' . $row['phrase'] : '/library/' . $filterType . '/' . $row['itemid'], 'type' => $filterType, 'typelabel' => $app->ll->str($filterType), 'itemid' => $row['itemid']];
         switch ($filterType) {
             case 'artist':
             case 'label':
                 $entry['img'] = '/skin/default/img/icon-' . $filterType . '.png';
                 break;
             case 'album':
             case 'track':
                 $entry['img'] = '/image-50/' . $filterType . '/' . $row['itemid'];
                 break;
         }
         $result[] = $entry;
     }
 }
Ejemplo n.º 14
0
 /** @Route("/drugs/autocomplete/{type}/{term}", name="drug_autocomplete", options={"expose":true}) */
 public function autocompleteAction($type, $term)
 {
     $words = explode(' ', $term);
     $query = implode('*|', $words) . '*';
     $s = new \SphinxClient();
     $s->SetServer("localhost", 9312);
     // должна быть запущена служба по порту: searchd --config /c/sphinx/shinx.cnf
     $s->SetMatchMode(SPH_MATCH_EXTENDED2);
     // SPH_MATCH_ALL will match all words in the search term
     $s->SetArrayResult(true);
     # получаем результаты поиска
     $data = $type == 'all' ? $s->Query("@(title) {$query} & @type=product|atc|company|molecule", 'autocomplete') : $s->Query("@(title) {$query} & @type={$type}", 'autocomplete');
     $items = isset($data['matches']) ? $data['matches'] : array();
     foreach ($items as &$item) {
         $h = $s->BuildExcerpts(array($item['attrs']['title']), 'autocomplete', $query, array('around' => 100));
         $item['attrs']['h'] = $h[0];
     }
     return new JsonResponse($items);
 }