|
18 | 18 | require 'elasticsearch/helpers/scroll_helper' |
19 | 19 |
|
20 | 20 | context 'Elasticsearch client helpers' do |
21 | | - let(:index) { 'books' } |
22 | | - let(:body) { { size: 12, query: { match_all: {} } } } |
23 | | - let(:scroll_helper) { Elasticsearch::Helpers::ScrollHelper.new(client, index, body) } |
| 21 | + context 'ScrollHelper' do |
| 22 | + let(:index) { 'books' } |
| 23 | + let(:body) { { size: 12, query: { match_all: {} } } } |
| 24 | + let(:scroll_helper) { Elasticsearch::Helpers::ScrollHelper.new(client, index, body) } |
24 | 25 |
|
25 | | - before do |
26 | | - documents = [ |
27 | | - { index: { _index: index, data: {name: "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561} } }, |
28 | | - { index: { _index: index, data: {name: "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482} } }, |
29 | | - { index: { _index: index, data: {name: "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604} } }, |
30 | | - { index: { _index: index, data: {name: "Dune Messiah", "author": "Frank Herbert", "release_date": "1969-10-15", "page_count": 331} } }, |
31 | | - { index: { _index: index, data: {name: "Children of Dune", "author": "Frank Herbert", "release_date": "1976-04-21", "page_count": 408} } }, |
32 | | - { index: { _index: index, data: {name: "God Emperor of Dune", "author": "Frank Herbert", "release_date": "1981-05-28", "page_count": 454} } }, |
33 | | - { index: { _index: index, data: {name: "Consider Phlebas", "author": "Iain M. Banks", "release_date": "1987-04-23", "page_count": 471} } }, |
34 | | - { index: { _index: index, data: {name: "Pandora's Star", "author": "Peter F. Hamilton", "release_date": "2004-03-02", "page_count": 768} } }, |
35 | | - { index: { _index: index, data: {name: "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585} } }, |
36 | | - { index: { _index: index, data: {name: "A Fire Upon the Deep", "author": "Vernor Vinge", "release_date": "1992-06-01", "page_count": 613} } }, |
37 | | - { index: { _index: index, data: {name: "Ender's Game", "author": "Orson Scott Card", "release_date": "1985-06-01", "page_count": 324} } }, |
38 | | - { index: { _index: index, data: {name: "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328} } }, |
39 | | - { index: { _index: index, data: {name: "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227} } }, |
40 | | - { index: { _index: index, data: {name: "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268} } }, |
41 | | - { index: { _index: index, data: {name: "Foundation", "author": "Isaac Asimov", "release_date": "1951-06-01", "page_count": 224} } }, |
42 | | - { index: { _index: index, data: {name: "The Giver", "author": "Lois Lowry", "release_date": "1993-04-26", "page_count": 208} } }, |
43 | | - { index: { _index: index, data: {name: "Slaughterhouse-Five", "author": "Kurt Vonnegut", "release_date": "1969-06-01", "page_count": 275} } }, |
44 | | - { index: { _index: index, data: {name: "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "release_date": "1979-10-12", "page_count": 180} } }, |
45 | | - { index: { _index: index, data: {name: "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470} } }, |
46 | | - { index: { _index: index, data: {name: "Neuromancer", "author": "William Gibson", "release_date": "1984-07-01", "page_count": 271} } }, |
47 | | - { index: { _index: index, data: {name: "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} } }, |
48 | | - { index: { _index: index, data: {name: "Starship Troopers", "author": "Robert A. Heinlein", "release_date": "1959-12-01", "page_count": 335} } }, |
49 | | - { index: { _index: index, data: {name: "The Left Hand of Darkness", "author": "Ursula K. Le Guin", "release_date": "1969-06-01", "page_count": 304} } }, |
50 | | - { index: { _index: index, data: {name: "The Moon is a Harsh Mistress", "author": "Robert A. Heinlein", "release_date": "1966-04-01", "page_count": 288 } } } |
51 | | - ] |
52 | | - client.bulk(body: documents, refresh: 'wait_for') |
53 | | - end |
54 | | - |
55 | | - after do |
56 | | - client.indices.delete(index: index) |
57 | | - end |
| 26 | + before do |
| 27 | + documents = [ |
| 28 | + { index: { _index: index, data: {name: "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561} } }, |
| 29 | + { index: { _index: index, data: {name: "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482} } }, |
| 30 | + { index: { _index: index, data: {name: "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604} } }, |
| 31 | + { index: { _index: index, data: {name: "Dune Messiah", "author": "Frank Herbert", "release_date": "1969-10-15", "page_count": 331} } }, |
| 32 | + { index: { _index: index, data: {name: "Children of Dune", "author": "Frank Herbert", "release_date": "1976-04-21", "page_count": 408} } }, |
| 33 | + { index: { _index: index, data: {name: "God Emperor of Dune", "author": "Frank Herbert", "release_date": "1981-05-28", "page_count": 454} } }, |
| 34 | + { index: { _index: index, data: {name: "Consider Phlebas", "author": "Iain M. Banks", "release_date": "1987-04-23", "page_count": 471} } }, |
| 35 | + { index: { _index: index, data: {name: "Pandora's Star", "author": "Peter F. Hamilton", "release_date": "2004-03-02", "page_count": 768} } }, |
| 36 | + { index: { _index: index, data: {name: "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585} } }, |
| 37 | + { index: { _index: index, data: {name: "A Fire Upon the Deep", "author": "Vernor Vinge", "release_date": "1992-06-01", "page_count": 613} } }, |
| 38 | + { index: { _index: index, data: {name: "Ender's Game", "author": "Orson Scott Card", "release_date": "1985-06-01", "page_count": 324} } }, |
| 39 | + { index: { _index: index, data: {name: "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328} } }, |
| 40 | + { index: { _index: index, data: {name: "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227} } }, |
| 41 | + { index: { _index: index, data: {name: "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268} } }, |
| 42 | + { index: { _index: index, data: {name: "Foundation", "author": "Isaac Asimov", "release_date": "1951-06-01", "page_count": 224} } }, |
| 43 | + { index: { _index: index, data: {name: "The Giver", "author": "Lois Lowry", "release_date": "1993-04-26", "page_count": 208} } }, |
| 44 | + { index: { _index: index, data: {name: "Slaughterhouse-Five", "author": "Kurt Vonnegut", "release_date": "1969-06-01", "page_count": 275} } }, |
| 45 | + { index: { _index: index, data: {name: "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "release_date": "1979-10-12", "page_count": 180} } }, |
| 46 | + { index: { _index: index, data: {name: "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470} } }, |
| 47 | + { index: { _index: index, data: {name: "Neuromancer", "author": "William Gibson", "release_date": "1984-07-01", "page_count": 271} } }, |
| 48 | + { index: { _index: index, data: {name: "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} } }, |
| 49 | + { index: { _index: index, data: {name: "Starship Troopers", "author": "Robert A. Heinlein", "release_date": "1959-12-01", "page_count": 335} } }, |
| 50 | + { index: { _index: index, data: {name: "The Left Hand of Darkness", "author": "Ursula K. Le Guin", "release_date": "1969-06-01", "page_count": 304} } }, |
| 51 | + { index: { _index: index, data: {name: "The Moon is a Harsh Mistress", "author": "Robert A. Heinlein", "release_date": "1966-04-01", "page_count": 288 } } } |
| 52 | + ] |
| 53 | + client.bulk(body: documents, refresh: 'wait_for') |
| 54 | + end |
58 | 55 |
|
59 | | - it 'instantiates a scroll helper' do |
60 | | - expect(scroll_helper).to be_an_instance_of Elasticsearch::Helpers::ScrollHelper |
61 | | - end |
| 56 | + after do |
| 57 | + client.indices.delete(index: index) |
| 58 | + end |
62 | 59 |
|
63 | | - it 'searches an index' do |
64 | | - my_documents = [] |
65 | | - while !(documents = scroll_helper.results).empty? |
66 | | - my_documents << documents |
| 60 | + it 'instantiates a scroll helper' do |
| 61 | + expect(scroll_helper).to be_an_instance_of Elasticsearch::Helpers::ScrollHelper |
67 | 62 | end |
68 | 63 |
|
69 | | - expect(my_documents.flatten.size).to eq 24 |
70 | | - end |
| 64 | + it 'searches an index' do |
| 65 | + my_documents = [] |
| 66 | + while !(documents = scroll_helper.results).empty? |
| 67 | + my_documents << documents |
| 68 | + end |
71 | 69 |
|
72 | | - it 'uses enumerable' do |
73 | | - count = 0 |
74 | | - scroll_helper.each { |a| count += 1 } |
75 | | - expect(count).to eq 24 |
76 | | - expect(scroll_helper).to respond_to(:count) |
77 | | - expect(scroll_helper).to respond_to(:reject) |
78 | | - expect(scroll_helper).to respond_to(:uniq) |
79 | | - expect(scroll_helper.map { |a| a['_id'] }.uniq.count).to eq 24 |
| 70 | + expect(my_documents.flatten.size).to eq 24 |
| 71 | + end |
| 72 | + |
| 73 | + it 'uses enumerable' do |
| 74 | + count = 0 |
| 75 | + scroll_helper.each { count += 1 } |
| 76 | + expect(count).to eq 24 |
| 77 | + expect(scroll_helper).to respond_to(:count) |
| 78 | + expect(scroll_helper).to respond_to(:reject) |
| 79 | + expect(scroll_helper).to respond_to(:uniq) |
| 80 | + expect(scroll_helper.map { |a| a['_id'] }.uniq.count).to eq 24 |
| 81 | + end |
80 | 82 | end |
81 | 83 | end |
0 commit comments