redisearch-rb 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/redisearch.rb +36 -7
- data/lib/redisearch/version.rb +1 -1
- data/test/redisearch_test.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9df8fe8e4d7c7a3624eee798a3a495296e244a1c
|
4
|
+
data.tar.gz: 704632f558f90028067c76ca1ee994ea6d08d70e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9cb8486726435e3832f6b598e9cd8a3addde0623c5371db1d5bca9641d67d0189d2ca4c180aaf3ff53c2c63450930b00536b520668fcbb31e2ab7e7994f6275
|
7
|
+
data.tar.gz: 6a4d206b73dd350c6a240b805cbf82dcde7ca536492815b2ff6719a5b617ee73ed191e625bdadbfa68ac1cc3cd09e617c47a9a7c2889db3bc77c2c3fac074d34
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ http://redisearch.io/
|
|
11
11
|
First of all, you need to install RediSearch, if you haven't yet:
|
12
12
|
|
13
13
|
1. Install Redis 4.0.1 or highger https://github.com/antirez/redis/releases/tag/4.0.1
|
14
|
-
2. Install RediSearch 1.0
|
14
|
+
2. Install RediSearch 1.2.0 or higher http://redisearch.io/Quick_Start/
|
15
15
|
3. Edit your `redis.conf` file and add a `loadmodule` directive to load the RediSearch module built in the step 2.
|
16
16
|
|
17
17
|
To install this gem, add this line to your application's Gemfile:
|
@@ -43,7 +43,7 @@ In order to run the tests, it's necessary to set some env variables (see .env.ex
|
|
43
43
|
```ruby
|
44
44
|
require 'redisearch-rb'
|
45
45
|
|
46
|
-
redis = Redis.new(REDIS_URL)
|
46
|
+
redis = Redis.new(url: REDIS_URL)
|
47
47
|
redisearch_client = RediSearch.new('test_idx', redis)
|
48
48
|
|
49
49
|
schema = ['title', 'TEXT', 'WEIGHT', '2.0',
|
data/lib/redisearch.rb
CHANGED
@@ -14,6 +14,7 @@ class RediSearch
|
|
14
14
|
|
15
15
|
OPTIONS_FLAGS = {
|
16
16
|
add: [:nosave, :replace, :partial],
|
17
|
+
addhash: [:replace],
|
17
18
|
create: [:nooffsets, :nofreqs, :nohl, :nofields],
|
18
19
|
del: [:dd],
|
19
20
|
drop: [:keepdocs],
|
@@ -26,6 +27,7 @@ class RediSearch
|
|
26
27
|
# { limit: ['0', '50'], sortby: ['year', 'desc'], return: ['2', 'title', 'year'] }
|
27
28
|
OPTIONS_PARAMS = {
|
28
29
|
add: [:language, :payload],
|
30
|
+
addhash: [:language],
|
29
31
|
create: [:stopwords],
|
30
32
|
search: [:filter, :return, :infields, :inkeys, :slop, :scorer, :sortby, :limit, :payload],
|
31
33
|
sugget: [:max],
|
@@ -71,6 +73,7 @@ class RediSearch
|
|
71
73
|
#
|
72
74
|
# @param [String] doc_id id assigned to the document
|
73
75
|
# @param [Array] fields name-value pairs to be indexed
|
76
|
+
# @param [Float] weight asigned by the user to the document
|
74
77
|
# @param [Hash] opts optional parameters
|
75
78
|
# Example:
|
76
79
|
#
|
@@ -79,8 +82,8 @@ class RediSearch
|
|
79
82
|
#
|
80
83
|
# See http://redisearch.io/Commands/#ftadd
|
81
84
|
# @return [String] "OK" on success
|
82
|
-
def add_doc(doc_id, fields, opts = {})
|
83
|
-
call(ft_add(doc_id, fields, opts))
|
85
|
+
def add_doc(doc_id, fields, opts = {}, weight = nil)
|
86
|
+
call(ft_add(doc_id, fields, opts, weight))
|
84
87
|
end
|
85
88
|
|
86
89
|
# Add a set of docs to the index. Uses redis `multi` to make a single bulk insert.
|
@@ -90,14 +93,30 @@ class RediSearch
|
|
90
93
|
# Example:
|
91
94
|
#
|
92
95
|
# redisearch = RediSearch.new('my_idx')
|
93
|
-
# docs = [['id_1', ['title', 'Lost in translation', 'director', 'Sofia Coppola'],
|
94
|
-
# ['id_2', ['title', 'Ex Machina', 'director', 'Alex Garland']]
|
96
|
+
# docs = [['id_1', ['title', 'Lost in translation', 'director', 'Sofia Coppola'], 0.75],
|
97
|
+
# ['id_2', ['title', 'Ex Machina', 'director', 'Alex Garland'], 0.95]
|
95
98
|
# redisearch.add_docs(docs)
|
96
99
|
#
|
97
100
|
# See http://redisearch.io/Commands/#ftadd
|
98
101
|
# @return [String] "OK" on success
|
99
102
|
def add_docs(docs, opts = {})
|
100
|
-
docs.
|
103
|
+
docs.map { |doc_id, fields, weight| call(ft_add(doc_id, fields, opts, weight))}
|
104
|
+
end
|
105
|
+
|
106
|
+
# Adds a document to the index from an existing HASH key `doc_id` in Redis.
|
107
|
+
#
|
108
|
+
# @param [String] doc_id HASH key holding the fields that need to be indexed
|
109
|
+
# @param [Hash] opts optional parameters
|
110
|
+
# @param [Float]weight asigned by the user to the document
|
111
|
+
# Example:
|
112
|
+
#
|
113
|
+
# redisearch = RediSearch.new('my_idx')
|
114
|
+
# redisearch.add_hash('id_1', { weight: true })
|
115
|
+
#
|
116
|
+
# See http://redisearch.io/Commands/#ftadd
|
117
|
+
# @return [String] "OK" on success
|
118
|
+
def add_hash(doc_id, opts = {}, weight = nil)
|
119
|
+
call(ft_addhash(doc_id, opts, weight))
|
101
120
|
end
|
102
121
|
|
103
122
|
# Search the index with the given `query`
|
@@ -109,6 +128,16 @@ class RediSearch
|
|
109
128
|
build_docs(call(ft_search(query, opts)), opts)
|
110
129
|
end
|
111
130
|
|
131
|
+
# Fetch the contents of multiple documents
|
132
|
+
#
|
133
|
+
# @param [Array] doc_ids ids assigned to the document
|
134
|
+
# @return [Array] documents found for the ids given
|
135
|
+
def get_by_ids(doc_ids)
|
136
|
+
call(ft_mget(doc_ids)).map.with_index do |doc, i|
|
137
|
+
{ 'id' => doc_ids[i] }.merge(Hash[*doc]) unless doc.empty?
|
138
|
+
end.compact
|
139
|
+
end
|
140
|
+
|
112
141
|
# Fetch a document by id
|
113
142
|
#
|
114
143
|
# @param [String] doc_id id assigned to the document
|
@@ -233,7 +262,7 @@ class RediSearch
|
|
233
262
|
['FT.ADD', @idx_name , doc_id, weight || DEFAULT_WEIGHT, *serialize_options(opts, :add), 'FIELDS', *fields]
|
234
263
|
end
|
235
264
|
|
236
|
-
def
|
265
|
+
def ft_addhash(doc_id, opts = {}, weight = nil)
|
237
266
|
['FT.ADDHASH', @idx_name , doc_id, weight || DEFAULT_WEIGHT, *serialize_options(opts, :add)]
|
238
267
|
end
|
239
268
|
|
@@ -294,7 +323,7 @@ class RediSearch
|
|
294
323
|
end
|
295
324
|
|
296
325
|
def build_docs(results, opts = {})
|
297
|
-
return
|
326
|
+
return [] if results.nil? || results[0] == 0
|
298
327
|
results.shift
|
299
328
|
score_offset = opts[:withscores] ? 1 : 0
|
300
329
|
content_offset = opts[:nocontent] ? 0 : 1
|
data/lib/redisearch/version.rb
CHANGED
data/test/redisearch_test.rb
CHANGED
@@ -84,6 +84,14 @@ class RediSearchTest < Minitest::Test
|
|
84
84
|
assert_includes(search_result.to_s, 'Ex Machina')
|
85
85
|
end
|
86
86
|
|
87
|
+
def test_add_hash
|
88
|
+
assert(@redisearch_client.create_index(@schema))
|
89
|
+
doc = ['title', 'Lost in translation', 'director', 'Sofia Coppola', 'year', '2004']
|
90
|
+
@redis_client.hmset('id_1', *doc)
|
91
|
+
assert(@redisearch_client.add_hash('id_1'))
|
92
|
+
assert_includes(@redis_client.call(['FT.SEARCH', 'test_idx', '@title:lost']).to_s, 'Lost in translation')
|
93
|
+
end
|
94
|
+
|
87
95
|
def test_get_by_id
|
88
96
|
assert(@redisearch_client.create_index(@schema))
|
89
97
|
docs = [['id_1', ['title', 'Lost in translation', 'director', 'Sofia Coppola', 'year', '2004']],
|
@@ -94,6 +102,18 @@ class RediSearchTest < Minitest::Test
|
|
94
102
|
assert_equal('Lost in translation', doc['title'])
|
95
103
|
end
|
96
104
|
|
105
|
+
def test_get_by_ids
|
106
|
+
assert(@redisearch_client.create_index(@schema))
|
107
|
+
docs = [['id_1', ['title', 'Lost in translation', 'director', 'Sofia Coppola', 'year', '2004']],
|
108
|
+
['id_2', ['title', 'Ex Machina', 'director', 'Alex Garland', 'year', '2014']]]
|
109
|
+
assert(@redisearch_client.add_docs(docs))
|
110
|
+
docs = @redisearch_client.get_by_ids(['id_1', 'id_2'])
|
111
|
+
assert_equal('id_1', docs[0]['id'])
|
112
|
+
assert_equal('Lost in translation', docs[0]['title'])
|
113
|
+
assert_equal('id_2', docs[1]['id'])
|
114
|
+
assert_equal('Ex Machina', docs[1]['title'])
|
115
|
+
end
|
116
|
+
|
97
117
|
def test_search_simple_query
|
98
118
|
assert(@redisearch_client.create_index(@schema))
|
99
119
|
docs = [['id_1', ['title', 'Lost in translation', 'director', 'Sofia Coppola', 'year', '2004']],
|
@@ -104,6 +124,7 @@ class RediSearchTest < Minitest::Test
|
|
104
124
|
assert matches.any? { |doc| 'Lost in translation' == doc['title'] }
|
105
125
|
assert matches.any? { |doc| 'Ex Machina' == doc['title'] }
|
106
126
|
matches.each { |doc| assert doc['score'].to_i > 0 }
|
127
|
+
assert_equal([], @redisearch_client.search('foobarwozzz'))
|
107
128
|
end
|
108
129
|
|
109
130
|
def test_search_field_selector
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redisearch-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
14
|
A simple Ruby client library for RediSearch.
|