qiita-elasticsearch 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f70a16904b46ae70d71fd2c397a78f8252c04c0
4
- data.tar.gz: 0da3883804dfa7142399a83ad16aa2f70a65711d
3
+ metadata.gz: 2fabbdb6cb1e979825d0a26bd12111dd8f6976f2
4
+ data.tar.gz: e77f04f00822dbac45cc3c5d7efb0f0d8223b8dc
5
5
  SHA512:
6
- metadata.gz: d75c39415180eeadec234d201bcbc9707780664595895d8afca43b0321ce111248dea3c809528c0aa9ef619735bf518335790d07e407e18716a69ed008ecedfc
7
- data.tar.gz: 84bb7676429cd15af3b5efae5601a225120b24546b85b82439d7005be41dd51b1270771faa066a6096a357950be0a5050da69a9d6a31b6742cb11d2eafc08f68
6
+ metadata.gz: 67abae98b1f5068a3c9cca3e5c2b86aaebae7990e2580dc0025a5f89eefc50f88b567b0374d866232c072206ad6e646beebe4cb15f8f9ba7ea587ee628baa84f
7
+ data.tar.gz: d0de917ab45b6924416acfb777f9641b8a7a3de67b1b485276b28072905eea19e5b8b215f0e2f467904c2bcbaec3f163360a1991d3007257228a4b1eb3c7e106
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.1.1
2
+ - Support hierarchal fields
3
+
4
+ ## 0.1.0
5
+ - 1st Release
@@ -5,8 +5,10 @@ module Qiita
5
5
  module Nodes
6
6
  class BoolQueryNode
7
7
  # @param [Array<Qiita::Elasticsearch::Tokens>] tokens
8
+ # @param [Array<String>, nil] hierarchal_fields
8
9
  # @param [Array<String>, nil] matchable_fields
9
- def initialize(tokens, matchable_fields: nil)
10
+ def initialize(tokens, hierarchal_fields: nil, matchable_fields: nil)
11
+ @hierarchal_fields = hierarchal_fields
10
12
  @matchable_fields = matchable_fields
11
13
  @tokens = tokens
12
14
  end
@@ -42,7 +44,11 @@ module Qiita
42
44
 
43
45
  def must_not_queries
44
46
  must_not_tokens.map do |token|
45
- Nodes::TokenNode.new(token, matchable_fields: @matchable_fields).to_hash
47
+ Nodes::TokenNode.new(
48
+ token,
49
+ hierarchal_fields: @hierarchal_fields,
50
+ matchable_fields: @matchable_fields,
51
+ ).to_hash
46
52
  end
47
53
  end
48
54
 
@@ -51,12 +57,20 @@ module Qiita
51
57
  end
52
58
 
53
59
  def must_query
54
- Nodes::TokenNode.new(must_tokens.first, matchable_fields: @matchable_fields).to_hash
60
+ Nodes::TokenNode.new(
61
+ must_tokens.first,
62
+ hierarchal_fields: @hierarchal_fields,
63
+ matchable_fields: @matchable_fields,
64
+ ).to_hash
55
65
  end
56
66
 
57
67
  def must_queries
58
68
  must_tokens.map do |token|
59
- Nodes::TokenNode.new(token, matchable_fields: @matchable_fields).to_hash
69
+ Nodes::TokenNode.new(
70
+ token,
71
+ hierarchal_fields: @hierarchal_fields,
72
+ matchable_fields: @matchable_fields,
73
+ ).to_hash
60
74
  end
61
75
  end
62
76
 
@@ -65,12 +79,18 @@ module Qiita
65
79
  end
66
80
 
67
81
  def should_query
68
- Nodes::TokenNode.new(should_tokens.first, matchable_fields: @matchable_fields).to_hash
82
+ Nodes::TokenNode.new(
83
+ should_tokens.first,
84
+ matchable_fields: @matchable_fields,
85
+ ).to_hash
69
86
  end
70
87
 
71
88
  def should_queries
72
89
  should_tokens.map do |token|
73
- Nodes::TokenNode.new(token, matchable_fields: @matchable_fields).to_hash
90
+ Nodes::TokenNode.new(
91
+ token,
92
+ matchable_fields: @matchable_fields,
93
+ ).to_hash
74
94
  end
75
95
  end
76
96
 
@@ -2,25 +2,60 @@ module Qiita
2
2
  module Elasticsearch
3
3
  module Nodes
4
4
  class FilterQueryNode
5
+ DEFAULT_HIERARCHAL_FIELDS = []
6
+
5
7
  # @param [Qiita::Elasticsearch::Token] token
6
- def initialize(token)
8
+ def initialize(token, hierarchal_fields: nil)
9
+ @hierarchal_fields = hierarchal_fields
7
10
  @token = token
8
11
  end
9
12
 
10
13
  # @return [Hash]
11
14
  def to_hash
