Like what we’re doing? Star us on GitHub!

How to search collection that has a relationship element content in rich text field?

EasonSoong
7 months ago
1 2

I am implementing a feature to get all other entries that has a relationship link to current entry. For example I have an author collection and an article collection. The article has a relationship field author. This feature shows how many articles are using this author instance as their relationship field author's value. In fact I using the rest api ?where[author][equals]=author_id to search this and it works.

image

But if I use a relationship in rich text that link to the author. Then how to search the collection that its rich text field has a relationship link to the author_id?

image

  • jmikrut
    Payload Team
    7 months ago

    Hey @EasonSoong

    You should be able to do this with something like:

    where: { myRichTextField: { contains: authorID } }
    

    Rich text fields can be queried on just like any other field, so give that a shot!

    3 replies
  • EasonSoong
    7 months ago

    Hi @jmikrut, thanks for your replying. And I tried to search document in this way but it did not worked. Below is my search params.

    // rest api search 
    api/collection?where[testRichText.value.id][equals]=rich-text
    // my collection field definition
    {
      name: 'testRichText',
      type: 'richText',
    }
    // data saved in my collection
    "testRichText": [
        {
          "children": [
            {
              "text": ""
            }
          ],
          "type": "relationship",
          "value": {
            "id": "rich-text"
          },
          "relationTo": "authors"
        },
        {
          "children": [
            {
              "text": ""
            }
          ]
        }
    ]

    I looked into the code of how to find a document in db because I want to figure out why my search api did not work. And I found my query was finally transformed to {"testRichText":{"$eq":"rich-text"}}. The .value.id was ignored. That's the key factor in fact.

    At last I found a buildQuery function and I think the problem is in here.

    I think it's because the rich text doesn't have clear schema definition. The schema.pathType will return adhocOrUndefined for the path testRichText.value or testRichText.value.id. And the real query to mongo will become another way other than how I write the code.

    And I think the blocks field has the same issue if one of the added block contains a rich text content.

  • EasonSoong
    7 months ago

    Is this an issue of the buildQuery or I didn't use it correctly?

  • jmikrut
    Payload Team
    7 months ago

    Hey @EasonSoong — I just looked into this and figured out a way to make this work.

    #888

    This will be released shortly.

    But basically, once it's merged, to query against rich text field content, you need to do something like this:

    where: { 'myRichTextField.children.value.id': { equals: yourID } }
    

    We will be deploying a patch version with this new feature shortly!

  • EasonSoong
    7 months ago

    Hello @jmikrut , I tried this feature in the newest payload version. And it works when I trying to search relationship in a rich text field. Thanks you for that!
    But I still cannot search relationship in a block rich text field.

    // I have a article collection which use a block field
    export const Articles: CollectionConfig = {
        fields: [
            {
                name: 'content',
                type: 'blocks',
                blocks: [Content, Media, Button],
            },
        ]
    };
    // And the Content block has a rich text field
    export const Content: Block = {
      fields: [
          {
              name: 'richText',
              type: 'richText',
          }
      ],
    };
    // My saved data in database
    {
        content: [{
            blockType: "content",
            id: "62e63a7bc3b0131e1b5d53a9",
            richText: [{
                children: [{text: ''}],
                relationTo: "authors",
                type: "relationship",
                value: { id: "rich-text" }
            }]
        }]
    }

    My search api:
    /api/articles?where[content.richText.value.id][equals]=rich-text&depth=0

    When I try to search relationship in rich text content from block field, the return result of buildQuery still ignore my path after content. Could you help to check this?

    5 replies
    EasonSoong
    7 months ago

    Do we have a plan we fix this?

    jmikrut
    Payload Team
    7 months ago

    Hey @EasonSoong - yes, sorry, was traveling today but this will be checked into first thing tomorrow!

    jmikrut
    Payload Team
    7 months ago

    OK @EasonSoong there was a bug related to an update of Mongoose. I just fixed it. You should now be able to query on fields within blocks.

    Will you give it a shot?

    EasonSoong
    7 months ago

    Sorry @jmikrut, it seems that it makes a progress but didn't resolve the issue completely. The path after content.richText is still been ignored.

    image

    queryBuild:  {
      where: { and: [], 'content.richText.value.id': { equals: 'test-213' } }
    }
    query:  { 'content.richText': { '$eq': 'test-213' } }
    EasonSoong
    7 months ago

    Hi @jmikrut , I upgraded my payload to 1.0.22 and it works now. You are really awesome, thank you~

Open the post
Continue the discussion in GitHub
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More