# Facet searching using Groovy DSL

**URL:** <https://discuss.elastic.co/t/facet-searching-using-groovy-dsl/7895>\
**Category:** Elasticsearch\
**Created:** [May 29, 2012, 4:19pm UTC](https://discuss.elastic.co/t/facet-searching-using-groovy-dsl/7895 "2012-05-29T16:19:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Smolyak](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Michael\_Smolyak](https://discuss.elastic.co/u/Michael_Smolyak)\
**Post date:** [May 29, 2012, 4:19pm UTC](https://discuss.elastic.co/t/facet-searching-using-groovy-dsl/7895/1 "2012-05-29T16:19:36Z")

</div>

I am trying to figure out the structure of a Groovy DSL for facet search.  
My search should contain term facets and a range facet. I was able to  
construct the search object using the facet builders:

```
    source.facet(new TermsFacetBuilder("type").field("types.type_id"))
    source.facet(new TermsFacetBuilder("state").field("state.state_id"))
    source.facet(new 

```

TermsFacetBuilder("category").field("categories.category\_id"))  
source.facet(new  
RangeFacetBuilder("rating").field("avgRate").addRange(0.0,  
1.0).addRange(1.0, 2.0).addRange(2.0, 3.0).addRange(3.0, 4.0).addRange(4.0,  
5.0))

I also determined what the DSL looks like for the term facets:

```
    def facets = {
        type {
            terms (field : "types.type_id")
        }
        state {
            terms (field : "state.state_id")
        }
        category {
            terms (field : "categories.category_id")
        }
    }

```

Is there a regular way to construct the DSL for a range facet (other than  
trial and error)? The DSL needs to specify that the facet is a range facet,  
provide its name, set the field and add the ranges? Should the  
RangeFacetBuilder API be the guide?

What should the Groovy DSL for a range facet look like?

Thank you,

Michael

---

<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:** [May 29, 2012, 10:27pm UTC](https://discuss.elastic.co/t/facet-searching-using-groovy-dsl/7895/2 "2012-05-29T22:27:16Z")

</div>

The DSL should look similar to the json structure of the range facet, the  
json builder from grails is used in elasticsearch.

On Tue, May 29, 2012 at 6:19 PM, Michael Smolyak \<  
[msmolyak.nextcentury@gmail.com](mailto:msmolyak.nextcentury@gmail.com)\> wrote:

> I am trying to figure out the structure of a Groovy DSL for facet search.  
> My search should contain term facets and a range facet. I was able to  
> construct the search object using the facet builders:
> 
> ```
> source.facet(new TermsFacetBuilder("type").field("types.type_id"))
> source.facet(new
> 
> ```
> 
> TermsFacetBuilder("state").field("state.state\_id"))  
> source.facet(new  
> TermsFacetBuilder("category").field("categories.category\_id"))  
> source.facet(new  
> RangeFacetBuilder("rating").field("avgRate").addRange(0.0,  
> 1.0).addRange(1.0, 2.0).addRange(2.0, 3.0).addRange(3.0, 4.0).addRange(4.0,  
> 5.0))
> 
> I also determined what the DSL looks like for the term facets:
> 
> ```
> def facets = {
> type {
> terms (field : "types.type_id")
> }
> state {
> terms (field : "state.state_id")
> }
> category {
> terms (field : "categories.category_id")
> }
> }
> 
> ```
> 
> Is there a regular way to construct the DSL for a range facet (other than  
> trial and error)? The DSL needs to specify that the facet is a range facet,  
> provide its name, set the field and add the ranges? Should the  
> RangeFacetBuilder API be the guide?
> 
> What should the Groovy DSL for a range facet look like?
> 
> Thank you,
> 
> Michael

---

<div class="post-metadata">

**Author:** ![Stewart\_Morgan](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Stewart\_Morgan](https://discuss.elastic.co/u/Stewart_Morgan)\
**Post date:** [July 14, 2012, 8:54pm UTC](https://discuss.elastic.co/t/facet-searching-using-groovy-dsl/7895/3 "2012-07-14T20:54:18Z")

</div>

Hi there did you manage to get anywhere with this? I've been trying to get  
a facet range search working for a groovy/grails app, but not getting  
anywhere... The DSL I have so far is

facets{  
distance{  
geo\_distance{  
"myfield"(lat:lat\_long[0],lon:lat\_long[1])  
ranges([to:'5'],[from:"5",to:"10"])  
}  
}  
}

but this the outputting....

facets={distance={geo\_distance={myfield={lat=0.12, lon=-1.13},  
ranges={to=5}}}}

On Tuesday, 29 May 2012 17:19:36 UTC+1, Michael Smolyak wrote:

> I am trying to figure out the structure of a Groovy DSL for facet search.  
> My search should contain term facets and a range facet. I was able to  
> construct the search object using the facet builders:
> 
> ```
> source.facet(new TermsFacetBuilder("type").field("types.type_id"))
> source.facet(new 
> 
> ```
> 
> TermsFacetBuilder("state").field("state.state\_id"))  
> source.facet(new  
> TermsFacetBuilder("category").field("categories.category\_id"))  
> source.facet(new  
> RangeFacetBuilder("rating").field("avgRate").addRange(0.0,  
> 1.0).addRange(1.0, 2.0).addRange(2.0, 3.0).addRange(3.0, 4.0).addRange(4.0,  
> 5.0))
> 
> I also determined what the DSL looks like for the term facets:
> 
> ```
> def facets = {
> type {
> terms (field : "types.type_id")
> }
> state {
> terms (field : "state.state_id")
> }
> category {
> terms (field : "categories.category_id")
> }
> }
> 
> ```
> 
> Is there a regular way to construct the DSL for a range facet (other than  
> trial and error)? The DSL needs to specify that the facet is a range facet,  
> provide its name, set the field and add the ranges? Should the  
> RangeFacetBuilder API be the guide?
> 
> What should the Groovy DSL for a range facet look like?
> 
> Thank you,
> 
> Michael

---

<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:20am UTC](https://discuss.elastic.co/t/facet-searching-using-groovy-dsl/7895/4 "2017-07-06T03:20:17Z")

</div>


