Skip to content

Instantly share code, notes, and snippets.

@MKorostoff
MKorostoff / Sort By Relevance.md
Last active Aug 29, 2015
Sorting By Relevance With Drupal Taxonomy
View Sort By Relevance.md

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

View gist:ca89cf962770fdff7926
<?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);
View gist:476d29bb9f6d2394cc5d
<?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++) {
View gist:7e84a04c2abe440a1cda
<?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++) {
View gist:2ea5e12af41a70f47043
<?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++) {
View gist:bf1704f8c307c3a35443

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 Apr 23, 2015
Load 5000 virtual blocks with 8 random tags each
View gist:4aa2da788dfbd23fca76
<?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 Apr 16, 2015
Scroll into view with behat
View gist:c94824a467ffa53f4fa9
/**
* @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 Aug 29, 2015
Benchmarking the performance difference between properties and fields on custom entities
View gist:1d93d28fac610199f865

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
View gist:e5a5412023831db41ae1
#!/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)