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:
- 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));
}
- Use
TermsQuery
and pass theCategortIds
forTerms
:
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?