qiita-elasticsearch 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +3 -0
- data/lib/qiita/elasticsearch/date_token.rb +10 -6
- data/lib/qiita/elasticsearch/int_token.rb +26 -4
- data/lib/qiita/elasticsearch/nodes/or_separatable_node.rb +1 -1
- data/lib/qiita/elasticsearch/query.rb +96 -0
- data/lib/qiita/elasticsearch/query_builder.rb +11 -11
- data/lib/qiita/elasticsearch/token.rb +34 -9
- data/lib/qiita/elasticsearch/tokenizer.rb +1 -1
- data/lib/qiita/elasticsearch/version.rb +1 -1
- metadata +5 -4
- data/lib/qiita/elasticsearch/errors.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ac89f9676b05d247337d139e417cb55d449152c
|
4
|
+
data.tar.gz: 44965231890460369545280221bedb008090743a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b507ea1cffce78984dd9e59a0d22b61727d1331a602f094b6731fe8784810495c3e65d09ffde4fce3c41dd9f747ed13087af9f400eefb677f8e8a8212eee030
|
7
|
+
data.tar.gz: 02286a98b45cfaca0f8bd6570d39faf5d3f21f68ca8b5cffa27a1430f00d7ec857ff25cf90032a68da59e6f48adc7c4a1285afa7505dd19e6127ae9b3c8d3119
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "active_support/core_ext/date"
|
2
2
|
require "active_support/core_ext/integer"
|
3
3
|
require "qiita/elasticsearch/concerns/range_operand_includable"
|
4
|
-
require "qiita/elasticsearch/errors"
|
5
4
|
require "qiita/elasticsearch/token"
|
6
5
|
|
7
6
|
module Qiita
|
@@ -53,16 +52,12 @@ module Qiita
|
|
53
52
|
}
|
54
53
|
end
|
55
54
|
else
|
56
|
-
|
55
|
+
Nodes::NullNode.new.to_hash
|
57
56
|
end
|
58
57
|
end
|
59
58
|
|
60
59
|
private
|
61
60
|
|
62
|
-
def date_match
|
63
|
-
@date_match ||= DATE_PATTERN.match(range_query || @term)
|
64
|
-
end
|
65
|
-
|
66
61
|
# @return [Date]
|
67
62
|
def beginning_of_range
|
68
63
|
@beginning_of_range ||=
|
@@ -76,6 +71,10 @@ module Qiita
|
|
76
71
|
end
|
77
72
|
end
|
78
73
|
|
74
|
+
def date_match
|
75
|
+
@date_match ||= DATE_PATTERN.match(range_query || @term)
|
76
|
+
end
|
77
|
+
|
79
78
|
# @return [Date]
|
80
79
|
def end_of_range
|
81
80
|
@end_of_range ||=
|
@@ -88,6 +87,11 @@ module Qiita
|
|
88
87
|
beginning_of_range + 1.year
|
89
88
|
end
|
90
89
|
end
|
90
|
+
|
91
|
+
# @note Override
|
92
|
+
def has_invalid_term?
|
93
|
+
!!date_match
|
94
|
+
end
|
91
95
|
end
|
92
96
|
end
|
93
97
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require "qiita/elasticsearch/concerns/range_operand_includable"
|
2
|
-
require "qiita/elasticsearch/errors"
|
3
2
|
require "qiita/elasticsearch/token"
|
4
3
|
|
5
4
|
module Qiita
|
@@ -12,7 +11,7 @@ module Qiita
|
|
12
11
|
# @return [Hash]
|
13
12
|
# @raise [InvalidQuery]
|
14
13
|
def to_hash
|
15
|
-
if range_parameter &&
|
14
|
+
if range_parameter && has_valid_range_query?
|
16
15
|
{
|
17
16
|
"range" => {
|
18
17
|
@field_name => {
|
@@ -20,16 +19,39 @@ module Qiita
|
|
20
19
|
},
|
21
20
|
},
|
22
21
|
}
|
23
|
-
elsif
|
22
|
+
elsif has_valid_int_term?
|
24
23
|
{
|
25
24
|
"term" => {
|
26
25
|
@field_name => @term.to_i,
|
27
26
|
},
|
28
27
|
}
|
29
28
|
else
|
30
|
-
|
29
|
+
Nodes::NullNode.new.to_hash
|
31
30
|
end
|
32
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def has_invalid_range_query?
|
36
|
+
has_range_query? && !has_valid_range_query?
|
37
|
+
end
|
38
|
+
|
39
|
+
# @note Override
|
40
|
+
def has_invalid_term?
|
41
|
+
range_parameter && has_invalid_range_query? || !has_valid_int_term?
|
42
|
+
end
|
43
|
+
|
44
|
+
def has_range_query?
|
45
|
+
!range_query.nil?
|
46
|
+
end
|
47
|
+
|
48
|
+
def has_valid_int_term?
|
49
|
+
INT_PATTERN === @term
|
50
|
+
end
|
51
|
+
|
52
|
+
def has_valid_range_query?
|
53
|
+
INT_PATTERN === range_query
|
54
|
+
end
|
33
55
|
end
|
34
56
|
end
|
35
57
|
end
|
@@ -31,7 +31,7 @@ module Qiita
|
|
31
31
|
|
32
32
|
# @return [Array<Array<Qiita::Elasticsearch::Token>>]
|
33
33
|
def tokens_grouped_by_or_token
|
34
|
-
@tokens_grouped_by_or_token ||= @tokens.each_with_object([[]]) do |token, groups|
|
34
|
+
@tokens_grouped_by_or_token ||= @tokens.reject(&:ignorable?).each_with_object([[]]) do |token, groups|
|
35
35
|
if token.or?
|
36
36
|
groups << []
|
37
37
|
else
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require "qiita/elasticsearch/nodes/null_node"
|
2
|
+
require "qiita/elasticsearch/nodes/or_separatable_node"
|
3
|
+
require "qiita/elasticsearch/tokenizer"
|
4
|
+
|
5
|
+
module Qiita
|
6
|
+
module Elasticsearch
|
7
|
+
class Query
|
8
|
+
# @param [Array<Qiita::Elasticsearch::Token>] tokens
|
9
|
+
# @param [Hash] query_builder_options For building new query from this query
|
10
|
+
def initialize(tokens, query_builder_options = nil)
|
11
|
+
@query_builder_options = query_builder_options
|
12
|
+
@tokens = tokens
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param [String] field_name
|
16
|
+
# @param [String] term
|
17
|
+
# @return [Qiita::Elasticsearch::Query]
|
18
|
+
# @example query.append_field_token(field_name: "tag", term: "Ruby")
|
19
|
+
def append_field_token(field_name: nil, term: nil)
|
20
|
+
build_query([*@tokens, "#{field_name}:#{term}"].join(" "))
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param [String] field_name
|
24
|
+
# @param [String] term
|
25
|
+
# @return [Qiita::Elasticsearch::Query]
|
26
|
+
# @example query.delete_field_token(field_name: "tag", term: "Ruby")
|
27
|
+
def delete_field_token(field_name: nil, term: nil)
|
28
|
+
build_query(
|
29
|
+
@tokens.reject do |token|
|
30
|
+
token.field_name == field_name && token.term == term
|
31
|
+
end.join(" ")
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param [String] field_name
|
36
|
+
# @param [String] term
|
37
|
+
# @example query.has_field_token?(field_name: "tag", term: "Ruby")
|
38
|
+
def has_field_token?(field_name: nil, term: nil)
|
39
|
+
@tokens.any? do |token|
|
40
|
+
token.field_name == field_name && token.term == term
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Hash]
|
45
|
+
# @example query.to_hash
|
46
|
+
def to_hash
|
47
|
+
if has_empty_tokens?
|
48
|
+
Nodes::NullNode.new.to_hash
|
49
|
+
else
|
50
|
+
Nodes::OrSeparatableNode.new(@tokens).to_hash
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [String] query string generated from its tokens
|
55
|
+
def to_s
|
56
|
+
@tokens.join(" ")
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param [String] field_name
|
60
|
+
# @param [String] term
|
61
|
+
# @return [Qiita::Elasticsearch::Query]
|
62
|
+
# @example query.update_field_token(field_name: "tag", term: "Ruby")
|
63
|
+
def update_field_token(field_name: nil, term: nil)
|
64
|
+
build_query(
|
65
|
+
@tokens.reject do |token|
|
66
|
+
token.field_name == field_name
|
67
|
+
end.map(&:to_s).push("#{field_name}:#{term}").join(" ")
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
# Build a new query from query string
|
74
|
+
# @param [String] query_string
|
75
|
+
# @return [Qiita::Elasticsearch::Query]
|
76
|
+
# @example build_query("test tag:Ruby")
|
77
|
+
def build_query(query_string)
|
78
|
+
query_builder.build(query_string)
|
79
|
+
end
|
80
|
+
|
81
|
+
def has_empty_tokens?
|
82
|
+
@tokens.size.zero?
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return [Qiita::Elasticsearch::QueryBuilder]
|
86
|
+
def query_builder
|
87
|
+
QueryBuilder.new(query_builder_options)
|
88
|
+
end
|
89
|
+
|
90
|
+
# @return [Hash]
|
91
|
+
def query_builder_options
|
92
|
+
@query_builder_options || {}
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -1,7 +1,6 @@
|
|
1
|
-
require "qiita/elasticsearch/errors"
|
2
1
|
require "qiita/elasticsearch/nodes/null_node"
|
3
2
|
require "qiita/elasticsearch/nodes/or_separatable_node"
|
4
|
-
require "qiita/elasticsearch/
|
3
|
+
require "qiita/elasticsearch/query"
|
5
4
|
|
6
5
|
module Qiita
|
7
6
|
module Elasticsearch
|
@@ -24,16 +23,17 @@ module Qiita
|
|
24
23
|
end
|
25
24
|
|
26
25
|
# @param [String] query_string Raw query string
|
27
|
-
# @return [
|
26
|
+
# @return [Qiita::Elasticsearch::Query]
|
28
27
|
def build(query_string)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
Query.new(
|
29
|
+
tokenizer.tokenize(query_string),
|
30
|
+
downcased_fields: @downcased_fields,
|
31
|
+
filterable_fields: @filterable_fields,
|
32
|
+
hierarchal_fields: @hierarchal_fields,
|
33
|
+
int_fields: @int_fields,
|
34
|
+
matchable_fields: @matchable_fields,
|
35
|
+
time_zone: @time_zone,
|
36
|
+
)
|
37
37
|
end
|
38
38
|
|
39
39
|
private
|
@@ -3,11 +3,16 @@ module Qiita
|
|
3
3
|
class Token
|
4
4
|
attr_reader :field_name, :term
|
5
5
|
|
6
|
-
# @param [true, false] downcased
|
7
|
-
|
6
|
+
# @param [true, false] downcased True if given term must be downcased on query representation
|
7
|
+
# @param [String, nil] field_name Field name part
|
8
|
+
# @param [true, fales] negative True if this term represents negative token (e.g. "-Perl")
|
9
|
+
# @param [true, false] quoted Given term is quoted or note
|
10
|
+
# @param [String] term Term part
|
11
|
+
# @param [String] token_string Original entire string
|
12
|
+
def initialize(downcased: nil, field_name: nil, negative: nil, quoted: nil, term: nil, token_string: nil)
|
8
13
|
@downcased = downcased
|
9
14
|
@field_name = field_name
|
10
|
-
@
|
15
|
+
@negative = negative
|
11
16
|
@quoted = quoted
|
12
17
|
@term = term
|
13
18
|
@token_string = token_string
|
@@ -22,14 +27,15 @@ module Qiita
|
|
22
27
|
@downcased_term ||= term.downcase
|
23
28
|
end
|
24
29
|
|
25
|
-
def to_hash
|
26
|
-
fail NotImplementedError
|
27
|
-
end
|
28
|
-
|
29
30
|
def filter?
|
30
31
|
!field_name.nil? || negative?
|
31
32
|
end
|
32
33
|
|
34
|
+
# @return [true, false] True if this token is ignorable on building query (e.g. "-stocked:foo")
|
35
|
+
def ignorable?
|
36
|
+
negative? && !field_name.nil? && has_invalid_term?
|
37
|
+
end
|
38
|
+
|
33
39
|
def must?
|
34
40
|
!field_name.nil? && positive?
|
35
41
|
end
|
@@ -43,7 +49,7 @@ module Qiita
|
|
43
49
|
# ^^^^^
|
44
50
|
# This
|
45
51
|
def negative?
|
46
|
-
|
52
|
+
!!@negative
|
47
53
|
end
|
48
54
|
|
49
55
|
# @return [true, false] True if this token is for OR filter
|
@@ -56,7 +62,7 @@ module Qiita
|
|
56
62
|
|
57
63
|
# @return [true, false] Opposite of #negative?
|
58
64
|
def positive?
|
59
|
-
|
65
|
+
!negative?
|
60
66
|
end
|
61
67
|
|
62
68
|
# @return [String] Downcased or not-downcased term
|
@@ -75,6 +81,25 @@ module Qiita
|
|
75
81
|
def quoted?
|
76
82
|
!!@quoted
|
77
83
|
end
|
84
|
+
|
85
|
+
# @note Override me
|
86
|
+
def to_hash
|
87
|
+
fail NotImplementedError
|
88
|
+
end
|
89
|
+
|
90
|
+
# @note Override
|
91
|
+
# @return [String]
|
92
|
+
def to_s
|
93
|
+
@token_string.to_s
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
# @note Override me if needed
|
99
|
+
# @return [true, false] True if its term is invalid value
|
100
|
+
def has_invalid_term?
|
101
|
+
false
|
102
|
+
end
|
78
103
|
end
|
79
104
|
end
|
80
105
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qiita-elasticsearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -113,7 +113,6 @@ files:
|
|
113
113
|
- lib/qiita/elasticsearch.rb
|
114
114
|
- lib/qiita/elasticsearch/concerns/range_operand_includable.rb
|
115
115
|
- lib/qiita/elasticsearch/date_token.rb
|
116
|
-
- lib/qiita/elasticsearch/errors.rb
|
117
116
|
- lib/qiita/elasticsearch/filterable_token.rb
|
118
117
|
- lib/qiita/elasticsearch/hierarchal_token.rb
|
119
118
|
- lib/qiita/elasticsearch/int_token.rb
|
@@ -126,6 +125,7 @@ files:
|
|
126
125
|
- lib/qiita/elasticsearch/nodes/or_separatable_node.rb
|
127
126
|
- lib/qiita/elasticsearch/nodes/query_node.rb
|
128
127
|
- lib/qiita/elasticsearch/nodes/term_node.rb
|
128
|
+
- lib/qiita/elasticsearch/query.rb
|
129
129
|
- lib/qiita/elasticsearch/query_builder.rb
|
130
130
|
- lib/qiita/elasticsearch/range_token.rb
|
131
131
|
- lib/qiita/elasticsearch/token.rb
|
@@ -152,8 +152,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
152
|
version: '0'
|
153
153
|
requirements: []
|
154
154
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.
|
155
|
+
rubygems_version: 2.4.5
|
156
156
|
signing_key:
|
157
157
|
specification_version: 4
|
158
158
|
summary: Elasticsearch client helper for Qiita.
|
159
159
|
test_files: []
|
160
|
+
has_rdoc:
|