# How do I perform a search on a smart field?

**URL:** <https://community.forestadmin.com/t/how-do-i-perform-a-search-on-a-smart-field/3051>\
**Category:** Help me!\
**Tags:** setup\
**Created:** [August 4, 2021, 7:22am UTC](https://community.forestadmin.com/t/how-do-i-perform-a-search-on-a-smart-field/3051 "2021-08-04T07:22:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois\_Vongue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/francois_vongue/32/2679_2.png) [@Francois\_Vongue](https://community.forestadmin.com/u/Francois_Vongue)\
**Post date:** [August 4, 2021, 7:22am UTC](https://community.forestadmin.com/t/how-do-i-perform-a-search-on-a-smart-field/3051/1 "2021-08-04T07:22:41Z")

</div>

## Expected behavior

I expect that by typing some text in the top search bar (over the table)  
i will be able to search by my smart field

## Actual behavior

It doesn’t find anything without failure logs

## Context

Smart field itself:

```js
    {
      field: 'Job title',
      type: 'String',
      get: async ({id: disputeId}) => {
          const job = await disputeService.getJob(disputeId);
          return job.title;
      },
      search: async function (query, search) {
        const ids = await disputeService.searchByJobName(search);

        var searchCondition = {
          [Op.or]: [
            { id: { [Op.in]: ids } },
          ]
        };

        query.where[Op.and][0][Op.or].push(searchCondition);
        return query;
      }
    },

```

Service methods I use (they work but the part where I push into query object doesnt seem to work)

```js
    async searchByJobName(jobName) {
        const ids = sequelize.query(`
            select d.id from disputes d
            join offers o on d.offer_id = o.id
            join jobs j on o.job_id = j.id
            where j.title ilike :s;
        `, {
            replacements: { s: `%${jobName}%` },
            type: QueryTypes.SELECT
        });
        
        return ids;
    },
    async getJob(disputeId) {
        const [job] = await sequelize.query(`
            select d.id, j.* from disputes d
            join offers o on d.offer_id = o.id
            join jobs j on o.job_id = j.id
            where d.id = :did;
        `, {
            replacements: { did: disputeId },
            type: QueryTypes.SELECT
        });
        
        return job;
    },

```

---

<div class="post-metadata">

**Author:** ![anon79585656](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anon79585656](https://community.forestadmin.com/u/anon79585656)\
**Post date:** [August 4, 2021, 2:46pm UTC](https://community.forestadmin.com/t/how-do-i-perform-a-search-on-a-smart-field/3051/2 "2021-08-04T14:46:23Z")

</div>

Hello @Francois_Vongue,

Sorry for the delay.

Could you try to temporarily replace the asynchronous call (`await disputeService.searchByJobName(search);`) by a static list of ids?  
It might be that async calls are not supported at this place.

Thank you

---

<div class="post-metadata">

**Author:** ![Francois\_Vongue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/francois_vongue/32/2679_2.png) [@Francois\_Vongue](https://community.forestadmin.com/u/Francois_Vongue)\
**Post date:** [August 5, 2021, 8:38am UTC](https://community.forestadmin.com/t/how-do-i-perform-a-search-on-a-smart-field/3051/3 "2021-08-05T08:38:53Z")

</div>

Yes, you are right, it works with static ids but not with promises.

---

<div class="post-metadata">

**Author:** ![Francois\_Vongue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/francois_vongue/32/2679_2.png) [@Francois\_Vongue](https://community.forestadmin.com/u/Francois_Vongue)\
**Post date:** [August 5, 2021, 8:44am UTC](https://community.forestadmin.com/t/how-do-i-perform-a-search-on-a-smart-field/3051/4 "2021-08-05T08:44:13Z")

</div>

But what can I do about it?

---

<div class="post-metadata">

**Author:** ![anon79585656](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anon79585656](https://community.forestadmin.com/u/anon79585656)\
**Post date:** [August 5, 2021, 12:25pm UTC](https://community.forestadmin.com/t/how-do-i-perform-a-search-on-a-smart-field/3051/5 "2021-08-05T12:25:12Z")

</div>

@Francois_Vongue

I’ll fill a feature request on my end to see if we can add this async support.

In the mean time, I suggest you try to replicate your query from `searchByJobName` in the search handler, using `sequelize.literal`. This should inline you SQL request within the one generated by Sequelize.

Some exemples you can use:

- [Manual | Sequelize](https://sequelize.org/master/manual/sub-queries.html)
- [javascript - How can I bind a variable to sequelize literal? - Stack Overflow](https://stackoverflow.com/a/53965410/5726224)
- [mysql - Using a raw SQL query with Sequelize ORM and literal - Stack Overflow](https://stackoverflow.com/a/57740363/5726224)

Apologies, this is not ideal.
