• I must be doing something wrong here but I can’t figure it out.

    I’m trying to grab Relevanassi search results via Ajax. I’m doing this the standard WordPress way via an ajax call to admin-ajax.php with a dedicated function registered in functions.php.

    I get different results on the search page vs my ajax call.

    Here is my relevanssi query in my ajax function for the search query “wow” (hardcoded for now):

    global $query;
    $search_results = $query;
    
    $search_results->query_vars['s'] = 'wow';
    $search_results->query_vars['posts_per_page'] = 10;
    $search_results->query_vars['offset'] = 0;
    
    relevanssi_do_query($search_results);

    “found_posts” on my search page is equal to 29 for the same search term, whereas the relevanssi call in my ajax query returns 39 found_posts.

    They are both using the same relevanssi query:

    string(391) "SELECT relevanssi.*, relevanssi.title * 5 +
    	relevanssi.content + relevanssi.comment * 0.5 +
    	relevanssi.tag * 3 + relevanssi.link * 0 +
    	relevanssi.author + relevanssi.category * 1 + relevanssi.excerpt +
    	relevanssi.taxonomy + relevanssi.customfield + relevanssi.mysqlcolumn AS tf
    	FROM wp_relevanssi AS relevanssi  WHERE  relevanssi.term = 'wow'   ORDER BY tf DESC LIMIT 500"

    Any idea why this might be happening? I’m stumped.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Which is the correct result? If you run the MySQL query on the database, how many results does it return? I’d start with that.

    I’m assuming the bigger number is correct, so I guess the next step is to try to figure out where the posts are going missing. You could for example check what relevanssi_hits_filter contains – how many posts you have there?

    Thread Starter niemand_0

    (@niemand_0)

    Great idea with running the query directly on the database.

    That turned out to not shed any light on the issue (other than that it returned the higher number as well), but using relevanssi_do_query in search.php (so I had the exact same code in both search.php and functions.php) and comparing the search results post by post did:

    Turns out the query in functions.php returns posts with status “draft” and “pending review” as well as “published”. Adding post_status = 'publish' fixed it:

    global $query;
    $search_results = $query;
    
    $search_results->query_vars['s'] = 'wow';
    
    $search_results->query_vars['posts_per_page'] = -1;
    $search_results->query_vars['post_status'] = 'publish';
    $search_results->query_vars['offset'] = 0;
    
    relevanssi_do_query($search_results);

    However, now I have two more issues/questions for you.

    In search.php (using the same code as above), relevanssi excerpts are only be generated for the first 10 posts. I am using get_the_excerpt().

    What is the best way to go about ordering posts with the same relevance_score? I would like posts with a more recent date to show up first.

    • This reply was modified 7 years, 4 months ago by niemand_0. Reason: cleaner code
    Plugin Author Mikko Saari

    (@msaari)

    Ah, that explains. Relevanssi doesn’t include private posts (ie. drafts and pending) in all searches, only when the user is allowed to see them.

    I’ll answer the excerpt thing in that thread, but for the ordering question, I wouldn’t spend too much time on secondary sorting, as posts generally very rarely have exactly the same relevance score – unless you do some rounding, the secondary scoring is not going to make much difference.

    If you want to go for it, then relevanssi_hits_filter is the way to go, it’ll give you the list of posts in relevancy order, then you need to manipulate the array so that you get the posts in the order you want.

    Relevanssi Premium includes a recency boost feature where posts newer than the cutoff date you specify get a weight boost – that might be a better approach for boosting more recent posts.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Search results via Ajax produces different results’ is closed to new replies.