Forum Discussion

anmi's avatar
anmi
Adept
3 years ago

json query syntax matches AND OR

Hello,

I cannot find the full json query syntax documentation here. Thats wha I'm asking in this post:

How does a json based query look like for AND matches in body text. I want to query all posts containing term1 AND term2 in their body.

 

My query currently is this. How do I add the term2?

[
{
"messages":{
"fields":[
"id",
"subject",
"search_snippet",
"body",
"cover_image.view_href",
"teaser",
"view_href",
"author",
"conversation.last_post_time",
"replies.count(*)",
"kudos.sum(weight)",
"metrics.views",
"labels"
],
"constraints":[
{
"category.id":"DE",
"conversation.style":"tkb",
"body":{ "matches":"term1" },
"depth":0
}
],
"sorts": [
"post_time desc"
],
"limit":10,
"subQueries":{
"labels":{
"fields":[
"text",
"id"
]
}
}
}
}
]

6 Replies

  • Hi anmi,

    Could you please try the below format,

    [
    {
    "messages":{
    "fields":[
    "id",
    "subject",
    "search_snippet",
    "body",
    "cover_image.view_href",
    "teaser",
    "view_href",
    "author",
    "conversation.last_post_time",
    "replies.count(*)",
    "kudos.sum(weight)",
    "metrics.views",
    "labels"
    ],
    "constraints":[
    {
    "category.id":"DE",
    "conversation.style":"tkb",
    "body":{ "matches":"term1" },
    "depth":0
    },
    {
    "body":{ "matches":"term2" },
    "depth":0
    }
    ],
    "sorts": [
    "post_time desc"
    ],
    "limit":10,
    "subQueries":{
    "labels":{
    "fields":[
    "text",
    "id"
    ]
    }
    }
    }
    }
    ] 

  • Akenefick's avatar
    Akenefick
    Genius
    3 years ago

    SyedSa I was really struggling with something very similar trying to match multiple labels with an AND logic with a JSON query. Easy to do with regular LiQL but could not get it to work with JSON. This worked great. Thanks!

  • SyedSa Does the second matches constraint need that 'depth': 0? If so, why?

  • Koben's avatar
    Koben
    Contributor
    22 days ago

    hi Akenefick its a long shot... but i don't suppose you have an example of the json query to match multiple tags?!?!

    we're really struggling to even know where to start in terms of getting it working...

  • Akenefick's avatar
    Akenefick
    Genius
    22 days ago

    Hi Koben are you on Aurora or Classic? I'm still on classic and only really familiar with that. So, here's how you would match multiple tags on classic with AND logic.

    GET https://community.yourcommunity.com/api/2.0/search

    [
        {
            "messages": {
                "fields": [
                    "author.avatar.message",
                    "author.login",
                    "author.view_href",
                    "author.rank.name",
                    "author.rank.icon_right",
                    "author.rank.color",
                    "subject",
                    "post_time",
                    "post_time_friendly",
                    "view_href",
                    "replies.count(*)",
                    "metrics.views",
                    "user_context.read",
                    "replies"
                ],
                "constraints": [
                    {
                        "board.id": "aktestboard",
                        "depth":0,
                        "tags.text": "tagone"
                    },
                    {
                        "tags.text": "tagtwo"
                    }
                ],
                "limit":10,
                "subQueries": {
                    "replies": {
                        "fields": [
                            "author.login",
                            "author.view_href",
                            "author.rank.name",
                            "author.rank.icon_right",
                            "author.rank.color",
                            "post_time"
                        ],
                        "limit": 1
                    }
                }
            }
        }
    ]

    The important part being the constraints

    "constraints": [
                    {
                        "board.id": "aktestboard",
                        "depth":0,
                        "tags.text": "tagone"
                    },
                    {
                        "tags.text": "tagtwo"
                    }
                ],

    And if you want an OR logic instead you would do it this way

    "constraints": [
                    {
                        "board.id": "aktestboard",
                        "depth":0,
                        "tags.text":{ "in":[ "tagone", "tagtwo" ] }
                    }
                ],

     

    You can find a lot of guidance on Json queries here The Community API v2 request

    And all the message fields and constraints etc. here messages

     

    I suspect since you're looking for tags instead of labels you might be on Aurora but if not happy to help if you have any more questions.

  • Koben's avatar
    Koben
    Contributor
    22 days ago

    Akenefickthat is amazing! thank you so much for getting back to us, it's really appreciated 😉

    the version we're on is still a bit confusing - at some point, tags became labels which threw us a little...

    but this is totally working for what we after!

    the 'or' query uses 'in':

    [
        {
            "messages": {
                "constraints": [
                    {
                        "depth": 0,
                        "labels.text": {
                            "in": [
                                "tag-one",
                                "tag-two"
                            ]
                        }
                    }
                ]
            }
        }
    ]


    the 'and' query uses multiple 'constraints':

    [
        {
            "messages": {
                "constraints": [
                    {
                        "depth": 0,
                        "labels.text": "tag-one"
                    },
                    {
                        "depth": 0,
                        "labels.text": "tag-two"
                    }
                ]
            }
        }
    ]

    we think you need to have the depth in each constraint, not 100% sure...

    again, many thanks for your help!