Search code examples
databasetagssearch-enginehashtag

Multiple tag search engine


Whenever we use hashtag for searching that always results from a single tag (except Tumblr). Just wondering is there a search engine allows multiple tag search? I suppose that using different combination of tags could be fun and accurate:

#A  general results
#A, #B more specific
#A, #B, #C, #D close to the truth what attributes (tags) are given.

Is any website served in this way? Or how can I build a database to make this happened? Thank you a lot.


Solution

  • This is old and written in VB but the principle I think will work for you.

    This code is not completely limiting to Images that have all of the tags it is instead pushing the images with the most tag hits to the top.

    This code works from the idea of show the user what fits best first but then give them other options below. Where as your AND scenario would cut out something that worked for 4 out of 5 of the tags. In this scenario they would just show below tags that had 5 out of 5.

    So if you had some images with tags like:

    Given:

    Image1 woman,dog,panda

    Image2 woman,telephone,smiling

    Image3 man,dog,panda,banana

    Image4 man,telephone,smiling

    Yield:

    A tag search of "telephone smiling" would score [Image2] and [Image4] to the top of the listing.

    A tag search of "panda" would only yield Image1 and Image3.

    A tag search of "man dog panda banana" Would yield Image3 as the top followed by Image1 followed by Image4.

    This implementation is for finding the best image based on tags.

    You create 3 tables in your SQL database as such (if you are doing webpages or stories or whatever change image to that for your own clarity):

    SQL Tables:

    imageTag [INT ID, String Tag]
    imageImages [INT ID, NVARCHAR(2000) Path]
    imageConnections [INT TagID, INT ImageID]
    

    VB.NET Code:

    'The beef of the SQL statement to get the scored results is here.
    Dim SearchString As String = 
        "SELECT it.path, count(it.path) AS cnt, it.Id, it.name, it.description, it.updated FROM imageimages AS it, imageconnections AS itt, imagetags AS t WHERE " + _
        "{REPLACE-TAG} AND t.id = itt.tagid AND it.id = itt.imageid " + _
        "GROUP BY Path, it.ID, it.name, it.description, it.updated ORDER BY cnt DESC"
    
    
    
    Dim keywords As String() = tstSearch.Text.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    
    If keywords.Length > 0 Then
    
        Dim strReplacement As String
    
        strReplacement = "( tag = '" + keywords(0) + "'"
    
        If keywords.Length > 1 Then
    
            For i As Integer = 1 To keywords.Length - 1
                strReplacement += " OR tag = '" + keywords(i) + "'"
            Next
    
        End If
    
        strReplacement += " )"
    
        SearchString = SearchString.Replace("{REPLACE-TAG}", strReplacement)
    
        dt = tran.GetDataTable(SearchString)
    
    End If