# Most efficient way to limit search result to List of Ids

**URL:** https://discuss.elastic.co/t/most-efficient-way-to-limit-search-result-to-list-of-ids/261082
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [January 14, 2021, 6:57am UTC](https://discuss.elastic.co/t/most-efficient-way-to-limit-search-result-to-list-of-ids/261082 "2021-01-14T06:57:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Hooman\_Bahreini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hooman_bahreini/32/29500_2.png) [@Hooman\_Bahreini](https://discuss.elastic.co/u/Hooman_Bahreini)
#### Post date: [January 14, 2021, 6:57am UTC](https://discuss.elastic.co/t/most-efficient-way-to-limit-search-result-to-list-of-ids/261082/1 "2021-01-14T06:57:31Z")

</div>

I am working on an e-commerce website. I have an index called `AdDocument` which is the products that are synched to Elasticsearch. Each product belongs to a category.

Now I want to write a query to get all products which belong to certain categories... for example, assuming I have the following categories:

- _Category: Fashion, CategoryId: 1_
- _Category: Sports, CategoryId: 2_
- _Category: Movies, CategoryId: 3_
- _Category: Books, CategoryId: 4_
- _Category: Furnitures, CategoryId: 5_
- _Category: Real Estate, CategoryId: 6_
- _Category: Jobs, CategoryId: 7_
- _Category: Services, CategoryId: 8_
- _and so on..._

I want to get all the products which belong to _Fashion, Sports, Movies_ or _Books_ category, so I would call the following method:

`GetQuery(new List<short> {1, 2, 3, 4}); // <-- Limit result to CategoryIds 1, 2, 3 or 4`

Using NEST, I have managed to achieve this using 2 different approaches:

1. Write a query for each category and logically `OR` all of them:

```auto
public QueryContainer GetQuery(List<short> categoryIds)
{
    var queries = new List<QueryContainer>();
    foreach (var categoryId in categoryIds)
    {
        var query = new TermQuery
        {
            Name = "Multi Category Filter",
            Field = Field<AdDocument>(p => p.CategoryId),
            Value = categoryId
        };

        queries.Add(query);
    }

    return queries.Aggregate((current, next) => (current || next));
}

```

1. Use `TermsQuery` and pass the `CategortIds` for `Terms`:

```auto
public QueryContainer GetQuery(List<short> categoryIds)
{
    var query = new TermsQuery
    {
        Name = "Multi Category Filter",
        Field = Field<AdDocument>(p => p.CategoryId),
        Terms = categoryIds.Cast<object>().ToList()
    };
	
	return query;
}

```

Both of the above approaches work as expected, however I don't know which is more efficient? If it was a SQL database, I knew that including so many `OR` conditions would negatively impact performance, but I don't know if this is also the case for Elasticsearch?

---

<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: [February 11, 2021, 6:57am UTC](https://discuss.elastic.co/t/most-efficient-way-to-limit-search-result-to-list-of-ids/261082/2 "2021-02-11T06:57:36Z")

</div>

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