Forum Discussion

cike's avatar
cike
Champion
9 years ago

Query messages by tag using API v2

Hi,

 

I'm currently working on a component which show all messages for given tag. To get all messages I want to use REST API v2.

My request looks like this:

 

SELECT * FROM messages WHERE depth = 0 AND conversation.style IN ('forum', 'group') AND tags.text IN ('Router') LIMIT 10 OFFSET 0

The tags.text constraint is listed within the documentation of the message resource. But if I read the JSON response, the tags object only contains the query attribute.

 

Is there another way to query all messages for a given tag by using API v2?

 

Thanks and kind regards,

Christian

5 Replies

  • Maybe I found a solution by myself. I tried using the MATCHES operator and this seems to work:

     

    SELECT * FROM messages WHERE depth = 0 AND conversation.style IN ('forum', 'group') AND tags.text MATCHES ('${tag_filter}') LIMIT ${page_size} OFFSET ${offset}
  • irach15's avatar
    irach15
    Maven
    9 years ago

    Hi cike,

    I was looking for something else and found your question.

    Sorry, it might be too late and you've already figured it out :-)

    Anyway,

    here is the way how I'm getting all messages tagged with a specific tag

    <#assign messages = rest("2.0","/search?q=" + "SELECT id, body, subject, view_href, post_time, board.id, teaser, board FROM messages WHERE tags.text = 'sometag' AND depth=0 ORDER BY post_time DESC LIMIT 10"?url) />

    you can mention 'conversation.style' and other restrictions. you can try 'restadmin' if you don't see it.

    ------------------------

    BTW, I'm looking how to get all tags and/or labels for a specific article without mentioning an article id, just any article when the user views it.

    Any idea?

    Thank you.

  • ChhamaJ's avatar
    ChhamaJ
    Khoros Staff
    9 years ago

    Hi irach15

     

    You can try to do some thing like this 

    select * from tags where messages.id = '1'

     You will have to provide message id though.

     

    Regards,
    Chhama

  • irach15's avatar
    irach15
    Maven
    9 years ago

    ChhamaJ,

    yeaaa... I know about that way for a single id..

    I need for any id, not a specific one aka universal call...

  • irach15's avatar
    irach15
    Maven
    9 years ago

    OK,

    I found how.

    it needs to be a couple calls.

    Some other parameters here are just for testing purpose :-)

     

    <#if page.context.thread??>
    <#assign apiVersion = "2.0"/>
    <#assign articleId = page.context.thread.topicMessage.uniqueId?c />
    <#assign tagsQuery = "SELECT * FROM tags WHERE messages.id = '" + articleId + "'"/>
    <#assign tags= rest(apiVersion, "/search?q=" + tagsQuery?url + "&restapi.response_style=view").data.items![] />
    <h4>Tags!!!</h4>
    <#list tags as tag>
    <ul>
        <li>Tag: ${tag.text}</li>
    </ul>
    </#list>
    <ul>
        <li>articleId: ${articleId}</li>
        <li>apiVersion: ${apiVersion}</li>
    </ul>
    </#if>