$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>ajax - REST API endpoint for elasticpress autosuggest|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ajax - REST API endpoint for elasticpress autosuggest

matteradmin9PV0评论

The AJAX, which is part of Elasticpress, looks like this

$.ajax( {
    url: epas.endpointUrl,
    type: 'GET',
    dataType: 'json',
    crossDomain: true,
    data: JSON.stringify( query )
} );

Additionally I registered my endpoint

add_action( 'rest_api_init', function ( $data ) {
    register_rest_route( 'elasticpress', '/autosuggest/', [
        'methods' => 'GET',
        'callback' => 'ep_autosuggest'
    ] );
} );

The callback looks like this

function ep_autosuggest( $data ) {
    // Elasticsearch PHP Client
    $client = ClientBuilder::create()->build();
    $params = [
        'index' => 'index',
        'type' => 'post',
        'body' => $data
    ];
    $response = $client->search( $params );
    return $response;
}

The different parts work as they should. I'm struggling with getting the data from the passed object. Any ideas?

The AJAX, which is part of Elasticpress, looks like this

$.ajax( {
    url: epas.endpointUrl,
    type: 'GET',
    dataType: 'json',
    crossDomain: true,
    data: JSON.stringify( query )
} );

Additionally I registered my endpoint

add_action( 'rest_api_init', function ( $data ) {
    register_rest_route( 'elasticpress', '/autosuggest/', [
        'methods' => 'GET',
        'callback' => 'ep_autosuggest'
    ] );
} );

The callback looks like this

function ep_autosuggest( $data ) {
    // Elasticsearch PHP Client
    $client = ClientBuilder::create()->build();
    $params = [
        'index' => 'index',
        'type' => 'post',
        'body' => $data
    ];
    $response = $client->search( $params );
    return $response;
}

The different parts work as they should. I'm struggling with getting the data from the passed object. Any ideas?

Share Improve this question edited Sep 8, 2018 at 18:35 Nicolai Grossherr asked Jan 25, 2018 at 7:15 Nicolai GrossherrNicolai Grossherr 18.9k8 gold badges64 silver badges111 bronze badges 2
  • What data? You're already passing data with the jQuery.ajax call. What other data do you want to send, and at what stage? – janh Commented Jan 25, 2018 at 8:06
  • Is it being sent correctly, have you checked your browser's dev tools? – janh Commented Jan 25, 2018 at 8:40
Add a comment  | 

2 Answers 2

Reset to default 8

After some inspecting the WP_REST_Request, it turned out, that the get_body() method was the one I'm looking for. Anyhow, this is what I ended up with:

add_action( 'rest_api_init', function() {
    register_rest_route( 'ep', '/as/', [
        'methods' => \WP_REST_Server::CREATABLE,
        'callback' => 'ep_autosuggest',
    ] );
} );
function ep_autosuggest( WP_REST_Request $data ) {
    // Elasticsearch PHP Client
    $client = ClientBuilder::create()->build();
    $params = [
        'index' => 'ep-test',
        'type' => 'post',
        'body' => $data->get_body()
    ];
    $response = $client->search( $params );
    return $response;
}

For anyone interested, I made a plugin out of it:

https://github/grossherr/elasticpress-autosuggest-endpoint

Thanks for the plugin Nicolai! Just wanted to point out a couple of things that weren't clear to me:

Once the plugin is installed, modify ep_autosuggest() in elasticpress-autosuggest-endpoint.php:

$params = [
    'index' => ep_get_index_name(), // get name of ES index dynamically
    'type' => 'post',
    'body' => $data->get_body()
];

Then, use http(s)://yourdomainname/wp-json/elasticpress/autosuggest/ (or whatever is specified in register_rest_route()) as the endpoint URL in the admin / ElasticPresss / Autosuggest / Settings.

Post a comment

comment list (0)

  1. No comments so far