Most efficient way to limit search result to List of Ids

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:
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:
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?

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