1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
module Elasticsearch
module DSL
# Provides DSL methods for building the search definition
# (queries, filters, aggregations, sorting, etc)
#
module Search
# Initialize a new Search object
#
# @example Building a search definition declaratively
#
# definition = search do
# query do
# match title: 'test'
# end
# end
# definition.to_hash
# => {:query=>{:match=>{:title=>"test"}}}
#
# @example Using the class imperatively
#
# definition = Search.new
# query = Queries::Match.new title: 'test'
# definition.query query
# definition.to_hash
# # => {:query=>{:match=>{:title=>"test"}}}
#
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
#
def search(*args, &block)
Search.new(*args, &block)
end
extend self
# Wraps the whole search definition (queries, filters, aggregations, sorting, etc)
#
class Search
attr_reader :aggregations
def initialize(*args, &block)
@options = Options.new *args
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block
end
# DSL method for building or accessing the `query` part of a search definition
#
# @return [self, {Query}]
#
def query(*args, &block)
case
when block
@query = Query.new(*args, &block)
self
when !args.empty?
@query = args.first
self
else
@query
end
end
# Set the query part of a search definition
#
def query=(value)
query value
end
# DSL method for building the `filter` part of a search definition
#
# @return [self]
#
def filter(*args, &block)
case
when block
@filter = Filter.new(*args, &block)
self
when !args.empty?
@filter = args.first
self
else
@filter
end
end
# Set the filter part of a search definition
#
def filter=(value)
filter value
end
# DSL method for building the `post_filter` part of a search definition
#
# @return [self]
#
def post_filter(*args, &block)
case
when block
@post_filter = Filter.new(*args, &block)
self
when !args.empty?
@post_filter = args.first
self
else
@post_filter
end
end
# Set the post_filter part of a search definition
#
def post_filter=(value)
post_filter value
end
# DSL method for building the `aggregations` part of a search definition
#
# @return [self]
#
def aggregation(*args, &block)
@aggregations ||= AggregationsCollection.new
if block
@aggregations.update args.first => Aggregation.new(*args, &block)
else
name = args.shift
@aggregations.update name => args.shift
end
self
end
# Set the aggregations part of a search definition
#
def aggregations=(value)
@aggregations = value
end
# DSL method for building the `highlight` part of a search definition
#
# @return [self]
#
def highlight(*args, &block)
if !args.empty? || block
@highlight = Highlight.new(*args, &block)
self
else
@highlight
end
end
# DSL method for building the `sort` part of a search definition
#
# @return [self]
#
def sort(*args, &block)
if !args.empty? || block
@sort = Sort.new(*args, &block)
self
else
@sort
end
end
# Set the sort part of a search definition
#
def sort=(value)
@sort = value
end
# DSL method for building the `stored_fields` part of a search definition
#
# @return [self]
#
def stored_fields(value=nil)
if value
@stored_fields = value
self
else
@stored_fields
end
end; alias_method :stored_fields=, :stored_fields
# DSL method for building the `size` part of a search definition
#
# @return [self]
#
def size(value=nil)
if value
@size = value
self
else
@size
end
end; alias_method :size=, :size
# DSL method for building the `from` part of a search definition
#
# @return [self]
#
def from(value=nil)
if value
@from = value
self
else
@from
end
end; alias_method :from=, :from
# DSL method for building the `suggest` part of a search definition
#
# @return [self]
#
def suggest(*args, &block)
if !args.empty? || block
@suggest ||= {}
key, options = args
@suggest.update key => Suggest.new(key, options, &block)
self
else
@suggest
end
end
# Set the suggest part of a search definition
#
def suggest=(value)
@suggest = value
end
# Delegates to the methods provided by the {Options} class
#
def method_missing(name, *args, &block)
if @options.respond_to? name
@options.__send__ name, *args, &block
self
else
super
end
end
# Converts the search definition to a Hash
#
# @return [Hash]
#
def to_hash
hash = {}
hash.update(query: @query.to_hash) if @query
hash.update(filter: @filter.to_hash) if @filter
hash.update(post_filter: @post_filter.to_hash) if @post_filter
hash.update(aggregations: @aggregations.reduce({}) { |sum,item| sum.update item.first => item.last.to_hash }) if @aggregations
hash.update(sort: @sort.to_hash) if @sort
hash.update(size: @size) if @size
hash.update(stored_fields: @stored_fields) if @stored_fields
hash.update(from: @from) if @from
hash.update(suggest: @suggest.reduce({}) { |sum,item| sum.update item.last.to_hash }) if @suggest
hash.update(highlight: @highlight.to_hash) if @highlight
hash.update(@options) unless @options.empty?
hash
end
end
end
end
end
|