Return object that corresponds to another tables' unique ids?

Hi, I'm running into issues. I'm sort of new to elastic search. Sometimes it works properly and sometimes it doesn't. Here are my issues..

I have 2 problems.

Problem 1:

I have a database of users. Users have an { active: true } property. Out of the 50 test users, 8 of them have the active prop as true. Also, they have a { isGrower: true } property as well.

My current code only displays 7 of these users, instead of 8. Am I missing something?

 let growers = await es.search({
        "index": table,
        "body": {
            "from": 0, "size": 200,
            "query": {
                "bool" : {
                    "must" : {
                        "match" : { "isGrower": true },
                        "match" : { "active": true }
                    }
                }
            }
        }
    })

  let { hits } = growers.hits
    
  let theReturn = await hits.map((item) => {
      return {
          'growerId': item._source.userId,
      }
  })

Problem 2:

I have an items table. Items is an object that belongs to each user. There can be multiple items for each user. In the User table, user has a unique ID that looks like ba0508dd-0e76-4be8-8b6e-9e938ab4abed. In the items table, the item has a unique itemId but also contains the userId, but called a growerId. This is how we find the items that belong to the user.

What I want to do is return an array of items that correspond to only active users.

module.exports = async (activeGrowers, searchQuery, table) => {
    
    let shouldArr = await activeGrowers.map(item => {
        return { "match": { "growerId": item.growerId } }
    })
    
    let must = [
        { "query_string" : 
            { 
                "query": searchQuery,
                "fields": ["item_name"] 
            }
        }
    ]
    
    let items = await es.search({
        "index": table,
        "body": {
          "from": 0, "size": 25,
          "query": {
            "bool": {
                "should": shouldArr,
                "must": must,
            }
          }
        }
    })
    

    return items.hits.hits
};


The search query works fine but the growerId is not matching the active user Ids.

How can I make sure that only active user items are being returned?

Thanks!!!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.