parisholley/wordpress-fantastic-elasticsearch — pull request #99 by schorsch (2014-09-18 09:04:02+00:00)
Title: mlt addon - MoreLikeThis related search

touch #82

Usage:

Setup indexing e.g in you-theme/functions.php

```php
// elasticsearch filter mapping
// add term_vector for all fields so we can use more-like-this to show related posts
add_filter('elasticsearch_indexer_map', 'add_elasticsearch_mapping', 1, 3 );
function add_elasticsearch_mapping($props, $field, $kind){
$mlt_fields = array('post_title', 'post_content', 'post_tag_name', 'category_name');
if(in_array($field, $mlt_fields)){
if (isset($props['fields'])){
// nested multi_field
$props['fields'][$field]['term_vector'] = 'yes';
if(isset($props['fields']['english'])){
//reset language analyzer to german
$props['fields']['english']['analyzer'] = 'german';
}
}else{
$props['term_vector'] = 'yes';
// $props['store'] = 'yes';
}
}
return $props;
}

```

Use the search e.g in a article detail view:
```php
while (have_posts()) : the_post(); ?>

..
array(
'search_size' => 10,
'mlt_fields' => 'post_title,post_content,category_name',
)
);
$my_query = $rel->do_search($post, $args);
if( $my_query && $my_query->have_posts() ) {
$wp_query = $my_query;
$list_title = 'Similar Articles';

// render results,
// in my case i delegate output to another partial template e.g
//include(locate_template('templates/list-images-book.php'));
}
?>

..

```