5

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?

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

2 Answers 2

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.com/grossherr/elasticpress-autosuggest-endpoint

6
  • Hi @nicolai - wondering if your plugin is up to date? I am following your example for the endpoint url after installing but I simply get "{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}" each time.
    – wharfdale
    Commented Aug 29, 2019 at 14:59
  • example.co.uk/wp-json/elasticpress/autosuggest
    – wharfdale
    Commented Aug 29, 2019 at 15:00
  • @wharfdale I haven't really dealt with the issue in about a year, so I'd say it's probably not up to date. Commented Aug 30, 2019 at 7:21
  • 1
    Anyone got this to work in 2019 with ElasticPress > = 3.2.1?
    – user127091
    Commented Oct 17, 2019 at 0:03
  • The above code is not working.. it breaks when it reaches the ClientBuilder.. does anyone have a recent example that actually works on the latest versions? Commented Aug 24, 2020 at 21:12
2

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.com/wp-json/elasticpress/autosuggest/ (or whatever is specified in register_rest_route()) as the endpoint URL in the admin / ElasticPresss / Autosuggest / Settings.

3
  • 1
    Update - you don't have to modify ep_autosuggest anymore. I created a pull request that has been merged into master. Now, these updates are included in the plugin!
    – Dmitriy
    Commented Apr 25, 2019 at 22:34
  • Does this still work for you and if so, which versions are you using of ElasticPress and ElasticSearch? I always get error messages about ep_get_host() or ep_get_index_name(), no matter what I try.
    – user127091
    Commented Oct 17, 2019 at 0:02
  • 1
    I haven't worked on this in a while, the only thing I know is that the ElasticPress plugin version is 2.7.0. What helped me while debugging issues is actually stepping through the code with Xdebug and seeing where it fails. Maybe try that?
    – Dmitriy
    Commented Oct 30, 2019 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.