• Resolved swinggraphics

    (@swinggraphics)


    Are taxonomy terms able to be used for weighting results? If someone searches for “art” I would want to rank a post tagged art higher than one that just happens to mention the world more times.

    Can weight be added to the terms for the HTML tag they appear in? For example, an article with <h2>Picasso</h2> would rank above one that only has Picasso in a paragraph.

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

    (@epsiloncool)

    Hi @swinggraphics

    I would propose to add tags to the search index, to the separate cluster “my_tags” and then assign this cluster a biggest weight (1).

    So these posts will be ordered much higher than others when any tag is matched.

    The same way you can apply to H2 texts in content – first you need to find those texts (using preg_match for example), put them to the separate index cluster and assign more weight to this cluster.

    Here is how the idea can be implemented with tags:

    
    add_filter('wpfts_index_post', function($index, $post)
    { 
        // Basic tokens 
        /* 
         * This piece of code was commented out intentionally to display things
         * which was already done before in the caller code
        $index['post_title'] = $post->post_title;
        $index['post_content'] = strip_tags($post->post_content);
        */
        if ($post->post_type == 'my_custom_post_type') {	// You need to specify your own post type here
       		
    	// You can add tags to the index in order to make this post 
    	// searchable by them (means fulltext search by tag name)
    	
    	$tags = wp_get_post_tags($post->ID);
    		
    	$t = array();
    	if (is_array($tags)) {
    		foreach ($tags as $tag) {
    			if (isset($tag->name)) {
    				$t[] = $tag->name;
    			}
    		}
    	}	
    	$index['my_tags'] = implode(' ', $t);  // Cluster name is imaginary
    
        } 
        return $index; 
    }, 3, 2);
    

    This code will be called on each post update or on full rebuild index.
    I’d recommend to update specific post to check how it works.

    You also can use WPFTS Settings / Sandbox / Index Tester to see what’s coming to index.

    Hope this helps.

    Thread Starter swinggraphics

    (@swinggraphics)

    Ah yes, I see. Have to wrap my head around that. Thanks!

    I’d like to suggest adding a screen shot after the example code in https://fulltextsearch.org/documentation/#item4_2 that shows the updated Cluster Weights. That would jump out to someone like me looking to add options there.

    Plugin Author Epsiloncool

    (@epsiloncool)

    Okay, @swinggraphics I will definitely do that.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Weighting based on taxonomy and HTML tags?’ is closed to new replies.