12
- {
13
- "filtered" => {
14
- "filter" => {
15
- "term" => {
16
- @token.field_name => @token.term,
15
+ if has_hierarchal_token?
16
+ {
17
+ "filtered" => {
18
+ "filter" => {
19
+ "bool" => {
20
+ "should" => [
21
+ "prefix" => {
22
+ @token.field_name => @token.term + "/",
23
+ },
24
+ "term" => {
25
+ @token.field_name => @token.term,
26
+ },
27
+ ],
28
+ },
29
+ },
30
+ "query" => {
31
+ "match_all" => {},
17
32
  },
18
33
  },
19
- "query" => {
20
- "match_all" => {},
34
+ }
35
+ else
36
+ {
37
+ "filtered" => {
38
+ "filter" => {
39
+ "term" => {
40
+ @token.field_name => @token.term,
41
+ },
42
+ },
43
+ "query" => {
44
+ "match_all" => {},
45
+ },
21
46
  },
22
- },
23
- }
47
+ }
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def has_hierarchal_token?
54
+ hierarchal_fields.include?(@token.field_name)
55
+ end
56
+
57
+ def hierarchal_fields
58
+ @hierarchal_fields || DEFAULT_HIERARCHAL_FIELDS
24
59
  end
25
60
  end
26
61
  end
@@ -6,8 +6,10 @@ module Qiita
6
6
  module Nodes
7
7
  class OrSeparatableNode
8
8
  # @param [Array<Qiita::Elasticsearch::Tokens>] tokens
9
+ # @param [Array<String>, nil] hierarchal_fields
9
10
  # @param [Array<String>, nil] matchable_fields
10
- def initialize(tokens, matchable_fields: nil)
11
+ def initialize(tokens, hierarchal_fields: nil, matchable_fields: nil)
12
+ @hierarchal_fields = hierarchal_fields
11
13
  @matchable_fields = matchable_fields
12
14
  @tokens = tokens
13
15
  end
@@ -19,6 +21,7 @@ module Qiita
19
21
  when 1
20
22
  Nodes::BoolQueryNode.new(
21
23
  tokens_grouped_by_or_token.first,
24
+ hierarchal_fields: @hierarchal_fields,
22
25
  matchable_fields: @matchable_fields,
23
26
  ).to_hash
24
27
  else
@@ -27,6 +30,7 @@ module Qiita
27
30
  "should" => tokens_grouped_by_or_token.map do |tokens|
28
31
  Nodes::BoolQueryNode.new(
29
32
  tokens,
33
+ hierarchal_fields: @hierarchal_fields,
30
34
  matchable_fields: @matchable_fields,
31
35
  ).to_hash
32
36
  end,
@@ -6,15 +6,17 @@ module Qiita
6
6
  module Nodes
7
7
  class TokenNode
8
8
  # @param [Qiita::Elasticsearch::Token] token
9
+ # @param [Array<String>, nil] hierarchal_fields
9
10
  # @param [Array<String>, nil] matchable_fields
10
- def initialize(token, matchable_fields: nil)
11
+ def initialize(token, hierarchal_fields: nil, matchable_fields: nil)
12
+ @hierarchal_fields = hierarchal_fields
11
13
  @matchable_fields = matchable_fields
12
14
  @token = token
13
15
  end
14
16
 
15
17
  def to_hash
16
18
  if @token.field_name
17
- FilterQueryNode.new(@token).to_hash
19
+ FilterQueryNode.new(@token, hierarchal_fields: @hierarchal_fields).to_hash
18
20
  else
19
21
  MatchQueryNode.new(@token, matchable_fields: @matchable_fields).to_hash
20
22
  end
@@ -5,11 +5,13 @@ require "qiita/elasticsearch/parser"
5
5
  module Qiita
6
6
  module Elasticsearch
7
7
  class QueryBuilder
8
- # @param [Array<String>, nil] matchable_fields
9
8
  # @param [Array<String>, nil] filterable_fields
10
- def initialize(matchable_fields: nil, filterable_fields: nil)
11
- @matchable_fields = matchable_fields
9
+ # @param [Array<String>, nil] hierarchal_fields
10
+ # @param [Array<String>, nil] matchable_fields
11
+ def initialize(hierarchal_fields: nil, filterable_fields: nil, matchable_fields: nil)
12
+ @hierarchal_fields = hierarchal_fields
12
13
  @filterable_fields = filterable_fields
14
+ @matchable_fields = matchable_fields
13
15
  end
14
16
 
15
17
  # @param [String] query_string Raw query string
@@ -21,6 +23,7 @@ module Qiita
21
23
  else
22
24
  Nodes::OrSeparatableNode.new(
23
25
  tokens,
26
+ hierarchal_fields: @hierarchal_fields,
24
27
  matchable_fields: @matchable_fields,
25
28
  ).to_hash
26
29
  end
@@ -1,5 +1,5 @@
1
1
  module Qiita
2
2
  module Elasticsearch
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qiita-elasticsearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -91,6 +91,7 @@ files:
91
91
  - ".rspec"
92
92
  - ".rubocop.yml"
93
93
  - ".travis.yml"
94
+ - CHANGELOG.md
94
95
  - Gemfile
95
96
  - LICENSE.txt
96
97
  - README.md