stretcher 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/stretcher/index.rb +17 -12
- data/lib/stretcher/index_type.rb +1 -0
- data/lib/stretcher/server.rb +13 -12
- data/lib/stretcher/version.rb +1 -1
- data/spec/lib/stretcher_index_spec.rb +6 -3
- metadata +2 -2
data/README.md
CHANGED
@@ -63,7 +63,7 @@ Stretcher::Server.with_server('http://localhost:9200') {|srv|
|
|
63
63
|
server.index(:foo).msearch([{query: {match_all: {}}}])
|
64
64
|
# => Returns an array of Stretcher::SearchResults
|
65
65
|
# Across multiple indexes
|
66
|
-
server.msearch([{index:
|
66
|
+
server.msearch([{index: :foo}, {query: {match_all: {}}}])
|
67
67
|
# => Returns an array of Stretcher::SearchResults
|
68
68
|
```
|
69
69
|
|
data/lib/stretcher/index.rb
CHANGED
@@ -15,21 +15,21 @@ module Stretcher
|
|
15
15
|
# Optionally takes a block, which will be passed a single arg with the Index obj
|
16
16
|
# The block syntax returns the evaluated value of the block
|
17
17
|
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# my_index.index(:foo) # => #<Stretcher::Index ...>
|
21
|
-
#
|
22
|
-
# my_index.index(:foo) {|idx| 1+1} # => 2
|
18
|
+
# my_index.index(:foo) # => #<Stretcher::Index ...>
|
19
|
+
# my_index.index(:foo) {|idx| 1+1} # => 2
|
23
20
|
def type(name, &block)
|
24
21
|
t = IndexType.new(self, name)
|
25
22
|
block ? block.call(t) : t
|
26
23
|
end
|
27
24
|
|
28
25
|
# Given a hash of documents, will bulk index
|
26
|
+
#
|
27
|
+
# docs = [{"_type" => "tweet", "_id" => 91011, "text" => "Bulked"}]
|
28
|
+
# server.index(:foo).bulk_index(docs)
|
29
29
|
def bulk_index(documents)
|
30
30
|
@server.bulk documents.reduce("") {|post_data, d_raw|
|
31
31
|
d = Hashie::Mash.new(d_raw)
|
32
|
-
action_meta = {"index" => {"_index" => name, "_type" => d["_type"], "_id" => d["id"]}}
|
32
|
+
action_meta = {"index" => {"_index" => name, "_type" => d["_type"], "_id" => d["_id"] || d["id"]}}
|
33
33
|
action_meta["index"]["_parent"] = d["_parent"] if d["_parent"]
|
34
34
|
post_data << (action_meta.to_json + "\n")
|
35
35
|
post_data << (d.to_json + "\n")
|
@@ -69,12 +69,13 @@ module Stretcher
|
|
69
69
|
end
|
70
70
|
|
71
71
|
# Issues a search with the given query opts and body, both should be hashes
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
72
|
+
#
|
73
|
+
# res = server.index('foo').search({size: 12}, {query: {match_all: {}}})
|
74
|
+
# es.class # Stretcher::SearchResults
|
75
|
+
# res.total # => 1
|
76
|
+
# res.facets # => nil
|
77
|
+
# res.results # => [#<Hashie::Mash _id="123" text="Hello">]
|
78
|
+
# res.raw # => #<Hashie::Mash ...> Raw JSON from the search
|
78
79
|
def search(query_opts={}, body=nil)
|
79
80
|
uri = path_uri('/_search?' + Util.querify(query_opts))
|
80
81
|
logger.info { "Stretcher Search: curl -XGET #{uri} -d '#{body.to_json}'" }
|
@@ -88,6 +89,9 @@ module Stretcher
|
|
88
89
|
# This deviates slightly from the official API in that *ONLY*
|
89
90
|
# queries are requried, the empty {} preceding them are not
|
90
91
|
# See: http://www.elasticsearch.org/guide/reference/api/multi-search.html
|
92
|
+
#
|
93
|
+
# server.index(:foo).msearch([{query: {match_all: {}}}])
|
94
|
+
# # => Returns an array of Stretcher::SearchResults
|
91
95
|
def msearch(queries=[])
|
92
96
|
raise ArgumentError, "msearch takes an array!" unless queries.is_a?(Array)
|
93
97
|
req_body = queries.reduce([]) {|acc,q|
|
@@ -98,6 +102,7 @@ module Stretcher
|
|
98
102
|
@server.msearch req_body
|
99
103
|
end
|
100
104
|
|
105
|
+
# Full path to this index
|
101
106
|
def path_uri(path="/")
|
102
107
|
@server.path_uri("/#{name}") + path.to_s
|
103
108
|
end
|
data/lib/stretcher/index_type.rb
CHANGED
data/lib/stretcher/server.rb
CHANGED
@@ -11,10 +11,10 @@ module Stretcher
|
|
11
11
|
end
|
12
12
|
|
13
13
|
# Represents a Server context in elastic search.
|
14
|
-
# Use +
|
14
|
+
# Use +with_server+ when you want to use the block syntax.
|
15
15
|
# The options hash takes an optional instance of Logger under :logger.
|
16
16
|
#
|
17
|
-
#
|
17
|
+
# server = Stretcher::Server.new('http://localhost:9200')
|
18
18
|
def initialize(uri='http://localhost:9200', options={})
|
19
19
|
@uri = uri
|
20
20
|
|
@@ -46,11 +46,8 @@ module Stretcher
|
|
46
46
|
# Optionally takes a block, which will be passed a single arg with the Index obj
|
47
47
|
# The block syntax returns the evaluated value of the block
|
48
48
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
# my_server.index(:foo) # => #<Stretcher::Index ...>
|
52
|
-
#
|
53
|
-
# my_server.index(:foo) {|idx| 1+1} # => 2
|
49
|
+
# my_server.index(:foo) # => #<Stretcher::Index ...>
|
50
|
+
# my_server.index(:foo) {|idx| 1+1} # => 2
|
54
51
|
def index(name, &block)
|
55
52
|
idx = Index.new(self, name, logger: logger)
|
56
53
|
block ? block.call(idx) : idx
|
@@ -71,10 +68,13 @@ module Stretcher
|
|
71
68
|
# Takes an array of msearch data as per
|
72
69
|
# http://www.elasticsearch.org/guide/reference/api/multi-search.html
|
73
70
|
# Should look something like:
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
71
|
+
# data = [
|
72
|
+
# {"index" : "test"}
|
73
|
+
# {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
|
74
|
+
# {"index" : "test", "search_type" : "count"}
|
75
|
+
# {"query" : {"match_all" : {}}}
|
76
|
+
# ]
|
77
|
+
# server.msearch(data)
|
78
78
|
def msearch(body=[])
|
79
79
|
raise ArgumentError, "msearch takes an array!" unless body.is_a?(Array)
|
80
80
|
fmt_body = body.map(&:to_json).join("\n") + "\n"
|
@@ -97,6 +97,7 @@ module Stretcher
|
|
97
97
|
}
|
98
98
|
end
|
99
99
|
|
100
|
+
# Full path to the server root dir
|
100
101
|
def path_uri(path="/")
|
101
102
|
@uri.to_s + path.to_s
|
102
103
|
end
|
@@ -110,7 +111,7 @@ module Stretcher
|
|
110
111
|
# Elastic search does mostly deal with JSON
|
111
112
|
req.headers["Content-Type"] = 'application/json'
|
112
113
|
block.call(req)
|
113
|
-
|
114
|
+
end
|
114
115
|
else
|
115
116
|
http.send(method, *args)
|
116
117
|
end
|
data/lib/stretcher/version.rb
CHANGED
@@ -5,9 +5,9 @@ describe Stretcher::Index do
|
|
5
5
|
let(:index) { server.index('foo') }
|
6
6
|
let(:corpus) {
|
7
7
|
[
|
8
|
-
{text: "Foo", "_type" => 'tweet'},
|
9
|
-
{text: "Bar", "_type" => 'tweet'},
|
10
|
-
{text: "Baz", "_type" => 'tweet'}
|
8
|
+
{text: "Foo", "_type" => 'tweet', "_id" => 'fooid'},
|
9
|
+
{text: "Bar", "_type" => 'tweet', "_id" => 'barid'},
|
10
|
+
{text: "Baz", "_type" => 'tweet', "id" => 'bazid'} # Note we support both _id and id
|
11
11
|
]
|
12
12
|
}
|
13
13
|
|
@@ -52,6 +52,9 @@ describe Stretcher::Index do
|
|
52
52
|
|
53
53
|
it "should bulk index documents properly" do
|
54
54
|
seed_corpus
|
55
|
+
corpus.each {|doc|
|
56
|
+
index.type(doc["_type"]).get(doc["_id"] || doc["id"]).text.should == doc[:text]
|
57
|
+
}
|
55
58
|
end
|
56
59
|
|
57
60
|
# TODO: Actually use two indexes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stretcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|