# Facets aren't working

**URL:** <https://discuss.elastic.co/t/facets-arent-working/18005>\
**Category:** Elasticsearch\
**Created:** [June 10, 2014, 6:25am UTC](https://discuss.elastic.co/t/facets-arent-working/18005 "2014-06-10T06:25:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Misha\_Tatinets](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/misha_tatinets/32/1478_2.png) [@Misha\_Tatinets](https://discuss.elastic.co/u/Misha_Tatinets)\
**Post date:** [June 10, 2014, 6:25am UTC](https://discuss.elastic.co/t/facets-arent-working/18005/1 "2014-06-10T06:25:28Z")

</div>

I have rails app where I try to use both elasticsearch-rails and  
elasticsearch-model gems.  
I'm getting results but facets aren't working. I wondering if anyone can  
pinpoint on what I am doing wrong.

Here is my code that is written but example from elasticsearch team

> **[elastic/elasticsearch-rails](https://github.com/elastic/elasticsearch-rails/tree/templates/elasticsearch-rails/lib/rails/templates)**
>
> elasticsearch-rails - Elasticsearch integrations for ActiveModel/Record and Ruby on Rails

View

 \<% @search.response['facets']['categories']['terms'].each do |c| %\> - \<%= link\_to companies\_path(params.merge(c: c['term'])), class: "list-group-item#{' active' if params[:c] == c['term']}" do c['term'].html\_safe + content\_tag(:small, c['count'], class: 'badge').html\_safe end %\> 
 \<% end %\>

```
        </div>

      </div>

```

module SearchCompanies  
extend ActiveSupport::Concern  
included do  
include Elasticsearch::Model

mapping do  
indexes :name, :type =\> 'string', :boost =\> 10  
indexes :made\_in\_usa  
indexes :categories do  
indexes :id, :type =\> 'string'  
indexes :name, :type =\> 'string', analyzer: 'keyword'  
end  
end

def as\_indexed\_json(options={})  
self.as\_json(  
include: { categories:{only:[:name, :id]}})  
end

def self.search(query, options={})

```
__set_filters = lambda do |key, f|

  @search_definition[:filter][:and] ||= []
  @search_definition[:filter][:and] |= [f]

  @search_definition[:facets][key.to_sym][:facet_filter][:and] ||= []
  @search_definition[:facets][key.to_sym][:facet_filter][:and] |= [f]
end

@search_definition = {
    query: {},
    filter: {},

    facets: {
        categories: {
            terms: {
                field: 'categories.name'
            },
            facet_filter: {}
        },
        made_in_usa: {
            terms: {
                field: 'made_in_usa'
            },
            facet_filter: {}
        }
    }
}

unless query.blank?
  @search_definition[:query] = {
      bool: {
          should: [
              { multi_match: {
                  query: query,
                  fields: ['name'],
                  operator: 'and'
              }
              }
          ]
      }
  }
else
  @search_definition[:query] = { match_all: {} }
  @search_definition[:sort] = { identification: 'desc' }
end

if options[:categories]
  f = { term: { 'categories.name' => options[:categories] } }

  __set_filters.(:made_in_usa, f)
end

if options[:made_in_usa]
  f = { term: { made_in_usa: options[:made_in_usa] } }

  __set_filters.(:categories, f)
end

if options[:sort]
  @search_definition[:sort] = { options[:sort] => 'desc' }
  @search_definition[:track_scores] = true
end

__elasticsearch__.search(@search_definition)

```

end  
end  
end

Controller  
def search  
params[:query].present?  
@search = Company.search params[:query]  
@companies = @search.results.to\_a  
respond\_to do |format|  
format.html { @companies }  
format.json { render :json =\> @companies}  
end  
end

My maping

{  
"companies" : {  
"mappings" : {  
"company" : {  
"properties" : {  
"categories" : {  
"properties" : {  
"id" : {  
"type" : "long"  
},  
"name" : {  
"type" : "string"  
}  
}  
},  
"id" : {  
"type" : "long"  
},  
"made\_in\_usa" : {  
"type" : "boolean"  
},  
"name" : {  
"type" : "string"  
}  
}  
}  
}

If anyone can see obvious error would you mind pinpoint it.

Thanks

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/075e6fd9-fafa-4b09-b049-a36cb2827830%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/075e6fd9-fafa-4b09-b049-a36cb2827830%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Karel\_Minarik\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karel_minarik_2/32/1078_2.png) [@Karel\_Minarik\_2](https://discuss.elastic.co/u/Karel_Minarik_2)\
**Post date:** [June 11, 2014, 5:19pm UTC](https://discuss.elastic.co/t/facets-arent-working/18005/2 "2014-06-11T17:19:12Z")

</div>

> I'm getting results but facets aren't working

Can you specify in a bit more detail how facet's "aren't working" (i.e.  
what do you want to achieve, what is expected, etc)? Since you base your  
code on the provided Rails templates, the code should be easily  
translatable to your use case.

Karel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/97eafa5e-79c2-4ae9-b6d8-b5426a9725a5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/97eafa5e-79c2-4ae9-b6d8-b5426a9725a5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Misha\_Tatinets](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/misha_tatinets/32/1478_2.png) [@Misha\_Tatinets](https://discuss.elastic.co/u/Misha_Tatinets)\
**Post date:** [June 11, 2014, 9:31pm UTC](https://discuss.elastic.co/t/facets-arent-working/18005/3 "2014-06-11T21:31:50Z")

</div>

Hi Karel,  
Thanks for replying.

What I mean by "facets aren't working", that I can't to filter my results.

When I submit query I see in my log next :  
curl -X GET '[http://localhost:9200/companies/company/\_search?pretty](http://localhost:9200/companies/company/_search?pretty)' -d  
'{"query":{"bool":{"should":[{"multi\_match":{"query":"3m","fields":["name"],"operator":"and"}}]}},"filter":{},"facets":{"categories":{"terms":{"field":"categories.name"},"facet\_filter":{}},"made\_in\_usa":{"terms":{"field":"made\_in\_usa"},"facet\_filter":{}}}}'

That gives me list of the records.

But when I try filter results, nothing is happening. Line in the log are  
identical, although rails log shows me that params "c" and "query" are  
passing through. Url also reflecting that params submitted. That where I am  
stuck.

My assumption that this is more rails part issue than ES, but I try to use  
your template and I don't understand what I am missing.

Could you please give more direction how I can fix it or more examples so I  
will find my error. I would appreciate any type of your help.

Thank you so much Karel.

On Wednesday, June 11, 2014 11:19:12 AM UTC-6, Karel Minařík wrote:

> I'm getting results but facets aren't working
> 
> > 
> 
> Can you specify in a bit more detail how facet's "aren't working" (i.e.  
> what do you want to achieve, what is expected, etc)? Since you base your  
> code on the provided Rails templates, the code should be easily  
> translatable to your use case.
> 
> Karel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [June 11, 2014, 9:40pm UTC](https://discuss.elastic.co/t/facets-arent-working/18005/4 "2014-06-11T21:40:33Z")

</div>

Can you provide an example where you apply a filter?

Keep in mind that if you are using the post filter (the 'filter' in your  
example), then the facets will not be affected by the filter. You must  
either use a filtered query or use the facet filters.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--  
Ivan

On Wed, Jun 11, 2014 at 2:31 PM, Misha Tatinets [mishatatinets@gmail.com](mailto:mishatatinets@gmail.com)  
wrote:

> Hi Karel,  
> Thanks for replying.
> 
> What I mean by "facets aren't working", that I can't to filter my results.
> 
> When I submit query I see in my log next :  
> curl -X GET '[http://localhost:9200/companies/company/\_search?pretty](http://localhost:9200/companies/company/_search?pretty)' -d  
> '{"query":{"bool":{"should":[{"multi\_match":{"query":"3m","fields":["name"],"operator":"and"}}]}},"filter":{},"facets":{"categories":{"terms":{"field":"  
> categories.name  
> "},"facet\_filter":{}},"made\_in\_usa":{"terms":{"field":"made\_in\_usa"},"facet\_filter":{}}}}'
> 
> That gives me list of the records.
> 
> But when I try filter results, nothing is happening. Line in the log are  
> identical, although rails log shows me that params "c" and "query" are  
> passing through. Url also reflecting that params submitted. That where I am  
> stuck.
> 
> My assumption that this is more rails part issue than ES, but I try to use  
> your template and I don't understand what I am missing.
> 
> Could you please give more direction how I can fix it or more examples so  
> I will find my error. I would appreciate any type of your help.
> 
> Thank you so much Karel.
> 
> On Wednesday, June 11, 2014 11:19:12 AM UTC-6, Karel Minařík wrote:
> 
> > I'm getting results but facets aren't working
> > 
> > > 
> > 
> > Can you specify in a bit more detail how facet's "aren't working" (i.e.  
> > what do you want to achieve, what is expected, etc)? Since you base your  
> > code on the provided Rails templates, the code should be easily  
> > translatable to your use case.
> > 
> > Karel
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBS1qq48VbLTkGhgKNkpPSLfSfy%3Dv%3DJ\_2cHmQv%2BsZY4Bw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBS1qq48VbLTkGhgKNkpPSLfSfy%3Dv%3DJ_2cHmQv%2BsZY4Bw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Misha\_Tatinets](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/misha_tatinets/32/1478_2.png) [@Misha\_Tatinets](https://discuss.elastic.co/u/Misha_Tatinets)\
**Post date:** [June 11, 2014, 10:38pm UTC](https://discuss.elastic.co/t/facets-arent-working/18005/5 "2014-06-11T22:38:12Z")

</div>

Ivan, I think I reply to author instead to all. Sorry

On Wednesday, June 11, 2014 3:40:41 PM UTC-6, Ivan Brusic wrote:

> Can you provide an example where you apply a filter?
> 
> Keep in mind that if you are using the post filter (the 'filter' in your  
> example), then the facets will not be affected by the filter. You must  
> either use a filtered query or use the facet filters.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html#search-request-post-filter)
> 
> --  
> Ivan
> 
> On Wed, Jun 11, 2014 at 2:31 PM, Misha Tatinets \<[mishat...@gmail.com](mailto:mishat...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Hi Karel,  
> > Thanks for replying.
> > 
> > What I mean by "facets aren't working", that I can't to filter my results.
> > 
> > When I submit query I see in my log next :  
> > curl -X GET '[http://localhost:9200/companies/company/\_search?pretty](http://localhost:9200/companies/company/_search?pretty)' -d  
> > '{"query":{"bool":{"should":[{"multi\_match":{"query":"3m","fields":["name"],"operator":"and"}}]}},"filter":{},"facets":{"categories":{"terms":{"field":"  
> > categories.name  
> > "},"facet\_filter":{}},"made\_in\_usa":{"terms":{"field":"made\_in\_usa"},"facet\_filter":{}}}}'
> > 
> > That gives me list of the records.
> > 
> > But when I try filter results, nothing is happening. Line in the log are  
> > identical, although rails log shows me that params "c" and "query" are  
> > passing through. Url also reflecting that params submitted. That where I am  
> > stuck.
> > 
> > My assumption that this is more rails part issue than ES, but I try to  
> > use your template and I don't understand what I am missing.
> > 
> > Could you please give more direction how I can fix it or more examples so  
> > I will find my error. I would appreciate any type of your help.
> > 
> > Thank you so much Karel.
> > 
> > On Wednesday, June 11, 2014 11:19:12 AM UTC-6, Karel Minařík wrote:
> > 
> > > I'm getting results but facets aren't working
> > > 
> > > > 
> > > 
> > > Can you specify in a bit more detail how facet's "aren't working" (i.e.  
> > > what do you want to achieve, what is expected, etc)? Since you base your  
> > > code on the provided Rails templates, the code should be easily  
> > > translatable to your use case.
> > > 
> > > Karel
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/45e8900c-0fe8-4e94-a4d2-9a1c092408bb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45e8900c-0fe8-4e94-a4d2-9a1c092408bb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Karel\_Minarik\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karel_minarik_2/32/1078_2.png) [@Karel\_Minarik\_2](https://discuss.elastic.co/u/Karel_Minarik_2)\
**Post date:** [June 12, 2014, 6:17am UTC](https://discuss.elastic.co/t/facets-arent-working/18005/6 "2014-06-12T06:17:43Z")

</div>

So, are you actually passing the `c` URL parameter to the search method?  
Notice the code: `if options[:categories]` in your search method.

But you don't seem to be passing just `params[:query]` in your controller: Company.search  
params[:query]

Karel

What I mean by "facets aren't working", that I can't to filter my results.

> > > But when I try filter results, nothing is happening. Line in the log are  
> > > identical, although rails log shows me that params "c" and "query" are  
> > > passing through. Url also reflecting that params submitted. That where I am  
> > > stuck.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e6d21d13-a97e-4d12-816f-6bb65e73fd61%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e6d21d13-a97e-4d12-816f-6bb65e73fd61%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Misha\_Tatinets](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/misha_tatinets/32/1478_2.png) [@Misha\_Tatinets](https://discuss.elastic.co/u/Misha_Tatinets)\
**Post date:** [June 19, 2014, 1:54am UTC](https://discuss.elastic.co/t/facets-arent-working/18005/7 "2014-06-19T01:54:20Z")

</div>

Thank you so much Karel, you were right I didn't pass options categories.

I added in my controller

def search  
options = {  
categories: params[:categories]  
}  
params[:query].present?  
@companies = Company.search(params[:query],  
options).page(params[:page]).per(10).results  
end

Now everything work perfectly.

Thanks again 🙂

On Thursday, June 12, 2014 12:17:43 AM UTC-6, Karel Minařík wrote:

> So, are you actually passing the `c` URL parameter to the search method?  
> Notice the code: `if options[:categories]` in your search method.
> 
> But you don't seem to be passing just `params[:query]` in your controller: Company.search  
> params[:query]
> 
> Karel
> 
> What I mean by "facets aren't working", that I can't to filter my results.
> 
> > > > But when I try filter results, nothing is happening. Line in the log  
> > > > are identical, although rails log shows me that params "c" and "query" are  
> > > > passing through. Url also reflecting that params submitted. That where I am  
> > > > stuck.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/26770952-630d-4f2f-95a9-05e758a4bdb4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/26770952-630d-4f2f-95a9-05e758a4bdb4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:21am UTC](https://discuss.elastic.co/t/facets-arent-working/18005/8 "2017-07-06T01:21:24Z")

</div>


