Module: ActiveEs::Quering
- Included in:
- Base
- Defined in:
- lib/active_es/quering.rb
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
-
#find(id) ⇒ Object
using ActiveEs::Base.find(‘W_KDRmgBeTay2K79iAf7’) => #<Content:0x00007fffc4637fb0 @description=“sample description 1”, @id=“W_KDRmgBeTay2K79iAf7”, @title=“sample title 1”>.
-
#match(attributes = nil) ⇒ Object
using It retrieves data that matches one of the attributes specified by the argument.
-
#multi_match(fields:, match:, operator: 'and') ⇒ Object
using We perform a search using a match query for multiple fields.
-
#must(attributes = {}) ⇒ Object
using Get data that matches all the attributes specified by arguments.
-
#must_not(attributes = {}) ⇒ Object
using Acquires all data that does not match Hash specified in argument’s.
-
#should(attributes = {}) ⇒ Object
using It retrieves data that matches one of the attributes specified by the argument.
-
#term(attributes = nil) ⇒ Object
using In the argument, pass field to key and Hash with numerical value to value.
-
#terms(attributes = nil) ⇒ Object
using We perform a search using a match query for multiple fields.
Instance Method Details
#all ⇒ Object
16 17 18 19 20 21 |
# File 'lib/active_es/quering.rb', line 16 def all body = { query: { match_all: {} } } result = client.search index: index, type: type, body: body result_instance(result) end |
#count ⇒ Object
12 13 14 |
# File 'lib/active_es/quering.rb', line 12 def count client.count(index: index, type: type)["count"] end |
#find(id) ⇒ Object
using ActiveEs::Base.find(‘W_KDRmgBeTay2K79iAf7’)
> #<Content:0x00007fffc4637fb0 @description=“sample description 1”, @id=“W_KDRmgBeTay2K79iAf7”, @title=“sample title 1”>
6 7 8 9 10 |
# File 'lib/active_es/quering.rb', line 6 def find(id) result = client.get index: index, type: type, id: id new(result["_source"].merge({ id: result["_id"] })) end |
#match(attributes = nil) ⇒ Object
using It retrieves data that matches one of the attributes specified by the argument. The value of Hash specified by attributes is an empty string delimiter and can be specified more than once. In that case, we will retrieve data whose value matches one of the specified keys. Unlike the term method, Analyze uses it for the field that Analyze needs to do.
ActiveEs::Base.match(title: “sample”)
> [
#<Content:0x00007fffc4637fb0 @description="sample description 1", @id="W_KDRmgBeTay2K79iAf7", @score=0.2876821, @title="sample title 1">,
#<Content:0x00007fffc3f0c7b8 @description="sample description 2", @id="XPKDRmgBeTay2K79ywfz", @score=0.2876821, @title="sample title 2">
]
ActiveEs::Base.match(title: ‘1 0’)
> [#<Content:0x00007fffdea9fea8 @description=“sample description 1”, @id=“W_KDRmgBeTay2K79iAf7”, @score=0.2876821, @title=“sample title 1”>]
ActiveEs::Base.match(title: ‘invalid_title’)
> []
39 40 41 42 43 44 |
# File 'lib/active_es/quering.rb', line 39 def match(attributes = nil) body = { query: { match: attributes } } result = client.search index: index, type: type, body: body result_instance(result) end |
#multi_match(fields:, match:, operator: 'and') ⇒ Object
using We perform a search using a match query for multiple fields. The default operation is “and”, but an or search is performed by specifying “or” as the key ‘operation’ of the argument. Unlike the terms method, Analyze uses it for the field that Analyze needs to do.
ActiveEs::Base.multi_match(field: [‘title’, ‘description’], match: ‘sample 1’)
> [#<Content:0x00007fffbc2746b0 @description=“sample description 1”, @id=“W_KDRmgBeTay2K79iAf7”, @score=0.5753642, @title=“sample title 1”>]
ActiveEs::Base.multi_match(fields: [‘title’, ‘description’], match: [‘sample’, ‘1’])
> [#<Content:0x00007fffbc2746b0 @description=“sample description 1”, @id=“W_KDRmgBeTay2K79iAf7”, @score=0.5753642, @title=“sample title 1”>]
ActiveEs::Base.multi_match(fields: [‘title’, ‘description’], match: [‘sample’, ‘1’], operator: ‘or’)
> [
#<Content:0x00007ffff712d280 @description="sample description 1", @id="W_KDRmgBeTay2K79iAf7", @score=0.5753642, @title="sample title 1">,
#<Content:0x00007ffff712c6a0 @description="sample description 2", @id="XPKDRmgBeTay2K79ywfz", @score=0.2876821, @title="sample title 2">
]
ActiveEs::Base.multi_match(fields: ”, match: [])
> []
80 81 82 83 84 85 86 87 88 |
# File 'lib/active_es/quering.rb', line 80 def multi_match(fields:, match:, operator: 'and') raise ArgumentError, "operator accepts only 'and','or'" unless operator =~ /and|or/ keyword = Array(match).join(' ') body = { query: { multi_match: { fields: fields, query: keyword, operator: operator} } } result = client.search index: index, type: type, body: body result_instance(result) end |
#must(attributes = {}) ⇒ Object
using Get data that matches all the attributes specified by arguments.
ActiveEs::Base.must(title: ‘sample’, description: ‘1’)
> [#<Content:0x00007fffd5f32230 @description=“sample description 1”, @id=“W_KDRmgBeTay2K79iAf7”, @score=0.5753642, @title=“sample title 1”>]
ActiveEs::Base.must(title: ”)
> []
122 123 124 125 126 127 128 |
# File 'lib/active_es/quering.rb', line 122 def must(attributes = {}) query = attributes.map { |attr| { match: Hash[*attr] } } body = { query: { bool: { must: query } } } result = client.search index: index, type: type, body: body result_instance(result) end |
#must_not(attributes = {}) ⇒ Object
using Acquires all data that does not match Hash specified in argument’s. Hash values of attributes can be separated by null character and multiple can be specified. In that case, we will retrieve all data that is not included in any of the specified values.
ActiveEs::Base.must_not(title: ‘1’)
> [#<Content:0x00007ffff31bc3f8 @description=“sample description 2”, @id=“XPKDRmgBeTay2K79ywfz”, @score=1.0, @title=“sample title 2”>]
ActiveEs::Base.must_not(title: ”)
> [
#<Content:0x00007ffff3b3bd20 @description="sample description 2", @id="XPKDRmgBeTay2K79ywfz", @score=1.0, @title="sample title 2">,
#<Content:0x00007ffff3b3b938 @description="sample description 1", @id="W_KDRmgBeTay2K79iAf7", @score=1.0, @title="sample title 1">
]
ActiveEs::Base.must_not(title: ‘1 2’)
> []
168 169 170 171 172 173 174 |
# File 'lib/active_es/quering.rb', line 168 def must_not(attributes = {}) query = attributes.map { |attr| { match: Hash[*attr] } } body = { query: { bool: { must_not: query } } } result = client.search index: index, type: type, body: body result_instance(result) end |
#should(attributes = {}) ⇒ Object
using It retrieves data that matches one of the attributes specified by the argument.
ActiveEs::Base.should(title: ‘1’)
> [#<Content:0x00007fffe7e42ea8 @description=“sample description 1”, @id=“W_KDRmgBeTay2K79iAf7”, @score=0.2876821, @title=“sample title 1”>]
ActiveEs::Base.should(title: ‘2’, description: ‘1’)
> [
#<Content:0x00007fffe7fe0198@description="sample description 2", @id="XPKDRmgBeTay2K79ywfz", @score=0.2876821, @title="sample title 2">,
#<Content:0x00007fffe7ff7e88@description="sample description 1", @id="W_KDRmgBeTay2K79iAf7", @score=0.2876821, @title="sample title 1">
]
ActiveEs::Base.should(title: ”)
> []
144 145 146 147 148 149 150 |
# File 'lib/active_es/quering.rb', line 144 def should(attributes = {}) query = attributes.map { |attr| { match: Hash[*attr] } } body = { query: { bool: { should: query } } } result = client.search index: index, type: type, body: body result_instance(result) end |
#term(attributes = nil) ⇒ Object
using In the argument, pass field to key and Hash with numerical value to value. Unlike the match method, since Analyze is not performed, high-speed searching is possible for fields where Analyze is unnecessary.
ActiveEs::Base.term(number: 3)
> [#<Content:0x00007fffe60264b0 @description=“sample description 1”, @id=“Hhp01mgBhOPWXkxaeleN”, @number=3, @score=1.0, @title=“sample title 1”>]
54 55 56 57 58 59 |
# File 'lib/active_es/quering.rb', line 54 def term(attributes = nil) body = { query: { term: attributes } } result = client.search index: index, type: type, body: body result_instance(result) end |
#terms(attributes = nil) ⇒ Object
using We perform a search using a match query for multiple fields. The default operation is “and”, but an or search is performed by specifying “or” as the key ‘operation’ of the argument. Unlike the multi_match method, since Analyze is not performed, high-speed searching is possible for fields where Analyze is unnecessary.
ActiveEs::Base.terms(number: 1)
> [#<Content:0x00007fffbc7dfeb0 @description=“description1”, @id=“IRrI1mgBhOPWXkxafVdk”, @number=1, @rank=1, @score=1.0, @title=“title1”>]
ActiveEs::Base.terms(number: [1, 2])
> [
#<Content:0x00007fffbbe29038 @description="description2", @id="IhrI1mgBhOPWXkxafVfv", @number=2, @rank=2, @score=1.0, @title="title2">,
#<Content:0x00007fffbbe33100 @description="description1", @id="IRrI1mgBhOPWXkxafVdk", @number=1, @rank=1, @score=1.0, @title="title1">
]
104 105 106 107 108 109 110 111 112 |
# File 'lib/active_es/quering.rb', line 104 def terms(attributes = nil) field = attributes.keys.first query = { field => attributes.values.flatten } body = { query: { terms: query } } result = client.search index: index, type: type, body: body result_instance(result) end |