# Return object that corresponds to another tables' unique ids?

**URL:** <https://discuss.elastic.co/t/return-object-that-corresponds-to-another-tables-unique-ids/162276>\
**Category:** Elasticsearch\
**Created:** [December 27, 2018, 6:41pm UTC](https://discuss.elastic.co/t/return-object-that-corresponds-to-another-tables-unique-ids/162276 "2018-12-27T18:41:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andres\_512](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andres_512/32/39218_2.png) [@andres\_512](https://discuss.elastic.co/u/andres_512)\
**Post date:** [December 27, 2018, 6:41pm UTC](https://discuss.elastic.co/t/return-object-that-corresponds-to-another-tables-unique-ids/162276/1 "2018-12-27T18:41:14Z")

</div>

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?

```javascript
 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.

```javascript
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!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 24, 2019, 6:41pm UTC](https://discuss.elastic.co/t/return-object-that-corresponds-to-another-tables-unique-ids/162276/2 "2019-01-24T18:41:23Z")

</div>

Closed on January 24, 2019, 6:41pm UTC

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