26

When I perform a search, the result entries display the relevant tags, but only if it is a question link. If it's an answer link, there are no tags displayed.

Sometimes I can easily infer the language, platform or technology, by reading the title and the excerpt -- but sometimes not, requiring me a visit to the question page.

I suggest that when answers are displayed as search result entries, these entries include the tags of the parent question.

enter image description here

  • 3
    So you want answers to display the tags of the parent question, right? Answers themselves are not tagged. – jmac Sep 26 '13 at 1:39
  • @jmac yes, of course – That Brazilian Guy Sep 26 '13 at 14:42
  • I think it's better to add tags in front of question title or something like that instead of below the answer ;). – shA.t Apr 30 '15 at 9:36
  • Well the system already does that for external search engines (it actually renames the <title> with the tag). – boxspah May 1 '15 at 1:14
7
+50

I've made a userscript to do this. It works on search pages on all SE sites. It will find the tags for the question the answer belongs to, and will add the tags and appropriate CSS depending on what type of tag it is.

// ==UserScript==
// @name         Show tags for answers when searching
// @namespace    http://stackexchange.com/users/4337810/
// @version      1.1
// @description  Adds the tags to answers whilst searching to provide useful context!
// @author       ᔕᖺᘎᕊ (http://stackexchange.com/users/4337810/)
// @match        *://*.stackexchange.com/search?q=*
// @match        *://*.stackoverflow.com/search?q=*
// @match        *://*.superuser.com/search?q=*
// @match        *://*.serverfault.com/search?q=*
// @match        *://*.askubuntu.com/search?q=*
// @match        *://*.stackapps.com/search?q=*
// @match        *://*.mathoverflow.net/search?q=*
// @grant        none
// ==/UserScript==
var sitename = $(location).attr('hostname').split('.')[0], //sitename for API call
    ids = [],
    idsAndTags = {};

$.each($('div[id*="answer"]'), function() { //loop through all *answers*
    ids.push($(this).find('.result-link a').attr('href').split('/')[2]); //Get the IDs for the questions for all the *answers*
});
$.getJSON("https://api.stackexchange.com/2.2/questions/" + ids.join(';') + "?site=" + sitename, function(json) {
    itemsLength = json.items.length;
    for (i = 0; i < itemsLength; i++) {
        idsAndTags[json.items[i].question_id] = json.items[i].tags;
    }

    $.each($('div[id*="answer"]'), function() { //loop through all *answers*
        id = $(this).find('.result-link a').attr('href').split('/')[2]; //get their ID
        $that = $(this);
        for (x = 0; x < idsAndTags[id].length; x++) { //Add the appropiate tags for the appropiate answer
            $that.find('.summary .tags').append("<a href='/questions/tagged/" + idsAndTags[id][x] + "' class='post-tag'>" + idsAndTags[id][x] + "</a>"); //add the tags and their link to the answers 
        }
        $that.find('.summary .tags a').each(function() {
            if ($(this).text().indexOf('status-') > -1) { //if it's a mod tag
                $(this).addClass('moderator-tag'); //add appropiate class
            } else if ($(this).text().match(/(discussion|feature-request|support|bug)/)) { //if it's a required tag
                $(this).addClass('required-tag'); //add appropiate class
            }
        });
    });
});

You can get this with with my SE Additional Optional Features userscript :)

Change in v1.1: changed to use 100 ids in one request, so you don't get a throttle violation (thanks for idea @2mkgz, and idea on implementation :)


It might be worth mentioning that it seems the tags are already known - the CSS classes are already there for them, eg. (with this search - How can I input '#' character in the beginning of the line?):

<div class="tags user-tags t-discussion t-code-formatting">
</div>

You can see the tags are already listed - all SE needs to do is add the links to those tags under the answer's!

3

This is a bit of a poor man's solution but with this SEDE query you can achieve something similar while we wait for the feature to be added.

select top 100
       p.id as [Post Link]
     , case when p.posttypeid = 1 then 'Q'
            when p.posttypeid = 2 then 'A'
       end as QA
     , left(p.body,100) as excerpt
     , case when mq.id is null then p.tags
            else mq.tags
       end as tags
from posts p
left outer join posts mq on mq.id = p.parentid
where p.body like '%##search##%'
-- add other search criteria at will
and p.posttypeid in (1,2) -- Q and A

Notice that I needed to reduce the number of rows returned because the full table scan is not something that will fly on the Stack Overflow posts table. You can add search criteria for the title and/or other columns like score and creationdate. I leave that as an exercise for the reader.

The rendered result looks like this: search result with tags

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .