Class: Qiita::Elasticsearch::Query
- Inherits:
-
Object
- Object
- Qiita::Elasticsearch::Query
- Defined in:
- lib/qiita/elasticsearch/query.rb
Constant Summary collapse
- DEFAULT_SORT =
[{ "created_at" => "desc" }, "_score"]
Instance Method Summary collapse
- #append_field_token(field_name: nil, term: nil) ⇒ Qiita::Elasticsearch::Query
- #delete_field_token(field_name: nil, term: nil) ⇒ Qiita::Elasticsearch::Query
- #has_field_token?(field_name: nil, positive: nil, term: nil) ⇒ Boolean
-
#initialize(tokens, query_builder_options = nil) ⇒ Query
constructor
A new instance of Query.
-
#query ⇒ Hash
Query property for request body for Elasticsearch.
-
#sort ⇒ Array
Sort property for request body for Elasticsearch.
-
#to_hash ⇒ Hash
Request body for Elasticsearch.
-
#to_s ⇒ String
Query string generated from its tokens.
-
#type ⇒ String?
Last positive type name in query string.
- #update_field_token(field_name: nil, term: nil) ⇒ Qiita::Elasticsearch::Query
Constructor Details
#initialize(tokens, query_builder_options = nil) ⇒ Query
Returns a new instance of Query.
13 14 15 16 |
# File 'lib/qiita/elasticsearch/query.rb', line 13 def initialize(tokens, = nil) = @tokens = tokens end |
Instance Method Details
#append_field_token(field_name: nil, term: nil) ⇒ Qiita::Elasticsearch::Query
22 23 24 25 26 27 28 |
# File 'lib/qiita/elasticsearch/query.rb', line 22 def append_field_token(field_name: nil, term: nil) if has_field_token?(field_name: field_name, term: term) self else build_query([*@tokens, "#{field_name}:#{term}"].join(" ")) end end |
#delete_field_token(field_name: nil, term: nil) ⇒ Qiita::Elasticsearch::Query
34 35 36 37 38 39 40 |
# File 'lib/qiita/elasticsearch/query.rb', line 34 def delete_field_token(field_name: nil, term: nil) build_query( @tokens.reject do |token| (field_name.nil? || token.field_name == field_name) && (term.nil? || token.term == term) end.join(" ") ) end |
#has_field_token?(field_name: nil, positive: nil, term: nil) ⇒ Boolean
46 47 48 49 50 51 |
# File 'lib/qiita/elasticsearch/query.rb', line 46 def has_field_token?(field_name: nil, positive: nil, term: nil) @tokens.any? do |token| (field_name.nil? || token.field_name == field_name) && (term.nil? || token.term == term) && (positive.nil? || positive && token.positive? || !positive && token.negative?) end end |
#query ⇒ Hash
Returns query property for request body for Elasticsearch.
54 55 56 |
# File 'lib/qiita/elasticsearch/query.rb', line 54 def query Nodes::OrSeparatableNode.new(@tokens).to_hash end |
#sort ⇒ Array
Returns sort property for request body for Elasticsearch.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/qiita/elasticsearch/query.rb', line 59 def sort case @tokens.select(&:sort?).last.try(:term) when "created-asc" [{ "created_at" => "asc" }, "_score"] when "lgtms-asc" [{ "lgtms" => "asc" }, "_score"] when "lgtms-desc" [{ "lgtms" => "desc" }, "_score"] when "related-asc" ["_score"] when "related-desc" [{ "_score" => "desc" }] when "stocks-asc" [{ "stocks" => "asc" }, "_score"] when "stocks-desc" [{ "stocks" => "desc" }, "_score"] when "updated-asc" [{ "updated_at" => "asc" }, "_score"] when "updated-desc" [{ "updated_at" => "desc" }, "_score"] else DEFAULT_SORT end end |
#to_hash ⇒ Hash
Returns request body for Elasticsearch.
85 86 87 88 89 90 |
# File 'lib/qiita/elasticsearch/query.rb', line 85 def to_hash { "query" => query, "sort" => sort, } end |
#to_s ⇒ String
Returns query string generated from its tokens.
93 94 95 |
# File 'lib/qiita/elasticsearch/query.rb', line 93 def to_s @tokens.join(" ") end |
#type ⇒ String?
Returns last positive type name in query string.
98 99 100 |
# File 'lib/qiita/elasticsearch/query.rb', line 98 def type @tokens.select(&:type?).select(&:positive?).last.try(:type) end |
#update_field_token(field_name: nil, term: nil) ⇒ Qiita::Elasticsearch::Query
106 107 108 109 110 111 112 |
# File 'lib/qiita/elasticsearch/query.rb', line 106 def update_field_token(field_name: nil, term: nil) build_query( @tokens.reject do |token| token.field_name == field_name end.map(&:to_s).push("#{field_name}:#{term}").join(" ") ) end |