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
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
54 55 56 |
# File 'lib/qiita/elasticsearch/query.rb', line 54 def query Nodes::OrSeparatableNode.new(@tokens).to_hash end |
#sort ⇒ Array
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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 "related-asc" ["_score"] when "related-desc" [{ "_score" => "desc" }] when "stocks-asc" [{ "stocks" => "asc" }, "_score"] when "stocks-desc" [{ "stocks" => "desc" }, "_score"] else DEFAULT_SORT end end |
#to_hash ⇒ Hash
77 78 79 80 81 82 |
# File 'lib/qiita/elasticsearch/query.rb', line 77 def to_hash { "query" => query, "sort" => sort, } end |
#to_s ⇒ String
85 86 87 |
# File 'lib/qiita/elasticsearch/query.rb', line 85 def to_s @tokens.join(" ") end |
#type ⇒ String?
90 91 92 |
# File 'lib/qiita/elasticsearch/query.rb', line 90 def type @tokens.select(&:type?).select(&:positive?).last.try(:type) end |
#update_field_token(field_name: nil, term: nil) ⇒ Qiita::Elasticsearch::Query
98 99 100 101 102 103 104 |
# File 'lib/qiita/elasticsearch/query.rb', line 98 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 |