Facets aren't working


(Misha Tatinets) #1

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

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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/075e6fd9-fafa-4b09-b049-a36cb2827830%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    (Karel Minařík-2) #2

    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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/97eafa5e-79c2-4ae9-b6d8-b5426a9725a5%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    (Misha Tatinets) #3

    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' -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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/53b804d4-d2c4-4b3e-b206-204a45c69043%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    (Ivan Brusic) #4

    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.

    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 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' -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.
    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?utm_medium=email&utm_source=footer
    .

    For more options, visit 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.
    To view this discussion on the web visit 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.


    (Misha Tatinets) #5

    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.

    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
    <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' -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 <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?utm_medium=email&utm_source=footer
    .

    For more options, visit 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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/45e8900c-0fe8-4e94-a4d2-9a1c092408bb%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    (Karel Minařík-2) #6

    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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e6d21d13-a97e-4d12-816f-6bb65e73fd61%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    (Misha Tatinets) #7

    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 :slight_smile:

    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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/26770952-630d-4f2f-95a9-05e758a4bdb4%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    (system) #8