# Any chance of facets in URI?

**URL:** <https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479>\
**Category:** Elasticsearch\
**Created:** [April 26, 2012, 4:37pm UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479 "2012-04-26T16:37:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian\_mayo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ian_mayo/32/2324_2.png) [@ian\_mayo](https://discuss.elastic.co/u/ian_mayo)\
**Post date:** [April 26, 2012, 4:37pm UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479/1 "2012-04-26T16:37:14Z")

</div>

Hi all,  
I'm aspiring to a nice & tidy REST API for my app. ES forms part of my  
app, but unfortunately the complexity of search is dirtying my lovely API.  
Across the rest of the system I've managed to included all data in the URI

- which both has a functional beauty, but it also offers opportunities for  
cache-ing, either at the server or the client.

In my ES searches, I'm able to encode the search in the URI, using "  
\_search?q=user:kimchy". But, to get the facets that relate to that search  
I'll have to switch to storing the data in a JSON payload.

I imagine it's possible to use a similar encoding to list which facets  
should be present in the results, particularly if the field-name is both  
used to specify which field to select, plus what to call the results object  
in the return payload.

I'd like to find out if there's any chance that facets could be included in  
the URI construct. Is it technically possible?

cheers,  
Ian

---

<div class="post-metadata">

**Author:** ![Benjamin\_Deveze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_deveze/32/1577_2.png) [@Benjamin\_Deveze](https://discuss.elastic.co/u/Benjamin_Deveze)\
**Post date:** [April 26, 2012, 6:10pm UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479/2 "2012-04-26T18:10:08Z")

</div>

Hey Ian,

I have begun working on something that would do exactly that and more.

This is the concept of search templates. Basically you can define a  
template using the full query DSL with variables inside of it. When  
you search you precise which template you want to use and the  
variables in the templates are replaced using variables from the uri.

This approach has a lot of advantages:

- less network transfer for each query which can be quite important if  
you have a lot of complex queries
- it is easier to cache results in cache layers like varnish and you  
can even change your templates without invalidating the cache
- you generally use only a few classes of queries in a common  
application so it is very clear if you define a search template for  
each class of query what queries you are actually using
- you can abstract query complexity from search clients and expose  
precisely the kind of queries and parameters you want. Even if the  
templates are quite complex the exposed search uri are very simple if  
you want to.
- you could decide to restrict queries to only search templates which  
would prevent client to do very costly queries

...

On Thu, Apr 26, 2012 at 6:37 PM, ian mayo [ian@planetmayo.com](mailto:ian@planetmayo.com) wrote:

> Hi all,  
> I'm aspiring to a nice & tidy REST API for my app. ES forms part of my app,  
> but unfortunately the complexity of search is dirtying my lovely API.  
> Across the rest of the system I've managed to included all data in the URI
> 
> - which both has a functional beauty, but it also offers opportunities for  
> cache-ing, either at the server or the client.
> 
> In my ES searches, I'm able to encode the search in the URI, using  
> "\_search?q=user:kimchy". But, to get the facets that relate to that search  
> I'll have to switch to storing the data in a JSON payload.
> 
> I imagine it's possible to use a similar encoding to list which facets  
> should be present in the results, particularly if the field-name is both  
> used to specify which field to select, plus what to call the results object  
> in the return payload.
> 
> I'd like to find out if there's any chance that facets could be included in  
> the URI construct. Is it technically possible?
> 
> cheers,  
> Ian

--  
Benjamin DEVEZE

---

<div class="post-metadata">

**Author:** ![Benjamin\_Deveze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_deveze/32/1577_2.png) [@Benjamin\_Deveze](https://discuss.elastic.co/u/Benjamin_Deveze)\
**Post date:** [April 26, 2012, 6:16pm UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479/3 "2012-04-26T18:16:52Z")

</div>

I have forgotten to add that ES provides index templates, mapping  
templates so for the sake of consistency we need search templates 🙂

On Thu, Apr 26, 2012 at 8:10 PM, Benjamin Devèze  
[benjamin.deveze@gmail.com](mailto:benjamin.deveze@gmail.com) wrote:

> Hey Ian,
> 
> I have begun working on something that would do exactly that and more.
> 
> This is the concept of search templates. Basically you can define a  
> template using the full query DSL with variables inside of it. When  
> you search you precise which template you want to use and the  
> variables in the templates are replaced using variables from the uri.
> 
> This approach has a lot of advantages:
> 
> - less network transfer for each query which can be quite important if  
> you have a lot of complex queries
> - it is easier to cache results in cache layers like varnish and you  
> can even change your templates without invalidating the cache
> - you generally use only a few classes of queries in a common  
> application so it is very clear if you define a search template for  
> each class of query what queries you are actually using
> - you can abstract query complexity from search clients and expose  
> precisely the kind of queries and parameters you want. Even if the  
> templates are quite complex the exposed search uri are very simple if  
> you want to.
> - you could decide to restrict queries to only search templates which  
> would prevent client to do very costly queries
> 
> ...
> 
> On Thu, Apr 26, 2012 at 6:37 PM, ian mayo [ian@planetmayo.com](mailto:ian@planetmayo.com) wrote:
> 
> > Hi all,  
> > I'm aspiring to a nice & tidy REST API for my app. ES forms part of my app,  
> > but unfortunately the complexity of search is dirtying my lovely API.  
> > Across the rest of the system I've managed to included all data in the URI
> > 
> > - which both has a functional beauty, but it also offers opportunities for  
> > cache-ing, either at the server or the client.
> > 
> > In my ES searches, I'm able to encode the search in the URI, using  
> > "\_search?q=user:kimchy". But, to get the facets that relate to that search  
> > I'll have to switch to storing the data in a JSON payload.
> > 
> > I imagine it's possible to use a similar encoding to list which facets  
> > should be present in the results, particularly if the field-name is both  
> > used to specify which field to select, plus what to call the results object  
> > in the return payload.
> > 
> > I'd like to find out if there's any chance that facets could be included in  
> > the URI construct. Is it technically possible?
> > 
> > cheers,  
> > Ian
> 
> --  
> Benjamin DEVEZE

--  
Benjamin DEVEZE

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 29, 2012, 4:26pm UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479/4 "2012-04-29T16:26:37Z")

</div>

There isn't a specific format for it, but, you can pass the actual json  
body in the URI under the source URI parameter.

On Thu, Apr 26, 2012 at 7:37 PM, ian mayo [ian@planetmayo.com](mailto:ian@planetmayo.com) wrote:

> Hi all,  
> I'm aspiring to a nice & tidy REST API for my app. ES forms part of my  
> app, but unfortunately the complexity of search is dirtying my lovely API.  
> Across the rest of the system I've managed to included all data in the URI
> 
> - which both has a functional beauty, but it also offers opportunities for  
> cache-ing, either at the server or the client.
> 
> In my ES searches, I'm able to encode the search in the URI, using "  
> \_search?q=user:kimchy". But, to get the facets that relate to that  
> search I'll have to switch to storing the data in a JSON payload.
> 
> I imagine it's possible to use a similar encoding to list which facets  
> should be present in the results, particularly if the field-name is both  
> used to specify which field to select, plus what to call the results object  
> in the return payload.
> 
> I'd like to find out if there's any chance that facets could be included  
> in the URI construct. Is it technically possible?
> 
> cheers,  
> Ian

---

<div class="post-metadata">

**Author:** ![tfreitas](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@tfreitas](https://discuss.elastic.co/u/tfreitas)\
**Post date:** [August 13, 2012, 4:03am UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479/5 "2012-08-13T04:03:10Z")

</div>

Hi kimcky

can you sample from that?

--

---

<div class="post-metadata">

**Author:** ![Mark\_Waddle\_2](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mark\_Waddle\_2](https://discuss.elastic.co/u/Mark_Waddle_2)\
**Post date:** [August 19, 2012, 10:45pm UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479/6 "2012-08-19T22:45:30Z")

</div>

curl  
'[http://localhost:9200/\_search?pretty=true&fields=\*&source=\{"query":\{"query\_string":\{"query":"\*"\}\](http://localhost:9200/_search?pretty=true&fields=*&source=%5C%7B%22query%22:%5C%7B%22query_string%22:%5C%7B%22query%22:%22*%22%5C%7D%5C)}}'

Although I am not doing it here, I recommend you URL encode the JSON.

On Sunday, August 12, 2012 9:03:10 PM UTC-7, tfreitas wrote:

> Hi kimcky
> 
> can you sample from that?

--

---

<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:** [July 6, 2017, 3:15am UTC](https://discuss.elastic.co/t/any-chance-of-facets-in-uri/7479/7 "2017-07-06T03:15:52Z")

</div>


