/** * Query the required server * passes all parameters to the appropriate function based on the server name * This allows for extensible server/core based query functions. * TODO allow for similar theme/output function */ function query($qry, $offset, $count, $fq, $sortby, $order, $server = 'master') { //NOTICE: does this needs to be cached to stop the db being hit to grab the options everytime search is being done. $plugin_s4wp_settings = solr_options(); $solr = get_solr(); return $this->master_query($solr, $qry, $offset, $count, $fq, $sortby, $order, $plugin_s4wp_settings); }
function load_all_posts($prev, $post_type = 'post') { global $wpdb, $current_blog, $current_site; $documents = array(); $cnt = 0; $batchsize = 500; $last = ""; $found = FALSE; $end = FALSE; $percent = 0; //multisite logic is decided s4wp_get_option $plugin_s4wp_settings = solr_options(); if (isset($blog)) { $blog_id = $blog->blog_id; } if (is_multisite()) { // there is potential for this to run for an extended period of time, depending on the # of blgos syslog(LOG_ERR, "starting batch import, setting max execution time to unlimited"); ini_set('memory_limit', '1024M'); set_time_limit(0); // get a list of blog ids $bloglist = $wpdb->get_col("SELECT * FROM {$wpdb->base_prefix}blogs WHERE spam = 0 AND deleted = 0", 0); syslog(LOG_INFO, "pushing posts from " . count($bloglist) . " blogs into Solr"); foreach ($bloglist as $bloginfo) { // for each blog we need to import we get their id // and tell wordpress to switch to that blog $blog_id = trim($bloginfo); syslog(LOG_INFO, "switching to blogid {$blog_id}"); // attempt to save some memory by flushing wordpress's cache wp_cache_flush(); // everything just works better if we tell wordpress // to switch to the blog we're using, this is a multi-site // specific function switch_to_blog($blog_id); // now we actually gather the blog posts $args = array('post_type' => $post_type, 'post_status' => 'publish', 'fields' => 'ids'); $query = new WP_Query($args); $postids = $query->posts; $postcount = count($postids); syslog(LOG_INFO, "building {$postcount} documents for " . substr(get_bloginfo('wpurl'), 7)); for ($idx = 0; $idx < $postcount; $idx++) { $postid = $postids[$idx]; $last = $postid; $percent = floatval($idx) / floatval($postcount) * 100; if ($prev && !$found) { if ($postid === $prev) { $found = TRUE; } continue; } if ($idx === $postcount - 1) { $end = TRUE; } // using wpurl is better because it will return the proper // URL for the blog whether it is a subdomain install or otherwise $solr = get_solr(); $update = $solr->createUpdate(); $documents[] = $this->build_document($update->createDocument(), get_blog_post($blog_id, $postid), substr(get_bloginfo('wpurl'), 7), $current_site->path); $cnt++; if ($cnt == $batchsize) { $this->post($documents, true, false); $this->post(false, true, false); wp_cache_flush(); $cnt = 0; $documents = array(); } } // post the documents to Solr // and reset the batch counters $this->post($documents, true, false); $this->post(false, true, false); $cnt = 0; $documents = array(); syslog(LOG_INFO, "finished building {$postcount} documents for " . substr(get_bloginfo('wpurl'), 7)); wp_cache_flush(); } // done importing so lets switch back to the proper blog id restore_current_blog(); } else { $args = array('post_type' => $post_type, 'post_status' => 'publish', 'fields' => 'ids'); $query = new WP_Query($args); $posts = $query->posts; $postcount = count($posts); if (0 == $postcount) { $end = true; printf("{\"type\": \"" . $post_type . "\", \"last\": \"%s\", \"end\": true, \"percent\": \"%.2f\"}", $last, 100); die; } for ($idx = 0; $idx < $postcount; $idx++) { $postid = $posts[$idx]; $last = $postid; $percent = floatval($idx) / floatval($postcount) * 100; if ($prev && !$found) { if ($postid === $prev) { $found = TRUE; } continue; } if ($idx === $postcount - 1) { $end = TRUE; } $solr = get_solr(); $update = $solr->createUpdate(); $documents[] = $this->build_document($update->createDocument(), get_post($postid)); $cnt++; if ($cnt == $batchsize) { $this->post($documents, true, FALSE); $cnt = 0; $documents = array(); wp_cache_flush(); break; } } } if ($documents) { $this->post($documents, true, FALSE); } if ($end) { printf("{\"type\": \"" . $post_type . "\", \"last\": \"%s\", \"end\": true, \"percent\": \"%.2f\"}", $last, 100); } else { printf("{\"type\": \"" . $post_type . "\", \"last\": \"%s\", \"end\": false, \"percent\": \"%.2f\"}", $last, $percent); } }
function autocomplete($q, $limit) { $solr = get_solr(); $response = NULL; if (!$solr) { return; } $query = $solr->createTerms(); $query->setFields('spell'); $query->setPrefix($q); $query->setLowerbound($q); $query->setLowerboundInclude(false); $query->setLimit($limit); $response = $solr->terms($query); if (!$response->getResponse()->getStatusCode() == 200) { return; } $terms = $response->getResults(); foreach ($terms['spell'] as $term => $count) { printf("%s\n", $term); } }