# Filter by categories

**URL:** https://discuss.elastic.co/t/filter-by-categories/271788
**Category:** Elasticsearch
**Created:** [April 30, 2021, 1:49pm UTC](https://discuss.elastic.co/t/filter-by-categories/271788 "2021-04-30T13:49:36Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ehsan\_kabiri\_33](https://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@ehsan\_kabiri\_33](https://discuss.elastic.co/u/ehsan_kabiri_33)
#### Post date: [April 30, 2021, 1:49pm UTC](https://discuss.elastic.co/t/filter-by-categories/271788/1 "2021-04-30T13:49:36Z")

</div>

My Index has a property named `categoryId`.My category Ids at the moment are `[1,2,3]` but there could be more categories like `[1,2,3,4,5]` in the future. Each record in my index can belongs to no categoryId , one categoryId or some categoryIds .  
Currently I use a `List<int>` in my `c#` application as a list of categories for each record in my index.  
This is a brief on how I index my records:

```auto
      //some codes here
      .Map<ItemElasticSearchViewModel>(m => m
                  .Dynamic(DynamicMapping.Strict)
                  .AutoMap()
                  .Properties(ps => ps               
                  .Keyword(k => k.Name(f => f.itemCategories).Norms(false))   
    //some codes here

```

And at the moment I filter my records by category like this, for example I need records which `categoryId=2` , so:

```auto
       //some codes here  
   multiSearchResponse = await client.MultiSearchAsync(selector: ms => ms
                  .Search<ItemElasticSearchViewModel>("Fuzziness_0", s => s
                    .Index("itemindex")
                    .From(0)
                    .Size(15)
                    .Query(q => q
                      .Bool(b => b
                        .Filter(f => f.Term(t => t.itemCategories, "2"))
                        .Must(mu => mu
                          .Match(m => m
                            .Field(f => f.itemSearch)
                            .Query(searchViewModel.searchText)
                            .Operator(Operator.And)
                          )
                        )
                      )
                    )
   //some codes here

```

Just focus on the `Filter` line. I need to know if there is a better way than what I have done, for filtering my searches by `categoryId`, cause at the moment I have about 200k records and I created a single record with `categoryId=8` and query time has no difference with or without using `Filter categoryId=8` . But in MySQL there is a significant difference in query time with or without `categoryId` filtering, cause when I use `where categoryId=8` in MySQL, it passes all the records except the one and so fast !!

How do you Filter your record by category in elastic search when you have flexible categories and a record can have multiple categories??

---

<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: [May 28, 2021, 1:50pm UTC](https://discuss.elastic.co/t/filter-by-categories/271788/2 "2021-05-28T13:50:28Z")

</div>

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