Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[FACETS] Added a "query" facet, allowing highly flexible faceting

  • Loading branch information...
commit 16d856ecd768de72b51544d5fc43a1570bce0145 1 parent d91afd6
@karmi authored
View
1  examples/tire-dsl.rb
@@ -582,6 +582,7 @@ def self.search
# * [date](http://www.elasticsearch.org/guide/reference/api/search/facets/date-histogram-facet.html)
# * [range](http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html)
# * [histogram](http://www.elasticsearch.org/guide/reference/api/search/facets/histogram-facet.html)
+# * [query](http://www.elasticsearch.org/guide/reference/api/search/facets/query-facet.html)
# We have seen that _ElasticSearch_ facets enable us to fetch complex aggregations from our data.
#
View
4 lib/tire/search/facet.rb
@@ -37,6 +37,10 @@ def histogram(field, options={})
self
end
+ def query(&block)
+ @value = { :query => Query.new(&block).to_hash }
+ end
+
def to_json
to_hash.to_json
end
View
2  test/fixtures/articles/1.json
@@ -1 +1 @@
-{"title" : "One", "tags" : ["ruby"], "published_on" : "2011-01-01", "words" : 125}
+{"title" : "One", "tags" : ["ruby"], "published_on" : "2011-01-01", "words" : 125, "draft" : true}
View
48 test/integration/facets_test.rb
@@ -77,6 +77,7 @@ class FacetsIntegrationTest < Test::Unit::TestCase
end
context "histogram" do
+
should "return aggregated values for all results" do
s = Tire.search('articles-test') do
query { all }
@@ -91,6 +92,53 @@ class FacetsIntegrationTest < Test::Unit::TestCase
assert_equal({"key" => 200, "count" => 2}, facets.entries[1], facets.inspect)
assert_equal({"key" => 300, "count" => 1}, facets.entries[2], facets.inspect)
end
+
+ end
+
+ context "query facets" do
+
+ should "return aggregated values for a string query" do
+ s = Tire.search('articles-test') do
+ facet 'tees' do
+ query { string 'T*' }
+ end
+ end
+
+ count = s.results.facets['tees']['count']
+ assert_equal 2, count, s.results.facets['tees'].inspect
+ end
+
+ should "return aggregated values for _exists_ string query" do
+ s = Tire.search('articles-test') do
+ facet 'drafts' do
+ query { string '_exists_:draft' }
+ end
+ end
+
+ count = s.results.facets['drafts']['count']
+ assert_equal 1, count, s.results.facets['drafts'].inspect
+ end
+
+ should "return aggregated values for a terms query" do
+ s = Tire.search('articles-test') do
+ facet 'friends' do
+ query { terms :tags, ['ruby', 'python'] }
+ end
+ end
+
+ count = s.results.facets['friends']['count']
+ assert_equal 2, count, s.results.facets['friends'].inspect
+
+ s = Tire.search('articles-test') do
+ facet 'friends' do
+ query { terms :tags, ['ruby', 'python'], :minimum_match => 2 }
+ end
+ end
+
+ count = s.results.facets['friends']['count']
+ assert_equal 1, count, s.results.facets['friends'].inspect
+ end
+
end
end
View
9 test/unit/search_facet_test.rb
@@ -81,6 +81,15 @@ class FacetTest < Test::Unit::TestCase
end
end
+ context "query facet" do
+ should "encode facet options" do
+ f = Facet.new('q_facet') do
+ query { string '_exists_:foo' }
+ end
+ assert_equal({ :q_facet => { :query => { :query_string => { :query => '_exists_:foo' } } } }.to_json, f.to_json)
+ end
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.