Created
December 5, 2018 07:03
-
-
Save Anduin-/67409dd9d2aae2583f04fa13891b3529 to your computer and use it in GitHub Desktop.
elastic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: tim | |
* Date: 2/5/18 | |
* Time: 10:56 PM | |
*/ | |
add_filter('ep_post_sync_args', 'add_content_to_elastic', 10, 2); | |
function add_content_to_elastic($post_args, $post_id) | |
{ | |
$old_prepared_meta = $post_args['post_meta']; | |
$additional_prepared_meta = []; | |
// code to get up additional meta | |
// set up data like this: | |
// $additional_prepared_meta[ $key ] = array( $value ); | |
// note that the value is enclosed into an array | |
// you can add one or multiple new elements by key => value association to the array | |
$lead = get_field("lead_p", $post_id); | |
$subtitle = get_field("subtitle", $post_id); | |
$blocks = get_field("content", $post_id); | |
$text = ""; | |
foreach ($blocks as $block) { | |
if (isset($block['text'])) { | |
$text .= $block['text']; | |
} | |
} | |
$additional_prepared_meta['subtitle'] = $subtitle; | |
$additional_prepared_meta['lead'] = $lead; | |
$additional_prepared_meta['acf_content'] = $text; | |
// afterwards merge new and old data | |
$new_prepared_meta = array_merge($old_prepared_meta, $additional_prepared_meta); | |
$post_args['post_meta'] = $new_prepared_meta; | |
return $post_args; | |
} | |
//add_filter( 'ep_config_mapping', function ( $mappings ) { | |
// $array = var_export($mappings, true); file_put_contents(get_stylesheet_directory() . '/dump.txt', $array); | |
// // my custom russian analyzer | |
// $mappings['settings']['analysis']['analyzer']['default'] = array( | |
// 'type' => 'custom', | |
// 'filter' => array( | |
// "standard", | |
// "lowercase", | |
// "russian_morphology", | |
// "stop" | |
// ), | |
// ); | |
//// $array = var_export($mappings, true); file_put_contents(get_stylesheet_directory() . '/dump.txt', $array, FILE_APPEND); | |
// | |
// | |
// return $mappings; | |
//} ); | |
function elasticpress_config_mapping($mapping) | |
{ | |
// bail early if $mapping is missing or not array | |
if ( ! isset($mapping) || ! is_array($mapping)) { | |
return false; | |
} | |
// ensure we have filters and is array | |
if ( | |
! isset($mapping['settings']['analysis']['filter']) | |
|| ! is_array($mapping['settings']['analysis']['filter']) | |
) { | |
return false; | |
} | |
// ensure we have analyzers and is array | |
if ( | |
! isset($mapping['settings']['analysis']['analyzer']['default']['filter']) | |
|| ! is_array($mapping['settings']['analysis']['analyzer']['default']['filter']) | |
) { | |
return false; | |
} | |
$json_analizers = '{"ru_stemming": { | |
"type": "snowball", | |
"language": "Russian" | |
}, | |
"ru_stopwords": { | |
"type": "stop", | |
"stopwords": "а,без,более,бы,был,была,были,было,быть,в,вам,вас,весь,во,вот,все,всего,всех,вы,где,да,даже,для,до,его,ее,если,есть,еще,же,за,здесь,и,из,или,им,их,к,как,ко,когда,кто,ли,либо,мне,может,мы,на,надо,наш,не,него,нее,нет,ни,них,но,ну,о,об,однако,он,она,они,оно,от,очень,по,под,при,с,со,так,также,такой,там,те,тем,то,того,тоже,той,только,том,ты,у,уже,хотя,чего,чей,чем,что,чтобы,чье,чья,эта,эти,это,я,a,an,and,are,as,at,be,but,by,for,if,in,into,is,it,no,not,of,on,or,such,that,the,their,then,there,these,they,this,to,was,will,with" | |
} | |
}'; | |
$analyzers = json_decode($json_analizers, true); | |
foreach ($analyzers as $index => $analyzer) { | |
$mapping['settings']['analysis']['filter'][$index] = $analyzer; | |
$mapping['settings']['analysis']['analyzer']['default']['filter'][] = $index; | |
} | |
$array = var_export($mapping, true); file_put_contents(get_stylesheet_directory() . '/dump.txt', $array); | |
// define the custom filter | |
// $mapping['settings']['analysis']['filter'][] = json_decode($json_analizers, true); | |
// tell the analyzer to use our newly created filter | |
$mapping['settings']['analysis']['analyzer']['default']['filter'][] = 'russian_morphology'; | |
$mapping['settings']['analysis']['analyzer']['default']['language'] = 'Russian'; | |
return $mapping; | |
} | |
add_filter('ep_config_mapping', 'elasticpress_config_mapping', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment