10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.24k stars 312 forks source link

Any way to get suggestion? (custom search) #3963

Closed yarovikov closed 2 weeks ago

yarovikov commented 4 weeks ago

Describe your question

Hi there.

I'v got custom ajax search page like

https://site.test/search?query=chicken

There is any way to get suggestion via php? Thanks :)

Code of Conduct

felipeelia commented 3 weeks ago

@yarovikov I'm not 100% sure I follow what you are trying to achieve, but you can integrate ElasticPress with AJAX calls. Check out this article: https://www.elasticpress.io/documentation/article/how-to-integrate-elasticpress-with-ajax-requests/

If that doesn't help, do you mind explaining a bit more about your solution and how that ajax search page works behind the scenes?

yarovikov commented 3 weeks ago

@yarovikov I'm not 100% sure I follow what you are trying to achieve, but you can integrate ElasticPress with AJAX calls. Check out this article: https://www.elasticpress.io/documentation/article/how-to-integrate-elasticpress-with-ajax-requests/

If that doesn't help, do you mind explaining a bit more about your solution and how that ajax search page works behind the scenes?

I apologize for the inaccuracy of the question. I mean list of suggestions. Usually we can get them like this do_action( 'ep_suggestions' ) or \ElasticPress\Features::factory()->get_registered_feature('did-you-mean')->get_suggestion();

But this is not working with my search as expected.

burhandodhy commented 3 weeks ago

Hi @yaroviko,

You can retrieve the suggestion list from the WP_Query object like this.

$query = new \WP_Query( $args );
$suggestion_list = $query->suggested_terms['options'] ?? [];

Regards, Burhan

yarovikov commented 3 weeks ago

Hi, @burhandodhy

Many thanks, this is working. Another question: does this only work with one word? There always seems to be no results when the search query contains more than 1 word.

github-actions[bot] commented 3 weeks ago

It has been 3 days since more information was requested from you in this issue and we have not heard back. This issue is now marked as stale and will be closed in 3 days, but if you have more information to add then please comment and the issue will stay open.

burhandodhy commented 3 weeks ago

Hi @yarovikov,

It should work fine even if the search term contains multiple words. Could you please share the search term you're using, as well as the post content? This will help us replicate the issue.

Regards, Burhan

yarovikov commented 3 weeks ago

Hi @burhandodhy

For example

search query: opil suggestion list:

Array
(
    [0] => Array
        (
            [text] => opel
            [score] => 0.0035423585
        )

    [1] => Array
        (
            [text] => oil
            [score] => 0.0019742446
        )

    [2] => Array
        (
            [text] => open
            [score] => 0.0016957124
        )

    [3] => Array
        (
            [text] => opor
            [score] => 0.0015871174
        )

    [4] => Array
        (
            [text] => opir
            [score] => 0.0014965526
        )

)

search query: opil vectra suggestion list:

Array
(
    [0] => Array
        (
            [text] => opel vectra
            [score] => 0.0014879783
        )

)

search query: opil vectra best suggestion list: empty

github-actions[bot] commented 2 weeks ago

It has been 3 days since more information was requested from you in this issue and we have not heard back. This issue is now marked as stale and will be closed in 3 days, but if you have more information to add then please comment and the issue will stay open.

github-actions[bot] commented 2 weeks ago

This issue has been automatically closed because there has been no response to our request for more information in the past 3 days. With only the information that is currently available, we are unable to take further action on this ticket. Please reach out if you have found or find the answer we need so that we can investigate further. When the information is ready, you can re-open this ticket to share it with us.