# How to make a search result which can filter by options like amazon.com?

**URL:** <https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538>\
**Category:** Elasticsearch\
**Created:** [March 19, 2018, 11:16am UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538 "2018-03-19T11:16:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickwai](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@patrickwai](https://discuss.elastic.co/u/patrickwai)\
**Post date:** [March 19, 2018, 11:16am UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/1 "2018-03-19T11:16:07Z")

</div>

Hello  
I am building a search project for products with following requirement:

1. get results by a keyword
2. then can filter results by some options in the web page.

filter options , for example filter by categories, by brands. For each category and each brand have product quantity displayed in web page. Something like `Electronic(10)`. `Electronic` is the category name. `10` is the results quantity that is pre-retrieved.

I want to ask which kinds of queries to use with elasticsearch for better performace.

thanks in advacne

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [March 19, 2018, 2:33pm UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/2 "2018-03-19T14:33:53Z")

</div>

Hi,

1°/ For the keyword field you can use a string with a special analyzer

```
"type": "string" 
"analyzer": "human"

```

Exemple of applied filters:

```
				"filter": [
					"lowercase",
					"asciifolding",
					"trim" ]

```

Then use a multiMatchQuery on the keyword field.

2°/ For filters you can use a not indexed field

```
"type": "string",
"index": "not_analyzed".

```

On this filters, use termsQuery to filter documents.

bye  
Xavier

---

<div class="post-metadata">

**Author:** ![patrickwai](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@patrickwai](https://discuss.elastic.co/u/patrickwai)\
**Post date:** [March 19, 2018, 3:16pm UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/3 "2018-03-19T15:16:16Z")

</div>

Hi xavierfacq

this can be very helpful for me. I am new on elasticsearch.  
1st multiMatchQuery with analyzed on related fields.  
2nd step using termsQuery with not\_analyzed.

Do you have any idea about the quantity for each item. how I can get the quantity for each item?

Actually, I am using a multiMatchQuery without filters to get all docs and then programmingly get the quantity for each item(eg: category), and repeat it when applying to filter. this is duplicated.

---

<div class="post-metadata">

**Author:** ![patrickwai](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@patrickwai](https://discuss.elastic.co/u/patrickwai)\
**Post date:** [March 19, 2018, 3:18pm UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/4 "2018-03-19T15:18:42Z")

</div>

there are two queries for each time. one for getting quantity without filter. The second for applying to filter. is there any improving suggest on this?

---

<div class="post-metadata">

**Author:** ![patrickwai](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@patrickwai](https://discuss.elastic.co/u/patrickwai)\
**Post date:** [March 19, 2018, 3:20pm UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/5 "2018-03-19T15:20:07Z")

</div>

there is a aggregation api. It can be used to get quantities for each filter item directly ?

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [March 19, 2018, 4:04pm UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/6 "2018-03-19T16:04:23Z")

</div>

> [@patrickwai](#):
>
> there is a aggregation api. It can be used to get quantities for each filter item directly ?

For "quantities", you should take a look at Aggregations:

```
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

```

1°/ You do not have to count yourself, let Elasticsearch do it.  
2°/ Note that it's also possible to run multiple query at one, this way you can run 1, 2, 3 or more  
queries in only one time. Pretty usefull !

```
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-multi-search.html

```

Have fun 😉

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [March 19, 2018, 4:35pm UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/7 "2018-03-19T16:35:52Z")

</div>

> [@patrickwai](#):
>
> there is a aggregation api. It can be used to get quantities for each filter item directly ?

Absolutly, I'll have to read some doc et run some tests queries, but when I'll have understand how does it work, it'll be perfect for what you want to do 😉

---

<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:** [April 16, 2018, 4:36pm UTC](https://discuss.elastic.co/t/how-to-make-a-search-result-which-can-filter-by-options-like-amazon-com/124538/8 "2018-04-16T16:36:03Z")

</div>

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