GithubHelp home page GithubHelp logo

Facet Counts about retire HOT 10 CLOSED

karmi avatar karmi commented on September 15, 2024
Facet Counts

from retire.

Comments (10)

willoughby avatar willoughby commented on September 15, 2024

This may not be a bug but a feature request to enable anding of filters, I will take a look at your code in depth

Thanks

from retire.

karmi avatar karmi commented on September 15, 2024

Hi, could you post a full example, with couple of docs, index creation, query, please? So I can recreate on my side?

Karel

On 17.May, 2011, at 21:59 , willoughby wrote:

When using filtering by facets only the counts do not seem to reflect what the search results are

@s = Tire.search "14" do
query do
string 'title:*'
end
filter :terms, :prices => ["#{price}"] if price
filter :terms, :tags => ["#{tag}"] if tag
facet 'current-tags' do
terms :tags
end
facet 'current-prices' do
terms :prices
end
end

Empty search returns

Tags
1 - 48 (coresponds to "Kitchen")
Prices
1 - 2282
2 - 1455
4 - 258
3 - 138

Filter based on tag 1 apears to select the correct documents but facet counts remain as above. however by editing the query to title :kitchen

the facets look correct

Tags
1 - 48
Prices
1 - 48
2 - 47
4 - 14
3 - 4

Hope this make sense.

Regards

Reply to this email directly or view it on GitHub:
#21

from retire.

willoughby avatar willoughby commented on September 15, 2024

Hi Karel

I have push this as an example https://[email protected]/willoughby/facet_example.git

What I am trying to do is,

From a initial query of title:*
Show all available facets
On selection of a facet (tag:ruby) show the relevant results and facets
Select a different facet (category:one) show results based on tag:ruby AND category:one

The call below does this but can do Tire do this?

Regards
Nath

{"query":
    {"query_string":{"query":"title:*"}},
    "facets" : {
         "tags" : {
              "terms" : {"field" : "tags"},
               "facet_filter" : {
                     "and" : [
                         {"term" : {"tags" : "ruby"}},
                         {"term" : {"category" : "one"}}
                      ]
               }
          },
          "category":{"terms":{"field":"category"},
                "facet_filter" : {
                    "and" : [
                         {"term" : {"tags" : "ruby"}},
                         {"term" : {"category" : "one"}}
                      ]
                  }
            },
           "price":{"terms":{"field":"price"},
                "facet_filter" : {
                      "and" : [
                         {"term" : {"tags" : "ruby"}},
                         {"term" : {"category" : "one"}}
                      ]
                  }
            }
    },"filter":{"terms":{"tags":["ruby"]}},
      "filter":{"terms":{"category":["one"]}}
}

from retire.

karmi avatar karmi commented on September 15, 2024

Hi, sooo, sorry it took so long. Now I understand what you mean, looking at http://www.elasticsearch.org/guide/reference/api/search/filter.html.

I think it should be doable right now, because facet options like global are encoded:

Tire.search 'someindex' do
  query { string "title:*" }

  facet 'tags' do
    terms :tags, :facet_filter => { :and => [ {:term => { :tags => 'ruby }}, :term => { :category => 'one' }  ] }
  end

  # ...
end

I'm writing this from heart, so it may not work as expected. Could you try it?

from retire.

karmi avatar karmi commented on September 15, 2024

Ping, any updates, @willoughby? :)

from retire.

ejlevin1 avatar ejlevin1 commented on September 15, 2024

I was trying to do the same thing that willoughby was and was unable to accomplish this because using the above example, the :facet_filter would be scoped within the :terms instead of under the facet itself. I am new to Tire/Elasticsearch so I may be doing something wrong, but here is what I was able to come up with:

Monkeypatch for Tire::Search::Facet:

  def filter(type, *options)
    @filters ||= []
    @filters << Filter.new(type, *options).to_hash
    self
  end

  def to_hash
    @value.update @options
    @value.update( { :facet_filter => @filters.first.to_hash } ) if @filters && @filters.size == 1
    @value.update( { :facet_filter => { :and => @filters.map { |filter| filter.to_hash } } } ) if  @filters && @filters.size > 1
    { @name => @value }
  end

then I could use it just like the search filter:

filters = []
filters << { :upccode => params[:upccode] } if params[:upccode]

@search = Tire.search( 'product' ) do |s|
s.query do |q|
q.string params[:keyword] if params[:keyword]
end

  #Specify the filters on this search
  filters.each { |filter| s.filter :term, filter }

  [:product_line_id].each do |f|
    s.facet(f.to_s) do |facet|
      facet.terms f
      facet.filter :term, { :upccode => ... }
    end
  end
end

from retire.

willoughby avatar willoughby commented on September 15, 2024

Hey ejlevin1

This might help solve your problem

s = Tire.search "products_#{current_site}" do
      query do
        boolean do
          must { string query}
          must {term :retailer_id, params[:retailer_id]  } if params[:retailer_id].present?
          must {term :price, params[:price] } if params[:price].present?
        end

      end
       facet "retailer_id" do
          terms :retailer_id, :size=> 1000
       end  
       facet "price" do
          terms :price
       end
     size(20)
     from(params[:page] || 0)
    end

from retire.

agungyuliaji avatar agungyuliaji commented on September 15, 2024
def self.power_facets(domain_id, anchor_words, tag)
    tire.search do 
      query do
        boolean do
          must { string "domain_id:#{domain_id}" }
        end
      end

      facet "market_ranks" do
        terms :market_rank, :facet_filter => { :and => [ {:term => { :tags => tag }}, :term => { :anchor_words => anchor_words }  ] } 
      end

    end
  end

code above is does not work

i want to use filter facets, http://www.elasticsearch.org/guide/reference/api/search/facets/filter-facet.html
how can I do that with tire?

from retire.

karmi avatar karmi commented on September 15, 2024

@agungyuliaji https://github.com/karmi/tire/blob/master/test/integration/facets_test.rb#L296-L305

from retire.

agungyuliaji avatar agungyuliaji commented on September 15, 2024

thanks! :)

but the results were not as I wanted
{"market_ranks"=>{"_type"=>"filter", "count"=>198}}

i want like this :
{"market_ranks"=>{"_type"=>"terms", "missing"=>0, "total"=>198, "other"=>0, "terms"=>[{"term"=>2, "count"=>134}, {"term"=>1, "count"=>38}, {"term"=>3, "count"=>10}, {"term"=>0, "count"=>10}, {"term"=>6, "count"=>2}, {"term"=>5, "count"=>2}, {"term"=>4, "count"=>2}]}}

now I have new a code :

  def self.power_facets(domain_id, anchor_words, tag)
    tire.search do 
      query do
        boolean do
          must { string "domain_id:#{domain_id}" }
        end
      end

      facet "market_ranks" do
        terms :market_rank
        facet_filter :term, tags: tag 
        facet_filter :term, anchor_words: anchor_words
      end

    end
  end

But it seems to apply only the last filter. But I need both of them.

from retire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.