function search($dbpath, $querystring, $offset = 0, $pagesize = 10) { // offset - defines starting point within result set // pagesize - defines number of records to retrieve // Open the database we're going to search. $db = new XapianDatabase($dbpath); ### Start of example code. // Set up a QueryParser with a stemmer and suitable prefixes $queryparser = new XapianQueryParser(); $queryparser->set_stemmer(new XapianStem("en")); $queryparser->set_stemming_strategy(XapianQueryParser::STEM_SOME); $queryparser->add_prefix("title", "S"); $queryparser->add_prefix("description", "XD"); $queryparser->add_boolean_prefix("material", "XM"); // And parse the query $query = $queryparser->parse_query($querystring); ### End of example code. // Use an Enquire object on the database to run the query $enquire = new XapianEnquire($db); $enquire->set_query($query); // Set up a spy to inspect the MAKER value at slot 1 $spy = new XapianValueCountMatchSpy(1); $enquire->add_matchspy($spy); // Retrieve the matches and compute start and end points $matches = $enquire->get_mset($offset, $pagesize); $start = $matches->begin(); $end = $matches->end(); // Use an array to record the DocIds of each match $docids = array(); while (!$start->equals($end)) { // retrieve the document and its data $doc = $start->get_document(); $fields = json_decode($doc->get_data()); $position = $start->get_rank() + 1; // record the docid $docid = $start->get_docid(); $docids[] = $docid; // display the results printf("%d: #%03d %s\n", $position, $docid, $fields->TITLE); // increment MSet iterator and our counter $start->next(); } // Parse and display the spy values $spy_start = $spy->values_begin(); $spy_end = $spy->values_end(); while (!$spy_start->equals($spy_end)) { print sprintf("Facet: %s; count: %d\n", $spy_start->get_term(), $spy_start->get_termfreq()); $spy_start->next(); } // Finally, make sure we log the query and displayed results log_info(sprintf("xapian.search:'%s'[%d:%d] = %s", $querystring, $offset, $offset + $pagesize, implode(" ", $docids))); }
/** * @brief 搜索用户帖子 * @param userid $Uid */ public static function searchByUid($Uid, $page = 1, $pagesize = 30, $max = 0) { $max = $max > 0 ? $max : self::$_MAX; $page = empty($page) ? 1 : $page; if (!self::_connect() || empty($Uid)) { return false; } $queryparser = new XapianQueryParser(); $queryparser->add_prefix('userId', "UID"); $queryparser->set_database(self::$_INSTANCE); $query = $queryparser->parse_query("userId:{$Uid}"); $enquire = new XapianEnquire(self::$_INSTANCE); $enquire->set_sort_by_value(0); //排序时间 $enquire->set_query($query); $matches = $enquire->get_mset(0, $max); //最多显示300条 $start = $matches->begin(); $end = $matches->end(); $count = $matches->size(); $index = 0; $re = array(); while (!$start->equals($end)) { $data = array(); if ($index < $page * $pagesize && $index >= ($page - 1) * $pagesize) { $doc = $start->get_document(); $result = json_decode($doc->get_data(), true); $data['puid'] = $result['puid']; $data['author'] = $result['author']; $data['title'] = $result['title']; $data['majory'] = $result['majory']; $data['majory_id'] = $result['majory_id']; $data['time_step'] = $result['time_step']; if (isset($result['diary_book'])) { $data['diary_book'] = $result['diary_book']; } $re[] = $data; } elseif ($index >= $page * $pagesize) { break; } $start->next(); $index++; } return array($re, $count); }
function run_count() { if (!defined('XAPIANDB') || !XAPIANDB) { return null; } $start = getmicrotime(); global $xapiandb; if (!$xapiandb) { $xapiandb = new XapianDatabase(XAPIANDB); } if (!$this->enquire) { $this->enquire = new XapianEnquire($xapiandb); } $queryparser = new XapianQueryParser(); $queryparser->set_stemming_strategy(QueryParser_STEM_NONE); $queryparser->set_default_op(Query_OP_AND); $queryparser->add_prefix("speaker", "speaker:"); $queryparser->add_prefix("major", "major:"); $queryparser->add_prefix('date', 'date:'); $queryparser->add_prefix('batch', 'batch:'); twfy_debug("SEARCH", "query remade -- " . $this->query_remade()); // We rebuild (with query_remade) our query and feed that text string to // the query parser. This is because the error handling in the query parser // is a bit knackered, and we want to be sure our highlighting etc. exactly // matches. XXX don't need to do this for more recent Xapians $query = $queryparser->parse_query($this->query_remade()); twfy_debug("SEARCH", "queryparser description -- " . $query->get_description()); $this->enquire->set_query($query); // Set collapsing and sorting global $PAGE; $collapsed = false; foreach ($this->prefixed as $items) { if ($items[0] == 'groupby') { $collapsed = true; if ($items[1] == 'day') { $this->enquire->set_collapse_key(2); } else { if ($items[1] == 'debate') { $this->enquire->set_collapse_key(3); } else { if ($items[1] == 'speech') { } else { $PAGE->error_message("Unknown group by '{$items['1']}' ignored"); } } } } elseif ($items[0] == 'bias') { list($weight, $halflife) = explode(":", $items[1]); $this->enquire->set_bias($weight, intval($halflife)); } elseif ($items[0] == 'speaker') { # Don't do any collapsing if we're searching for a person's speeches $collapsed = true; } } // default to grouping by subdebate, i.e. by page if (!$collapsed) { $this->enquire->set_collapse_key(7); } $matches = $this->enquire->get_mset(0, 500); // Take either: 1) the estimate which is sometimes too large or 2) the // size which is sometimes too low (it is limited to the 500 in the line // above). We get the exact mset we need later, according to which page // we are on. if ($matches->size() < 500) { $count = $matches->size(); } else { $count = $matches->get_matches_estimated(); } $duration = getmicrotime() - $start; twfy_debug("SEARCH", "Search count took {$duration} seconds."); return $count; }
function search($dbpath, $querystring, $materials, $offset = 0, $pagesize = 10) { // offset - defines starting point within result set // pagesize - defines number of records to retrieve // Open the database we're going to search. $db = new XapianDatabase($dbpath); ### Start of example code. // Set up a QueryParser with a stemmer and suitable prefixes $queryparser = new XapianQueryParser(); $queryparser->set_stemmer(new XapianStem("english")); $queryparser->set_stem_strategy(XapianQueryParser::STEM_SOME); $queryparser->add_prefix("title", "S"); $queryparser->add_prefix("description", "XD"); // And parse the query $query = $queryparser->parse_query($querystring); if (empty($materials) === false) { // Filter the results to ones which contain at least one of the // materials. $material_queries = array(); // Build a query for each material value foreach ($materials as $material) { $material = str_replace("material:", "", $material); $material_queries[] = new XapianQuery('XM' . strtolower($material)); } // Combine these queries with an OR operator $material_query = new XapianQuery(XapianQuery::OP_AND, $material_queries); // Use the material query to filter the main query $query = new XapianQuery(XapianQuery::OP_FILTER, $query, $material_query); } ### End of example code. // Use an Enquire object on the database to run the query $enquire = new XapianEnquire($db); $enquire->set_query($query); // Set up a spy to inspect the MAKER value at slot 1 $spy = new XapianValueCountMatchSpy(1); $enquire->add_matchspy($spy); // Retrieve the matches and compute start and end points $matches = $enquire->get_mset($offset, $pagesize); $start = $matches->begin(); $end = $matches->end(); $index = 0; // Use an array to record the DocIds of each match $docids = array(); while (!$start->equals($end)) { // retrieve the document and its data $doc = $start->get_document(); $fields = json_decode($doc->get_data()); $position = $offset + $index + 1; // record the docid $docid = $start->get_docid(); $docids[] = $docid; // display the results print sprintf("%d: #%03d %s\n", $position, $docid, $fields->TITLE); // increment MSet iterator and our counter $start->next(); $index++; } // Parse and display the spy values $spy_start = $spy->values_begin(); $spy_end = $spy->values_end(); while (!$spy_start->equals($spy_end)) { print sprintf("Facet: %s; count: %d\n", $spy_start->get_term(), $spy_start->get_termfreq()); $spy_start->next(); } // Finally, make sure we log the query and displayed results log_info(sprintf("xapian.search:'%s'[%d:%d] = %s", $querystring, $offset, $offset + $pagesize, implode(" ", $docids))); }