Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View MKorostoff's full-sized avatar

Matt Korostoff MKorostoff

View GitHub Profile
@MKorostoff
MKorostoff / Sort By Relevance.md
Last active August 29, 2015 14:19
Sorting By Relevance With Drupal Taxonomy

Sorting By Relevance With Drupal Taxonomy

Often it useful to be able to sort content by relevance. For instance, imagine you owned a site that published articles about the city of New York. Imagine this site contained a search page, with a form like this:

an image of views exposed filters

These fields hold taxonomy terms. Users can input the terms they want to search for, and the resulting list should be ranked by relevance. That is to say, an article tagged with "new york", "brooklyn", and "nightlife" should appear in the results, but it should rank lower than an article tagged with "new york", "brooklyn", "nightlife", and "community." In short, you want to know which content has been tagged the most with relevant search terms.

This can be done in an extremely performant way, with pure SQL. There is no need to involve a 3rd party search solution, such as Apache Solr. What follows is a set of benchmarks revealing the most performant way to generate this ranked

<?php
for ($i=1; $i <= 10; $i++) {
$start = microtime(true);
for ($ii=0; $ii < 5000; $ii++) {
$tids1 = $tids2 = $tids3 = array();
for ($iii=0; $iii < 3; $iii++) {
$tids1[] = rand(500, 1000000);
<?php
for ($i=1; $i <= 10; $i++) {
$start = microtime(true);
for ($ii=0; $ii < 5000; $ii++) {
//Pick 8 random tids to search for
$tids = array();
for ($iii=0; $iii < 8; $iii++) {
<?php
for ($i=1; $i <= 10; $i++) {
$start = microtime(true);
for ($ii=0; $ii < 50; $ii++) {
//Pick 8 random tids to search for
$tids = array();
for ($iii=0; $iii < 7; $iii++) {
<?php
for ($i=1; $i <= 10; $i++) {
$start = microtime(true);
for ($ii=0; $ii < 5000; $ii++) {
$tids1 = $tids2 = $tids3 = array();
for ($iii=0; $iii < 3; $iii++) {

Sorting By Relevance With Drupal Taxonomy

Often it useful to be able to sort content by relevance. For instance, imagine you owned a site that published articles about the city of New York. Imagine this site contained a search page, with a form like this:

an image of views exposed filters

These fields hold taxonomy terms. Users can input the terms they want to search for, and the resulting list should be ranked by relevance. That is to say, an article tagged with "new york", "brooklyn", and "nightlife" should appear in the results, but it should rank lower than an article tagged with "new york", "brooklyn", "nightlife", and "community." In short, you want to know which content has been tagged the most with relevant search terms.

This can be done in an extremely performant way, with pure SQL. There is no need to involve a 3rd party search solution, such as Apache Solr. What follows is a set of benchmarks revealing the most performant way to generate this ranked

@MKorostoff
MKorostoff / gist:4aa2da788dfbd23fca76
Created April 23, 2015 15:05
Load 5000 virtual blocks with 8 random tags each
<?php
for ($i=1; $i <= 10; $i++) {
$start = microtime(true);
for ($ii=0; $ii < 5000; $ii++) {
//Pick 8 random tids to search for
$tids = array();
for ($iii=0; $iii < 8; $iii++) {
@MKorostoff
MKorostoff / gist:c94824a467ffa53f4fa9
Created April 16, 2015 22:01
Scroll into view with behat
/**
* @When I scroll :elementId into view
*/
public function scrollIntoView($elementId) {
$function = <<<JS
(function(){
var elem = document.getElementById("$elementId");
elem.scrollIntoView(false);
})()
JS;
@MKorostoff
MKorostoff / gist:1d93d28fac610199f865
Last active August 29, 2015 14:19
Benchmarking the performance difference between properties and fields on custom entities

Test 1: entity_load 5000 custom entities, using entity cache, without memcache

Run with 18 fields 1 property

First run

  • 33948.94ms

Remaining runs

  • 2376.19ms
  • 2443.55ms
  • 2209.95ms
#!/bin/sh
#
# @author Matt Korostoff <mkorostoff@gmail.com>
#
# @internal
#
# @category
#
# @copyright Licensed under the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)