grossherr / elasticpress-autosuggest-endpoint

24 stars 9 forks source link

Plugin is not working with latest version of elasticpress Version 3.3 #12

Open tricore-dev11 opened 4 years ago

tamara-m commented 4 years ago

@tricore-dev11 Hi. Have the exact same problem here after upgrading ElasticSearch to 7.5 and ElasticPress to 3.3 to make use of ES 7.5

With ElasticPress 3.3 and ElasticSearch 5.6 it still works.

Did you manage to figure it out or gain any insight?

brandwaffle commented 4 years ago

@tricore-dev11 @tamara-m I've determined the problem is in line 51 of this plugin https://github.com/grossherr/elasticpress-autosuggest-endpoint/blob/master/elasticpress-autosuggest-endpoint.php#L51. ep_get_index_name is deprecated because as of Version 3.0 of EP, there are multiple Indexables (Posts and Users, with Terms launching in 3.4).

To provide Autosuggest for Posts, you can call this method instead \ElasticPress\Indexables::factory()->get( ‘post’ )->get_index_name();

@tamara-m I'm not sure how this lines up since it seems for you the problem only exists with ES updated, not just EP updated to > 3.0. Since this method doesn't exist in the plugin, it shouldn't work with any version of ES. Either way, if this is updated in this plugin it will work again with EP > 3.0.

tamara-m commented 4 years ago

@brandwaffle

Replying on the other issue as well, but sorry to say this is not the issue we are having although it's true this plugin as it is here doesn't work any more. As mentioned a couple of times, I had already updated this plugin to work with the refactored ElasticPress 3

When using the updated EP 3 functions for getting the host and index, this plugin works with ElasticSearch 5.6 through 6.8 and stops working as from ElasticSearch 7

tamara-m commented 4 years ago

@grossherr

If you like you can update your plugin for ElasticPress 3

/**
 * Load Elasticseach PHP Client
 */
require 'vendor/autoload.php';

/**
 * Init Elasticsearch PHP Client
 */
use Elasticsearch\ClientBuilder;
use ElasticPress\Indexables as Indexables;
use ElasticPress\Utils;

/**
 * Register Elasticpress Autosuggest Endpoint
 *
 * This is the endpoint you have to specify in the admin
 * like this: http(s)://domain.com/wp-json/elasticpress/autosuggest/
 */
add_action( 'rest_api_init', function() {
    register_rest_route( 'elasticpress', '/autosuggest/', [
        'methods' => \WP_REST_Server::CREATABLE,
        'callback' => 'ep_autosuggest',
    ] );
} );

/**
 * Elasticpress Autosuggest Endpoint Callback
 *
* gets host and index name dynamically. Otherwise,
 * if not specified, host would default to localhost:9200
 * and index name would default to 'index'
 *
 * @param \WP_REST_Request $data
 * @return array|callable
 */
function ep_autosuggest( \WP_REST_Request $data ){

$client = ClientBuilder::create();
$client->setHosts( [ElasticPress\Utils\get_host()] ); // get host dynamically
$client = $client->build(); 

$json_params = $data->get_json_params();

$params = [
        'index' => Indexables::factory()->get( 'post' )->get_index_name(), // get index dynamically
        'type' => 'post',
        'body' => $json_params
        ];

$response = $client->search( $params );

return $response;
}
victorpintot8 commented 3 years ago

Hi there, does anyone know where to copy this code snippet?. I'm pasting in wp-content/plugins/elasticpress/includes , but when I load the URL in the browser, it always says {"code":"rest_no_route","data":{"status":404}}

marianmazarovici commented 3 years ago

@victorpintot8 the URL only accepts POST method.

danharper83 commented 3 years ago

I'm also confused about where to put this code, is this just a plugin?

Also any reason you're not maintaining it anymore?

jasonruyle commented 3 years ago

I'm also confused about where to put this code, is this just a plugin?

Also any reason you're not maintaining it anymore?

Take this original plugin, swap out the code above in the file elasticpress-autosuggest-endpoint.php and in the plugin folder, run:

composer install

As you'll want those dependencies. There are issues with the code above, its not full compatible with the latest past 5.5, but that should get you started

tamara-m commented 8 months ago

I am testing this again for a new project after a long time and came across an issue in the composer file now running PHP 8.xx

For this test, I am using ElasticSearch version 7.5, so if this is OK for your project and you are using PHP 8.xx, update your composer.json file like so:

{
    "require" : {
        "elasticsearch/elasticsearch" : "~7.0"
    },
    "authors" : [{
            "name" : "Nicolai",
            "email" : "sonst@ngcorp.de",
            "homepage" : "https://ngcorp.de/"
        }
    ],
    "name" : "grossherr/elasticpress-autosuggest-endpoint",
    "description" : "Creates Endpoint for the WordPress Plugin ElasticPress to make use of the Autosuggest feature",
    "homepage" : "https://github.com/grossherr/elasticpress-autosuggest-endpoint",
    "license" : "GPL-3.0+",
    "keywords" : [
        "ElasticPress Autosuggest Elasticsearch WordPress"
    ],
    "type": "wordpress-plugin"
}