Support » Plugin: Relevanssi - A Better Search » Order the query without filtering it

  • Resolved fabiencharrasse

    (@fabiencharrasse)


    Hello and thank you for this great plugin !

    I use relevanssi outside classic search page so i put my query object inside relevanssi_do_query() function.
    I’m trying to sort by relevance the posts without excluding any.
    So if my query return 100 results without relevanssi, i want it to return the same number with relevanssi but ordered by relevance even if score of some posts is zero.

    In summary, I want the plugin to just order results

    is it possible ?

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

    (@msaari)

    Relevanssi doesn’t like weight 0 and will weed out results that don’t match the search query at all. If you want to include everything Relevanssi finds, you need to make sure nothing gets weight zero.

    This filter will give posts with weight zero a weight of 0.00001 – so practically zero, but still enough to be included in the results.

    add_filter( 'relevanssi_match', 'rlv_not_zero' );
    function rlv_not_zero( $match ) {
        if ( 0 === $match->weight ) {
            $match->weight = 0.00001;
        }
        return $match;
    }

    That should help.

    Thread Starter fabiencharrasse

    (@fabiencharrasse)

    Thanks for the quick answer !
    This filter seems to be running too late, posts are already excluded at this point.

    I tried something that seems to work but I don’t realy understand :

    add_filter( 'relevanssi_fuzzy_query', 'order_the_results' );
    function order_the_results( $hits ) {
        return "1";
    }

    Is this correct ?

    Plugin Author Mikko Saari

    (@msaari)

    No, the first time Relevanssi excludes anything with weight 0 comes immediately after relevanssi_match is run.

    That filter is going to make Relevanssi match everything. At that point you’re really better off not using Relevanssi at all.

    Relevanssi does require the results include the search terms; if they are not included (which would be the most common reason for weight 0) the posts are not used as results. But that’s something you can’t really circumvent; Relevanssi can’t order posts by relevancy if there’s no relevancy at all. In that case Relevanssi would always return ALL posts to every query, and that’s just impossible in most cases because it would lead to “out of memory” errors in every search.

    So perhaps if you elaborate a little bit what you’re actually trying to achieve here, I may be able to help you better.

    Thread Starter fabiencharrasse

    (@fabiencharrasse)

    Ok, I’ll try to explain more fully :

    My query args :

    $args = array(
            		    'post_type'          => 'word',
            		    'lang'          => pll_current_language(),
            		    'paged'          => $paged,
            		    'posts_per_page' => 100,
            		    's'       => $word,
            		    'order' => 'ASC',
            		    'tax_query' => array(
            		        array(
            		            'taxonomy' => 'rime',
            		            'field'    => 'term_id',
            		            'terms'    => $term->term_id,
            		        ),
            		    ),
            		);
                    global $wp_query, $paged;
            		$wp_query = new WP_Query( $args );
            		relevanssi_do_query($wp_query);

    I search in a list of custom post type “word”. My query returns words whose ending rhymes with the word searched by the user. All the results are already valid thanks to the taxonomy filter. What I want is to just display the most relevant ones first and all the other valid words then.

    Example (With French words but no matter) :

    If the user searches for “tir”, the search returns the following results (which ends with the sound “ir”) :

    (the most relevant)

    sortir
    attire
    retentir
    tire
    sentir
    partir
    avertir
    mentir
    garantir

    (valid but less relevant)

    cuir
    saisir
    mourir
    souffrir

    This result is correct and it is obtained using the filter 'relevanssi_fuzzy_query'. If i use your filter, the query only return “the most relevant” part and not “valid but less relevant” part.

    I hope it’s clearer now.

    Plugin Author Mikko Saari

    (@msaari)

    Ok, that’s just not going to work. Relevanssi can’t sort those words in order of Relevancy: all will appear exactly as relevant.

    What if instead of using Relevanssi, you just fetch the rhyming words with your tax_query and then go through the results and split them in two arrays depending on whether the search term appears in the word or not? It’d be easier and more effective than involving Relevanssi in something that really has very little to do with what Relevanssi is built to do.

    Thread Starter fabiencharrasse

    (@fabiencharrasse)

    Thanks for the help and suggestion 😉

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Order the query without filtering it’ is closed to new replies.