├── CODE_OF_CONDUCT ├── NOTICE ├── .gitignore ├── CONTRIBUTING.md ├── lib ├── elasticsearch-dsl.rb └── elasticsearch │ └── dsl │ ├── version.rb │ ├── utils.rb │ └── search │ ├── queries │ ├── match_all.rb │ ├── ids.rb │ ├── term.rb │ ├── prefix.rb │ ├── terms.rb │ ├── span_term.rb │ ├── regexp.rb │ ├── wildcard.rb │ ├── span_multi.rb │ ├── span_first.rb │ ├── template.rb │ ├── span_or.rb │ ├── geo_shape.rb │ ├── match_phrase.rb │ ├── match_phrase_prefix.rb │ ├── boosting.rb │ ├── span_near.rb │ ├── dis_max.rb │ ├── span_not.rb │ ├── indices.rb │ ├── fuzzy_like_this_field.rb │ ├── fuzzy_like_this.rb │ ├── common.rb │ ├── fuzzy.rb │ ├── simple_query_string.rb │ └── exists.rb │ ├── aggregations │ ├── avg.rb │ ├── sum.rb │ ├── max.rb │ ├── min.rb │ ├── value_count.rb │ ├── extended_stats.rb │ ├── global.rb │ ├── ip_range.rb │ ├── filter.rb │ ├── cardinality.rb │ ├── percentiles.rb │ ├── nested.rb │ ├── geohash_grid.rb │ ├── histogram.rb │ ├── percentile_ranks.rb │ ├── top_hits.rb │ ├── date_range.rb │ ├── missing.rb │ ├── filters.rb │ ├── stats.rb │ ├── pipeline │ │ ├── cumulative_sum.rb │ │ ├── derivative.rb │ │ ├── avg_bucket.rb │ │ ├── sum_bucket.rb │ │ ├── stats_bucket.rb │ │ ├── serial_diff.rb │ │ ├── max_bucket.rb │ │ ├── min_bucket.rb │ │ ├── percentiles_bucket.rb │ │ ├── bucket_selector.rb │ │ └── extended_stats_bucket.rb │ ├── reverse_nested.rb │ ├── geo_distance.rb │ ├── children.rb │ ├── terms.rb │ └── geo_bounds.rb │ ├── suggest.rb │ └── filters │ ├── match_all.rb │ ├── prefix.rb │ ├── limit.rb │ ├── ids.rb │ ├── type.rb │ ├── script.rb │ ├── exists.rb │ ├── term.rb │ ├── regexp.rb │ ├── geo_distance_range.rb │ ├── terms.rb │ ├── missing.rb │ ├── geo_shape.rb │ ├── range.rb │ └── geo_polygon.rb ├── spec ├── spec_helper.rb └── elasticsearch │ └── dsl │ └── search │ ├── filters │ ├── match_all_spec.rb │ ├── prefix_spec.rb │ ├── terms_spec.rb │ ├── term_spec.rb │ ├── not_spec.rb │ ├── query_spec.rb │ ├── limit_spec.rb │ ├── exists_spec.rb │ ├── missing_spec.rb │ ├── type_spec.rb │ └── geo_polygon_spec.rb │ ├── aggregations │ ├── reverse_nested_spec.rb │ ├── avg_spec.rb │ ├── max_spec.rb │ ├── min_spec.rb │ ├── sum_spec.rb │ ├── value_count_spec.rb │ ├── extended_stats_spec.rb │ ├── filter_spec.rb │ ├── stats_spec.rb │ ├── global_spec.rb │ └── children_spec.rb │ └── queries │ ├── span_term_spec.rb │ ├── terms_spec.rb │ ├── term_spec.rb │ ├── exists_spec.rb │ ├── match_all_spec.rb │ ├── span_or_spec.rb │ ├── prefix_spec.rb │ ├── span_first_spec.rb │ ├── span_multi_spec.rb │ ├── span_not_spec.rb │ └── span_near_spec.rb ├── Gemfile ├── .github └── workflows │ └── main.yml └── test ├── unit ├── dsl_test.rb ├── search_suggest_test.rb └── utils_test.rb └── integration ├── search_options_test.rb └── search_sort_test.rb /CODE_OF_CONDUCT: -------------------------------------------------------------------------------- 1 | See: https://www.elastic.co/community/codeofconduct -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Elasticsearch DSL Ruby - Ruby API for the Elasticsearch Query DSL 2 | Copyright 2021 Elasticsearch B.V. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | To work on the code, clone the repository and install the dependencies: 2 | 3 | ``` 4 | git clone https://github.com/elasticsearch/elasticsearch-dsl-ruby.git 5 | cd elasticsearch-dsl/ 6 | bundle install 7 | ``` 8 | 9 | Use the Rake tasks to run the test suites: 10 | 11 | ``` 12 | bundle exec rake test:unit 13 | bundle exec rake test:integration 14 | ``` 15 | 16 | To launch a separate Elasticsearch server for integration tests, see instructions in the [Elasticsearch Ruby Client](https://github.com/elastic/elasticsearch-ruby/blob/main/CONTRIBUTING.md). 17 | -------------------------------------------------------------------------------- /lib/elasticsearch-dsl.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'elasticsearch/dsl' 19 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/version.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | VERSION = "0.1.10" 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'elasticsearch' 19 | require 'elasticsearch-dsl' 20 | 21 | RSpec.configure do |config| 22 | config.formatter = 'documentation' 23 | config.color = true 24 | end 25 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | source 'https://rubygems.org' 19 | 20 | # Specify your gem's dependencies in elasticsearch-dsl.gemspec 21 | gemspec 22 | 23 | group :development do 24 | gem 'rspec' 25 | if defined?(JRUBY_VERSION) 26 | gem 'pry-nav' 27 | else 28 | gem 'debug' 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: main 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | branches: 8 | - main 9 | jobs: 10 | test-main: 11 | env: 12 | TEST_ES_SERVER: http://localhost:9250 13 | PORT: 9250 14 | strategy: 15 | fail-fast: false 16 | matrix: 17 | ruby: [ '3.2', '3.3', '3.4', 'jruby-9.4', 'jruby-10' ] 18 | runs-on: ubuntu-latest 19 | steps: 20 | - uses: actions/checkout@v4 21 | - name: Increase system limits 22 | run: | 23 | sudo swapoff -a 24 | sudo sysctl -w vm.swappiness=1 25 | sudo sysctl -w fs.file-max=262144 26 | sudo sysctl -w vm.max_map_count=262144 27 | - uses: elastic/elastic-github-actions/elasticsearch@master 28 | with: 29 | stack-version: 9.0.0-SNAPSHOT 30 | security-enabled: false 31 | - uses: ruby/setup-ruby@v1 32 | with: 33 | ruby-version: ${{ matrix.ruby }} 34 | - name: Build 35 | run: | 36 | sudo apt-get update 37 | bundle install 38 | - name: elasticsearch-dsl 39 | run: bundle exec rake test:all 40 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/match_all_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::MatchAll do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(match_all: {}) 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/reverse_nested_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::ReverseNested do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(reverse_nested: {}) 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /test/unit/dsl_test.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'test_helper' 19 | 20 | module Elasticsearch 21 | module Test 22 | class DSLTest < ::Elasticsearch::Test::UnitTestCase 23 | context "The DSL" do 24 | class DummyDSLReceiver 25 | include Elasticsearch::DSL 26 | end 27 | 28 | should "include the module in receiver" do 29 | assert_contains DummyDSLReceiver.included_modules, Elasticsearch::DSL 30 | assert_contains DummyDSLReceiver.included_modules, Elasticsearch::DSL::Search 31 | end 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/utils.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | 21 | # Generic utility methods 22 | # 23 | module Utils 24 | 25 | # Camelize an underscored string 26 | # 27 | # A lightweight version of ActiveSupport's `camelize` 28 | # 29 | # @example 30 | # __camelize('query_string') 31 | # # => 'QueryString' 32 | # 33 | # @api private 34 | # 35 | def __camelize(string) 36 | string.to_s.split('_').map(&:capitalize).join 37 | end 38 | 39 | extend self 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/avg_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Avg do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(avg: {}) 30 | end 31 | end 32 | 33 | context '#initialize' do 34 | 35 | let(:search) do 36 | described_class.new(foo: 'bar') 37 | end 38 | 39 | it 'takes a hash' do 40 | expect(search.to_hash).to eq(avg: { foo: 'bar' }) 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/max_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Max do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(max: {}) 30 | end 31 | end 32 | 33 | context '#initialize' do 34 | 35 | let(:search) do 36 | described_class.new(foo: 'bar') 37 | end 38 | 39 | it 'takes a hash' do 40 | expect(search.to_hash).to eq(max: { foo: 'bar' }) 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/min_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Min do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(min: {}) 30 | end 31 | end 32 | 33 | context '#initialize' do 34 | 35 | let(:search) do 36 | described_class.new(foo: 'bar') 37 | end 38 | 39 | it 'takes a hash' do 40 | expect(search.to_hash).to eq(min: { foo: 'bar' }) 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/sum_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Sum do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(sum: {}) 30 | end 31 | end 32 | 33 | context '#initialize' do 34 | 35 | let(:search) do 36 | described_class.new(foo: 'bar') 37 | end 38 | 39 | it 'takes a hash' do 40 | expect(search.to_hash).to eq(sum: { foo: 'bar' }) 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/value_count_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::ValueCount do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(value_count: {}) 30 | end 31 | end 32 | 33 | context '#initialize' do 34 | 35 | let(:search) do 36 | described_class.new(foo: 'bar') 37 | end 38 | 39 | it 'sets the value' do 40 | expect(search.to_hash).to eq(value_count: { foo: 'bar' }) 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/extended_stats_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::ExtendedStats do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(extended_stats: {}) 30 | end 31 | end 32 | 33 | context '#initialize' do 34 | 35 | let(:search) do 36 | described_class.new(foo: 'bar') 37 | end 38 | 39 | it 'takes a hash' do 40 | expect(search.to_hash).to eq(extended_stats: { foo: 'bar' }) 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /test/unit/search_suggest_test.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'test_helper' 19 | 20 | module Elasticsearch 21 | module Test 22 | class SearchSuggestTest < ::Elasticsearch::Test::UnitTestCase 23 | subject { Elasticsearch::DSL::Search::Suggest.new :foo } 24 | 25 | context "Search suggest" do 26 | should "be an empty hash by default" do 27 | assert_equal({ foo: {} }, subject.to_hash) 28 | end 29 | 30 | should "contain options" do 31 | subject = Elasticsearch::DSL::Search::Suggest.new :foo, boo: 'bam' 32 | assert_equal({ foo: { boo: 'bam' } }, subject.to_hash) 33 | end 34 | end 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/match_all.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which matches all documents 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # match_all 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html 34 | # 35 | class MatchAll 36 | include BaseComponent 37 | 38 | option_method :boost 39 | end 40 | 41 | end 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/prefix_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Prefix do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(prefix: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a hash is provided' do 36 | 37 | let(:search) do 38 | described_class.new(foo: 'bar') 39 | end 40 | 41 | it 'applies the hash' do 42 | expect(search.to_hash).to eq(prefix: { foo: 'bar' }) 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /test/unit/utils_test.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'test_helper' 19 | 20 | module Elasticsearch 21 | module Test 22 | class UtilsTest < ::Elasticsearch::Test::UnitTestCase 23 | context "Utils" do 24 | should "convert a string to camelcase" do 25 | assert_equal 'Foo', Elasticsearch::DSL::Utils.__camelize('foo') 26 | end 27 | 28 | should "convert an underscored string to camelcase" do 29 | assert_equal 'FooBar', Elasticsearch::DSL::Utils.__camelize('foo_bar') 30 | end 31 | 32 | should "convert a symbol" do 33 | assert_equal 'FooBar', Elasticsearch::DSL::Utils.__camelize(:foo_bar) 34 | end 35 | end 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/terms_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Terms do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(terms: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a hash is specified' do 36 | 37 | let(:search) do 38 | described_class.new(foo: ['abc', 'xyz']) 39 | end 40 | 41 | it 'sets the value' do 42 | expect(search.to_hash).to eq(terms: { foo: ['abc', 'xyz'] }) 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/span_term_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::SpanTerm do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(span_term: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a hash is provided' do 36 | 37 | let(:search) do 38 | described_class.new(foo: 'bar') 39 | end 40 | 41 | it 'sets the value' do 42 | expect(search.to_hash[:span_term][:foo]).to eq('bar') 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/terms_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::Terms do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(terms: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a hash is provided' do 36 | 37 | let(:search) do 38 | described_class.new(foo: ['abc', 'xyz']) 39 | end 40 | 41 | it 'sets the value' do 42 | expect(search.to_hash[:terms]).to eq(foo: ['abc', 'xyz']) 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/avg.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-value metric aggregation which returns the average of numeric values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :avg_clicks do 29 | # avg field: 'clicks' 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html 34 | # 35 | class Avg 36 | include BaseComponent 37 | end 38 | 39 | end 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/sum.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-value metric aggregation which returns the sum of numeric values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :sum_clicks do 29 | # sum field: 'clicks' 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html 34 | # 35 | class Sum 36 | include BaseComponent 37 | end 38 | 39 | end 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/max.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-value metric aggregation which returns the maximum value from numeric values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :max_clicks do 29 | # max field: 'clicks' 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html 34 | # 35 | class Max 36 | include BaseComponent 37 | end 38 | 39 | end 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/min.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-value metric aggregation which returns the minimum value from numeric values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :min_clicks do 29 | # min field: 'clicks' 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html 34 | # 35 | class Min 36 | include BaseComponent 37 | end 38 | 39 | end 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/ids.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching the specified IDs 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # ids values: [1, 2, 3] 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html 34 | # 35 | class Ids 36 | include BaseComponent 37 | 38 | option_method :type 39 | option_method :values 40 | end 41 | 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/suggest.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | 22 | # Wraps the `suggest` part of a search definition 23 | # 24 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html 25 | # 26 | class Suggest 27 | include BaseComponent 28 | 29 | def initialize(key, options={}, &block) 30 | @key = key 31 | @options = options 32 | @block = block 33 | end 34 | 35 | # Convert the definition to a Hash 36 | # 37 | # @return [Hash] 38 | # 39 | def to_hash 40 | { @key => @options } 41 | end 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/term.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching the specified term 24 | # 25 | # @note The specified term is *not analyzed* (lowercased, stemmed, etc) 26 | # 27 | # @example 28 | # 29 | # search do 30 | # query do 31 | # term category: 'Opinion' 32 | # end 33 | # end 34 | # 35 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html 36 | # 37 | class Term 38 | include BaseComponent 39 | end 40 | 41 | end 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/value_count.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-value metric aggregation which returns the number of values for the aggregation scope 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :value_count do 29 | # value_count field: 'clicks' 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html 34 | # 35 | class ValueCount 36 | include BaseComponent 37 | end 38 | 39 | end 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/match_all.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which matches on all documents 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # match_all 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-filter.html 38 | # 39 | class MatchAll 40 | include BaseComponent 41 | end 42 | 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/prefix.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching a specified prefix 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # prefix :title do 30 | # value 'dis' 31 | # end 32 | # end 33 | # end 34 | # 35 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html 36 | # 37 | class Prefix 38 | include BaseComponent 39 | 40 | option_method :value 41 | option_method :boost 42 | end 43 | 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/terms.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching the specified terms 24 | # 25 | # @note The specified terms are *not analyzed* (lowercased, stemmed, etc) 26 | # 27 | # @example 28 | # 29 | # search do 30 | # query do 31 | # terms categories: ['World', 'Opinion'] 32 | # end 33 | # end 34 | # 35 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html 36 | # 37 | class Terms 38 | include BaseComponent 39 | end 40 | 41 | end 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/extended_stats.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-value metrics aggregation which returns the extended statistical information on numeric values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :clicks_stats do 29 | # extended_stats field: 'clicks' 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-extendedstats-aggregation.html 34 | # 35 | class ExtendedStats 36 | include BaseComponent 37 | end 38 | 39 | end 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/span_term.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents having a span containing a term 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # span_term title: 'disaster' 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html 34 | # @see https://lucene.apache.org/core/5_0_0/core/org/apache/lucene/search/spans/package-summary.html 35 | # 36 | class SpanTerm 37 | include BaseComponent 38 | end 39 | 40 | end 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/prefix.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents where the field value a specified prefix 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # prefix path: '/usr/local' 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-filter.html 38 | # 39 | class Prefix 40 | include BaseComponent 41 | end 42 | 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/limit.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which limits the number of documents to evaluate 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # limit value: 100 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-limit-filter.html 38 | # 39 | class Limit 40 | include BaseComponent 41 | 42 | option_method :value 43 | end 44 | 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/regexp.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which matches documents matching a regular expression 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # regexp :path do 30 | # value '^/usr/?.*/var' 31 | # end 32 | # end 33 | # end 34 | # 35 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html 36 | # 37 | class Regexp 38 | include BaseComponent 39 | 40 | option_method :value 41 | option_method :boost 42 | option_method :flags 43 | end 44 | 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/wildcard.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching a wildcard expression 24 | # 25 | # @note The expression is *not analyzed* (lowercased, stemmed, etc) 26 | # 27 | # @example 28 | # 29 | # search do 30 | # query do 31 | # wildcard title: 'tw*' 32 | # end 33 | # end 34 | # 35 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html 36 | # 37 | class Wildcard 38 | include BaseComponent 39 | 40 | option_method :value 41 | option_method :boost 42 | end 43 | 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/ids.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents matching the specified IDs 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # ids values: [1, 2, 3] 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-filter.html 38 | # 39 | class Ids 40 | include BaseComponent 41 | 42 | option_method :type 43 | option_method :values 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/filter_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Filter do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(filter: {}) 30 | end 31 | end 32 | 33 | context 'when another aggregation is nested' do 34 | 35 | let(:search) do 36 | described_class.new(terms: { foo: 'bar' }) do 37 | aggregation :sum_clicks do 38 | sum moo: 'bam' 39 | end 40 | end 41 | end 42 | 43 | it 'nests the aggregation in the hash' do 44 | expect(search.to_hash).to eq(filter: { terms: { foo: 'bar' } }, 45 | aggregations: { sum_clicks: { sum: { moo: 'bam' } } }) 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/global.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # Defines a single bucket of all the documents matching a query 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :all_documents do 29 | # global do 30 | # aggregation :avg_clicks do 31 | # avg field: 'clicks' 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html 38 | # 39 | class Global 40 | include BaseAggregationComponent 41 | end 42 | 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/span_multi.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching a multi-term query as a span query 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # span_multi match: { prefix: { name: 'jo' } } 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html 34 | # @see https://lucene.apache.org/core/5_0_0/core/org/apache/lucene/search/spans/package-summary.html 35 | # 36 | class SpanMulti 37 | include BaseComponent 38 | 39 | option_method :match 40 | end 41 | 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/span_first.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents having spans in the beginning of the field 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # span_first match: { span_term: { title: 'disaster' } }, end: 10 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html 34 | # @see https://lucene.apache.org/core/5_0_0/core/org/apache/lucene/search/spans/package-summary.html 35 | # 36 | class SpanFirst 37 | include BaseComponent 38 | 39 | option_method :match 40 | end 41 | 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/template.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which allows to use Mustache templates for query definitions 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # template do 30 | # query match: { content: '{query_string}' } 31 | # params query_string: 'twitter' 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html 37 | # 38 | class Template 39 | include BaseComponent 40 | 41 | option_method :query 42 | option_method :params 43 | end 44 | 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/span_or.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching the union of provided queries 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # span_or clauses: [ { span_term: { title: 'disaster' } }, { span_term: { title: 'health' } } ] 30 | # end 31 | # end 32 | # 33 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-or-query.html 34 | # @see https://lucene.apache.org/core/5_0_0/core/org/apache/lucene/search/spans/package-summary.html 35 | # 36 | class SpanOr 37 | include BaseComponent 38 | 39 | option_method :clauses 40 | end 41 | 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/type.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents matching the specified type 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # type do 32 | # value 'article' 33 | # end 34 | # end 35 | # end 36 | # end 37 | # end 38 | # 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-filter.html 41 | # 42 | class Type 43 | include BaseComponent 44 | 45 | option_method :value 46 | end 47 | 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/script.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents matching the criteria defined with a script 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # script script: "doc['clicks'].value % 4 == 0" 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-filter.html 38 | # 39 | class Script 40 | include BaseComponent 41 | 42 | option_method :script 43 | option_method :params 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/stats_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Stats do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(stats: {}) 30 | end 31 | end 32 | 33 | context '#initialize' do 34 | 35 | let(:search) do 36 | described_class.new(foo: 'bar') 37 | end 38 | 39 | it 'takes a hash' do 40 | expect(search.to_hash).to eq(stats: { foo: 'bar' }) 41 | end 42 | 43 | context 'when a block is provided' do 44 | 45 | let(:search) do 46 | described_class.new do 47 | field 'bar' 48 | end 49 | end 50 | 51 | it 'executes the block' do 52 | expect(search.to_hash).to eq(stats: { field: 'bar' }) 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/exists.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents which have a non-`null` value in the specified field 24 | # (ie. the reverse of the `missing` filter) 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # filtered do 31 | # filter do 32 | # exists field: 'occupation' 33 | # end 34 | # end 35 | # end 36 | # end 37 | # 38 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html 39 | # 40 | class Exists 41 | include BaseComponent 42 | 43 | option_method :field 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/term.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents matching the specified terms 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # term color: 'red' 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @note The specified terms are *not analyzed* (lowercased, stemmed, etc), 38 | # so they must match the indexed terms. 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html 41 | # 42 | class Term 43 | include BaseComponent 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/ip_range.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-bucket aggregation which returns document counts for defined IP ranges 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :ips do 29 | # ip_range do 30 | # field 'ip' 31 | # ranges [ { mask: '10.0.0.0/25' }, { mask: '10.0.0.127/25' } ] 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-iprange-aggregation.html 37 | # 38 | class IpRange 39 | include BaseAggregationComponent 40 | 41 | option_method :field 42 | option_method :ranges 43 | end 44 | 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/geo_shape.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents which fall into a specified geographical shape 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # geo_shape :location do 30 | # shape type: 'envelope', 31 | # coordinates: [[14.2162566185,49.9415476869], [14.7149200439,50.1815123678]] 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html 37 | # 38 | class GeoShape 39 | include BaseComponent 40 | 41 | option_method :shape 42 | option_method :indexed_shape 43 | end 44 | 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/filter.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # Defines a single bucket with documents matching the provided filter, 24 | # usually to define scope for a nested aggregation 25 | # 26 | # @example 27 | # 28 | # search do 29 | # aggregation :clicks_for_tag_one do 30 | # filter terms: { tags: ['one'] } do 31 | # aggregation :sum_clicks do 32 | # sum field: 'clicks' 33 | # end 34 | # end 35 | # end 36 | # end 37 | # 38 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html 39 | # 40 | class Filter 41 | include BaseAggregationComponent 42 | end 43 | 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/regexp.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents matching the specified regular expression 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # regexp :path do 32 | # value '^/usr/?.*/var' 33 | # end 34 | # end 35 | # end 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-filter.html 40 | # 41 | class Regexp 42 | include BaseComponent 43 | 44 | option_method :value 45 | option_method :flags 46 | end 47 | 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/geo_distance_range.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents which fall into a specified geographical distance range 24 | # 25 | # @example Define the filter with a hash 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # geo_distance location: '50.090223,14.399590', gte: '2km', lte: '5km' 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-distance.html 38 | # 39 | class GeoDistanceRange 40 | include BaseComponent 41 | 42 | option_method :lat 43 | option_method :lon 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/match_phrase.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query that analyzes the text and creates a phrase query out of the analyzed text 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # match_phrase :content do 30 | # query 'example content' 31 | # analyzer 'standard' 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html 37 | # 38 | class MatchPhrase 39 | include BaseComponent 40 | 41 | option_method :query 42 | option_method :analyzer 43 | option_method :boost 44 | option_method :slop 45 | end 46 | 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/cardinality.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-value metric aggregation which returns the approximate count of distinct values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :authors do 29 | # cardinality do 30 | # field 'author' 31 | # end 32 | # end 33 | # 34 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html 35 | # 36 | class Cardinality 37 | include BaseComponent 38 | 39 | option_method :field 40 | option_method :precision_threshold 41 | option_method :rehash 42 | option_method :script 43 | option_method :params 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/terms.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents matching any term from the specified list of terms 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # terms tags: ['ruby', 'development'] 32 | # end 33 | # end 34 | # end 35 | # end 36 | # 37 | # @note The specified terms are *not analyzed* (lowercased, stemmed, etc), 38 | # so they must match the indexed terms. 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html 41 | # 42 | class Terms 43 | include BaseComponent 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/match_phrase_prefix.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # The same as match_phrase, except that it allows for prefix matches on the last term in the text 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # match_phrase_prefix :content do 30 | # query 'example content' 31 | # max_expansions 10 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html 37 | # 38 | class MatchPhrasePrefix 39 | include BaseComponent 40 | 41 | option_method :query 42 | option_method :boost 43 | option_method :max_expansions 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/term_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Term do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(term: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a scalar is specified' do 36 | 37 | let(:search) do 38 | described_class.new(message: 'test') 39 | end 40 | 41 | it 'sets the value' do 42 | expect(search.to_hash).to eq(term: { message: 'test' }) 43 | end 44 | end 45 | 46 | context 'when a hash is specified' do 47 | 48 | let(:search) do 49 | described_class.new(message: { query: 'test' }) 50 | end 51 | 52 | it 'sets the value' do 53 | expect(search.to_hash).to eq(term: { message: { query: 'test' } }) 54 | end 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /test/integration/search_options_test.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'test_helper' 19 | 20 | module Elasticsearch 21 | module Test 22 | class SearchOptionsIntegrationTest < ::Elasticsearch::Test::IntegrationTestCase 23 | include Elasticsearch::DSL::Search 24 | 25 | context "Search options" do 26 | setup do 27 | @client.indices.create index: 'test' 28 | @client.index index: 'test', id: '1', body: { title: 'Test' } 29 | @client.index index: 'test', id: '2', body: { title: 'Rest' } 30 | @client.indices.refresh index: 'test' 31 | end 32 | 33 | should "explain the match" do 34 | response = @client.search index: 'test', body: search { 35 | query { match title: 'test' } 36 | explain true 37 | }.to_hash 38 | 39 | assert_equal 1, response['hits']['total']['value'] 40 | assert_not_nil response['hits']['hits'][0]['_explanation'] 41 | end 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/missing.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents which have a `null` value in the specified field 24 | # (ie. the reverse of the `exists` filter) 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # filtered do 31 | # filter do 32 | # missing field: 'occupation' 33 | # end 34 | # end 35 | # end 36 | # end 37 | # 38 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-missing-filter.html 39 | # 40 | class Missing 41 | include BaseComponent 42 | 43 | option_method :field 44 | option_method :existence 45 | option_method :null_value 46 | end 47 | 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/term_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::Term do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(term: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a String is provided' do 36 | 37 | let(:search) do 38 | described_class.new(message: 'test') 39 | end 40 | 41 | it 'executes the block' do 42 | expect(search.to_hash[:term][:message]).to eq('test') 43 | end 44 | end 45 | 46 | context 'when a hash is provided' do 47 | 48 | let(:search) do 49 | described_class.new(message: { query: 'test', boost: 2 }) 50 | end 51 | 52 | it 'sets the value' do 53 | expect(search.to_hash[:term][:message]).to eq(query: 'test', boost: 2) 54 | end 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/percentiles.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-value metrics aggregation which calculates percentiles on numeric values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :load_time_outliers do 29 | # percentiles do 30 | # field 'load_time' 31 | # end 32 | # end 33 | # end 34 | # 35 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html 36 | # 37 | class Percentiles 38 | include BaseComponent 39 | 40 | option_method :field 41 | option_method :percents 42 | option_method :script 43 | option_method :params 44 | option_method :compression 45 | end 46 | 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/boosting.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which will decrease the score of documents matching the `negative` query 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # boosting do 30 | # positive terms: { amenities: ['wifi', 'pets'] } 31 | # negative terms: { amenities: ['pool'] } 32 | # negative_boost 0.5 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html 38 | # 39 | class Boosting 40 | include BaseComponent 41 | 42 | option_method :positive 43 | option_method :negative 44 | option_method :negative_boost 45 | end 46 | 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/nested.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-bucket aggregation which allows to aggregate on nested fields 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :offers do 29 | # nested do 30 | # path 'offers' 31 | # aggregation :min_price do 32 | # min field: 'offers.price' 33 | # end 34 | # end 35 | # end 36 | # end 37 | # 38 | # See the integration test for a full example. 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html 41 | # 42 | class Nested 43 | include BaseAggregationComponent 44 | 45 | option_method :path 46 | end 47 | 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/not_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Not do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(not: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a hash is provided' do 36 | 37 | let(:search) do 38 | described_class.new(filters: [ { term: { foo: 'bar' } } ]) 39 | end 40 | 41 | it 'applies the hash' do 42 | expect(search.to_hash).to eq(not: { filters: [ { term: { foo: 'bar' } } ] }) 43 | end 44 | end 45 | 46 | context 'when a block is provided' do 47 | 48 | let(:search) do 49 | described_class.new do 50 | term foo: 'bar' 51 | end 52 | end 53 | 54 | it 'executes the block' do 55 | expect(search.to_hash).to eq(not: { term: { foo: 'bar' } }) 56 | end 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/query_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Query do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(query: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a hash is provided' do 36 | 37 | let(:search) do 38 | described_class.new(query_string: { query: 'foo' }) 39 | end 40 | 41 | it 'applies the hash' do 42 | expect(search.to_hash).to eq(query: { query_string: { query: 'foo' } }) 43 | end 44 | end 45 | 46 | context 'when a block is provided' do 47 | 48 | let(:search) do 49 | described_class.new do 50 | match foo: 'bar' 51 | end 52 | end 53 | 54 | it 'executes the block' do 55 | expect(search.to_hash).to eq(query: { match: { foo: 'bar' } }) 56 | end 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/geohash_grid.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-bucket aggregation which will return document counts for geohash grid cells 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :venue_distributions do 29 | # geohash_grid do 30 | # field :location 31 | # precision 5 32 | # end 33 | # end 34 | # end 35 | # 36 | # See the integration test for a full example. 37 | # 38 | # @see https://www.elastic.co/guide/en/elasticsearch/guide/current/geohash-grid-agg.html 39 | # 40 | class GeohashGrid 41 | include BaseAggregationComponent 42 | 43 | option_method :field 44 | option_method :precision 45 | option_method :size 46 | option_method :shard_size 47 | end 48 | 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/span_near.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching spans near each other 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # span_near clauses: [ { span_term: { title: 'disaster' } }, { span_term: { title: 'health' } } ], 30 | # slop: 10 31 | # end 32 | # end 33 | # 34 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html 35 | # @see https://lucene.apache.org/core/5_0_0/core/org/apache/lucene/search/spans/package-summary.html 36 | # 37 | class SpanNear 38 | include BaseComponent 39 | 40 | option_method :span_near 41 | option_method :slop 42 | option_method :in_order 43 | option_method :collect_payloads 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/histogram.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-bucket aggregation which returns document counts for a defined numerical interval 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :age do 29 | # histogram do 30 | # field 'age' 31 | # interval 5 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html 37 | # 38 | class Histogram 39 | include BaseAggregationComponent 40 | 41 | option_method :field 42 | option_method :interval 43 | option_method :min_doc_count 44 | option_method :extended_bounds 45 | option_method :order 46 | option_method :keyed 47 | end 48 | 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/percentile_ranks.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-value metrics aggregation which calculates percentile ranks on numeric values 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :load_time_outliers do 29 | # percentile_ranks do 30 | # field 'load_time' 31 | # values [ 15, 30 ] 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-rank-aggregation.html 37 | # 38 | class PercentileRanks 39 | include BaseComponent 40 | 41 | option_method :field 42 | option_method :values 43 | option_method :script 44 | option_method :params 45 | option_method :compression 46 | end 47 | 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/top_hits.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A metric aggregator which returns the most relevant documents per bucket 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :tags do 29 | # terms do 30 | # field 'tags' 31 | # 32 | # aggregation :top_hits do 33 | # top_hits sort: [ clicks: { order: 'desc' } ], _source: { include: 'title' } 34 | # end 35 | # end 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html 40 | # 41 | class TopHits 42 | include BaseComponent 43 | 44 | option_method :from 45 | option_method :size 46 | option_method :sort 47 | end 48 | 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/dis_max.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which will score the documents based on the highest score of any individual specified query, 24 | # not by summing the scores (as eg. a `bool` query would) 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # dis_max do 31 | # queries [ 32 | # { match: { title: 'albino' } }, 33 | # { match: { content: 'elephant' } } 34 | # ] 35 | # end 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/guide/current/_best_fields.html 40 | # 41 | class DisMax 42 | include BaseComponent 43 | 44 | option_method :queries 45 | option_method :boost 46 | option_method :tie_breaker 47 | end 48 | 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/span_not.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which discards matching documents which overlap with another query 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # span_not include: { span_term: { title: 'disaster' } }, 30 | # exclude: { span_term: { title: 'health' } } 31 | # end 32 | # end 33 | # 34 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html 35 | # @see https://lucene.apache.org/core/5_0_0/core/org/apache/lucene/search/spans/package-summary.html 36 | # 37 | class SpanNot 38 | include BaseComponent 39 | 40 | option_method :include 41 | option_method :exclude 42 | option_method :pre 43 | option_method :post 44 | option_method :dist 45 | end 46 | 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/indices.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which executes a custom query only for documents in specified indices, 24 | # and optionally another query for documents in other indices 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # indices do 31 | # indices ['audio', 'video'] 32 | # query match: { artist: 'Fugazi' } 33 | # no_match_query match: { label: 'Dischord' } 34 | # end 35 | # end 36 | # end 37 | # 38 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-indices-query.html 39 | # 40 | class Indices 41 | include BaseComponent 42 | 43 | option_method :indices 44 | option_method :query 45 | option_method :no_match_query 46 | end 47 | 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/geo_shape.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents which fall into a specified geographical shape 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # geo_shape :location do 32 | # shape type: 'envelope', 33 | # coordinates: [[14.2162566185,49.9415476869], [14.7149200439,50.1815123678]] 34 | # end 35 | # end 36 | # end 37 | # end 38 | # end 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-filter.html 41 | # 42 | class GeoShape 43 | include BaseComponent 44 | 45 | option_method :shape 46 | option_method :indexed_shape 47 | end 48 | 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/exists_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::Exists do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(exists: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | describe '#field' do 40 | 41 | before do 42 | search.field('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:exists][:field]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new do 57 | field 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash[:exists][:field]).to eq('bar') 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/limit_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Limit do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(limit: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new(:foo) 37 | end 38 | 39 | describe '#value' do 40 | 41 | before do 42 | search.value('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:limit][:foo][:value]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new do 57 | value 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash).to eq(limit: { value: 'bar' }) 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/exists_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Exists do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(exists: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new(:foo) 37 | end 38 | 39 | describe '#field' do 40 | 41 | before do 42 | search.field('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:exists][:foo][:field]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new do 57 | field 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash).to eq(exists: { field: 'bar' }) 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/date_range.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-bucket aggregation which returns document counts for custom date ranges 24 | # 25 | # @example 26 | # 27 | # search do 28 | # aggregation :compare_to_last_year do 29 | # date_range do 30 | # field 'published_at' 31 | # ranges [ 32 | # { from: 'now-1M/M', to: 'now/M' }, 33 | # { from: 'now-13M/M', to: 'now-12M/M' } 34 | # ] 35 | # end 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html 40 | # 41 | class DateRange 42 | include BaseAggregationComponent 43 | 44 | option_method :field 45 | option_method :format 46 | option_method :ranges 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/fuzzy_like_this_field.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents which are similar to the specified text, 24 | # executed on a single field 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # fuzzy_like_this_field :content do 31 | # like_text 'Eyjafjallajökull' 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-flt-field-query.html 37 | # 38 | class FuzzyLikeThisField 39 | include BaseComponent 40 | 41 | option_method :like_text 42 | option_method :fuzziness 43 | option_method :analyzer 44 | option_method :max_query_terms 45 | option_method :prefix_length 46 | option_method :boost 47 | option_method :ignore_tf 48 | end 49 | 50 | end 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/missing_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Missing do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(missing: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new(:foo) 37 | end 38 | 39 | describe '#field' do 40 | 41 | before do 42 | search.field('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:missing][:foo][:field]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new do 57 | field 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash).to eq(missing: { field: 'bar' }) 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/type_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::Type do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(type: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new(:foo) 37 | end 38 | 39 | describe '#value' do 40 | 41 | before do 42 | search.value('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:type][:foo][:value]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new(:foo) do 57 | value 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash).to eq(type: { foo: { value: 'bar' } }) 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/match_all_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::MatchAll do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(match_all: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | describe '#boost' do 40 | 41 | before do 42 | search.boost('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:match_all][:boost]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new do 57 | boost 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash).to eq(match_all: { boost: 'bar' }) 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/missing.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single bucket aggregation that creates a bucket of all documents 24 | # which are missing a value for the field 25 | # 26 | # @example Passing the options as a Hash 27 | # 28 | # aggregation :articles_without_tags do 29 | # missing field: 'tags' 30 | # end 31 | # 32 | # @example Passing the options as a block 33 | # 34 | # search do 35 | # aggregation :articles_without_tags do 36 | # missing do 37 | # field 'tags' 38 | # end 39 | # end 40 | # end 41 | # 42 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-bucket-missing-aggregation.html 43 | # 44 | class Missing 45 | include BaseAggregationComponent 46 | 47 | option_method :field 48 | end 49 | 50 | end 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/global_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Global do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(global: {}) 30 | end 31 | end 32 | 33 | describe '#initialize' do 34 | 35 | context 'when a block is provided' do 36 | 37 | let(:search) do 38 | described_class.new do 39 | end 40 | end 41 | 42 | it 'executes the block' do 43 | expect(search.to_hash).to eq(global: {}) 44 | end 45 | end 46 | 47 | context 'when another aggregation is nested' do 48 | 49 | let(:search) do 50 | described_class.new do 51 | aggregation :foo do 52 | terms field: "bar" 53 | end 54 | end 55 | end 56 | 57 | it 'nests the aggregation in the hash' do 58 | expect(search.to_hash).to eq(aggregations: { foo: { terms: { field: "bar" } } }, global: {}) 59 | end 60 | end 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/range.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents that have terms in a specified range 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # range :age do 32 | # gte 10 33 | # lte 20 34 | # end 35 | # end 36 | # end 37 | # end 38 | # end 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-filter.html 41 | # 42 | class Range 43 | include BaseComponent 44 | 45 | option_method :gte 46 | option_method :gt 47 | option_method :lte 48 | option_method :lt 49 | option_method :boost 50 | option_method :time_zone 51 | option_method :format 52 | end 53 | 54 | end 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/fuzzy_like_this.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents which are similar to the specified text 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # fuzzy_like_this do 30 | # like_text 'Eyjafjallajökull' 31 | # fields [:title, :abstract, :content] 32 | # end 33 | # end 34 | # end 35 | # 36 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-flt-query.html 37 | # 38 | class FuzzyLikeThis 39 | include BaseComponent 40 | 41 | option_method :fields 42 | option_method :like_text 43 | option_method :fuzziness 44 | option_method :analyzer 45 | option_method :max_query_terms 46 | option_method :prefix_length 47 | option_method :boost 48 | option_method :ignore_tf 49 | end 50 | 51 | end 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/aggregations/children_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Aggregations::Children do 21 | 22 | let(:search) do 23 | described_class.new 24 | end 25 | 26 | describe '#to_hash' do 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(children: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new(:foo) 37 | end 38 | 39 | describe '#type' do 40 | 41 | before do 42 | search.type('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:children][:foo][:type]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new(:foo) do 57 | type 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash).to eq({ children: { foo: { type: 'bar' } } }) 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/filters.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-bucket aggregation which defines multiple buckets matching the provided filters, 24 | # usually to define scope for a nested aggregation 25 | # 26 | # @example 27 | # 28 | # search do 29 | # aggregation :avg_clicks_per_tag_one_and_two do 30 | # filters do 31 | # filters one: { terms: { tags: ['one'] } }, 32 | # two: { terms: { tags: ['two'] } } 33 | # 34 | # aggregation :avg do 35 | # avg field: 'clicks' 36 | # end 37 | # end 38 | # end 39 | # end 40 | # 41 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html 42 | # 43 | class Filters 44 | include BaseAggregationComponent 45 | 46 | option_method :filters 47 | end 48 | 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/stats.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-value metrics aggregation which returns statistical information on numeric values 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # search do 28 | # aggregation :clicks_stats do 29 | # stats field: 'clicks' 30 | # end 31 | # end 32 | # 33 | # @example Passing the options as a block 34 | # 35 | # search do 36 | # aggregation :clicks_stats do 37 | # stats do 38 | # field 'clicks' 39 | # end 40 | # end 41 | # end 42 | # 43 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html 44 | # 45 | class Stats 46 | include BaseComponent 47 | 48 | option_method :field 49 | option_method :script 50 | end 51 | 52 | end 53 | end 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/filters/geo_polygon_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Filters::GeoPolygon do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(geo_polygon: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new(:foo) 37 | end 38 | 39 | describe '#points' do 40 | 41 | before do 42 | search.points('bar') 43 | end 44 | 45 | it 'applies the option' do 46 | expect(search.to_hash[:geo_polygon][:foo][:points]).to eq('bar') 47 | end 48 | end 49 | end 50 | 51 | describe '#initialize' do 52 | 53 | context 'when a block is provided' do 54 | 55 | let(:search) do 56 | described_class.new :foo do 57 | points 'bar' 58 | end 59 | end 60 | 61 | it 'executes the block' do 62 | expect(search.to_hash).to eq(geo_polygon: { foo: { points: 'bar' } }) 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /test/integration/search_sort_test.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'test_helper' 19 | 20 | module Elasticsearch 21 | module Test 22 | class SortingIntegrationTest < ::Elasticsearch::Test::IntegrationTestCase 23 | include Elasticsearch::DSL::Search 24 | 25 | context "Sorting integration" do 26 | setup do 27 | @client.indices.create index: 'test' 28 | @client.index index: 'test', id: '1', body: { tags: ['one'], clicks: 15 } 29 | @client.index index: 'test', id: '2', body: { tags: ['one', 'two'], clicks: 5 } 30 | @client.index index: 'test', id: '3', body: { tags: ['one', 'three'], clicks: 20 } 31 | @client.indices.refresh index: 'test' 32 | end 33 | 34 | context "sorting by clicks" do 35 | should "return documents in order" do 36 | response = @client.search index: 'test', body: search { 37 | sort do 38 | by :clicks, order: 'desc' 39 | end 40 | }.to_hash 41 | 42 | assert_same_elements ['3', '1', '2'], response['hits']['hits'].map { |d| d['_id'] } 43 | end 44 | end 45 | 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/cumulative_sum.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A parent pipeline aggregation which calculates the cumulative sum of a specified metric in a parent histogram (or date_histogram) aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :cumulative_sales do 28 | # cumulative_sum buckets_path: 'sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :cumulative_sales do 34 | # cumulative_sum do 35 | # buckets_path 'sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html 40 | # 41 | class CumulativeSum 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :format 46 | end 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/derivative.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A parent pipeline aggregation which calculates the derivative of a specified metric in a parent histogram (or date_histogram) aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :sales_deriv do 28 | # derivative buckets_path: 'sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :sales_deriv do 34 | # derivative do 35 | # buckets_path 'sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-derivative-aggregation.html 40 | # 41 | class Derivative 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :gap_policy 46 | option_method :format 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/reverse_nested.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-bucket aggregation which allows to aggregate on "parent" documents 24 | # from the nested documents 25 | # 26 | # @example 27 | # 28 | # search do 29 | # aggregation :offers do 30 | # nested do 31 | # path 'offers' 32 | # aggregation :top_categories do 33 | # reverse_nested do 34 | # aggregation :top_category_per_offer do 35 | # terms field: 'category' 36 | # end 37 | # end 38 | # end 39 | # end 40 | # end 41 | # end 42 | # 43 | # See the integration test for a full example. 44 | # 45 | # @see https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-aggregation.html 46 | # 47 | class ReverseNested 48 | include BaseAggregationComponent 49 | end 50 | 51 | end 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/avg_bucket.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A sibling pipeline aggregation which calculates the (mean) average value of a specified metric in a sibling aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :avg_monthly_sales do 28 | # avg_bucket buckets_path: 'sales_per_month>sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :avg_monthly_sales do 34 | # avg_bucket do 35 | # buckets_path 'sales_per_month>sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html 40 | # 41 | class AvgBucket 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :gap_policy 46 | option_method :format 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/sum_bucket.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A sibling pipeline aggregation which calculates the sum across all bucket of a specified metric in a sibling aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :sum_monthly_sales do 28 | # sum_bucket buckets_path: 'sales_per_month>sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :sum_monthly_sales do 34 | # sum_bucket do 35 | # buckets_path 'sales_per_month>sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html 40 | # 41 | class SumBucket 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :gap_policy 46 | option_method :format 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/span_or_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::SpanOr do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(span_or: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | [ 'clauses' ].each do |option| 40 | 41 | describe "##{option}" do 42 | 43 | before do 44 | search.send(option, 'bar') 45 | end 46 | 47 | it 'applies the option' do 48 | expect(search.to_hash[:span_or][option.to_sym]).to eq('bar') 49 | end 50 | end 51 | end 52 | end 53 | 54 | describe '#initialize' do 55 | 56 | context 'when a block is provided' do 57 | 58 | let(:search) do 59 | described_class.new do 60 | clauses 'bar' 61 | end 62 | end 63 | 64 | it 'executes the block' do 65 | expect(search.to_hash[:span_or][:clauses]).to eq('bar') 66 | end 67 | end 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/prefix_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::Prefix do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(prefix: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | [ 'value', 40 | 'boost'].each do |option| 41 | 42 | describe "##{option}" do 43 | 44 | before do 45 | search.send(option, 'bar') 46 | end 47 | 48 | it 'applies the option' do 49 | expect(search.to_hash[:prefix][option.to_sym]).to eq('bar') 50 | end 51 | end 52 | end 53 | end 54 | 55 | describe '#initialize' do 56 | 57 | context 'when a block is provided' do 58 | 59 | let(:search) do 60 | described_class.new do 61 | value 'bar' 62 | end 63 | end 64 | 65 | it 'executes the block' do 66 | expect(search.to_hash[:prefix][:value]).to eq('bar') 67 | end 68 | end 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/span_first_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::SpanFirst do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(span_first: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | [ 'match' ].each do |option| 40 | 41 | describe "##{option}" do 42 | 43 | before do 44 | search.send(option, 'bar') 45 | end 46 | 47 | it 'applies the option' do 48 | expect(search.to_hash[:span_first][option.to_sym]).to eq('bar') 49 | end 50 | end 51 | end 52 | end 53 | 54 | describe '#initialize' do 55 | 56 | context 'when a block is provided' do 57 | 58 | let(:search) do 59 | described_class.new do 60 | match 'bar' 61 | end 62 | end 63 | 64 | it 'executes the block' do 65 | expect(search.to_hash[:span_first][:match]).to eq('bar') 66 | end 67 | end 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/span_multi_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::SpanMulti do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(span_multi: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | [ 'match' ].each do |option| 40 | 41 | describe "##{option}" do 42 | 43 | before do 44 | search.send(option, 'bar') 45 | end 46 | 47 | it 'applies the option' do 48 | expect(search.to_hash[:span_multi][option.to_sym]).to eq('bar') 49 | end 50 | end 51 | end 52 | end 53 | 54 | describe '#initialize' do 55 | 56 | context 'when a block is provided' do 57 | 58 | let(:search) do 59 | described_class.new do 60 | match 'bar' 61 | end 62 | end 63 | 64 | it 'executes the block' do 65 | expect(search.to_hash[:span_multi][:match]).to eq('bar') 66 | end 67 | end 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/stats_bucket.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A sibling pipeline aggregation which calculates a variety of stats across all bucket of a specified metric in a sibling aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :stats_monthly_sales do 28 | # stats_bucket buckets_path: 'sales_per_month>sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :stats_monthly_sales do 34 | # stats_bucket do 35 | # buckets_path 'sales_per_month>sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-stats-bucket-aggregation.html 40 | # 41 | class StatsBucket 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :gap_policy 46 | option_method :format 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/common.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which executes the search for low frequency terms first, and high frequency ("common") 24 | # terms second 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # common :body do 31 | # query 'shakespeare to be or not to be' 32 | # end 33 | # end 34 | # end 35 | # 36 | # This query is frequently used when a stopwords-based approach loses too much recall and/or precision. 37 | # 38 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html 39 | # 40 | class Common 41 | include BaseComponent 42 | 43 | option_method :query 44 | option_method :cutoff_frequency 45 | option_method :low_freq_operator 46 | option_method :minimum_should_match 47 | option_method :boost 48 | option_method :analyzer 49 | option_method :disable_coord 50 | end 51 | 52 | end 53 | end 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/serial_diff.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # Serial differencing is a technique where values in a time series are subtracted from itself at different time lags or periods. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :thirtieth_difference do 28 | # serial_diff buckets_path: 'the_sum' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :thirtieth_difference do 34 | # serial_diff do 35 | # buckets_path 'the_sum' 36 | # lag 30 37 | # end 38 | # end 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html 41 | # 42 | class SerialDiff 43 | include BaseAggregationComponent 44 | 45 | option_method :buckets_path 46 | option_method :lag 47 | option_method :gap_policy 48 | option_method :format 49 | end 50 | end 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/max_bucket.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A sibling pipeline aggregation which identifies the bucket(s) with the maximum value of a specified metric in a sibling aggregation and outputs both the value and the key(s) of the bucket(s). 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :max_monthly_sales do 28 | # max_bucket buckets_path: 'sales_per_month>sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :max_monthly_sales do 34 | # max_bucket do 35 | # buckets_path 'sales_per_month>sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-max-bucket-aggregation.html 40 | # 41 | class MaxBucket 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :gap_policy 46 | option_method :format 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/min_bucket.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A sibling pipeline aggregation which identifies the bucket(s) with the minimum value of a specified metric in a sibling aggregation and outputs both the value and the key(s) of the bucket(s). 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :min_monthly_sales do 28 | # min_bucket buckets_path: 'sales_per_month>sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :min_monthly_sales do 34 | # min_bucket do 35 | # buckets_path 'sales_per_month>sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-min-bucket-aggregation.html 40 | # 41 | class MinBucket 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :gap_policy 46 | option_method :format 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/geo_distance.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-bucket aggregation which will return document counts for distance perimeters, 24 | # defined as ranges 25 | # 26 | # @example 27 | # 28 | # search do 29 | # aggregation :venue_distances do 30 | # geo_distance do 31 | # field :location 32 | # origin '38.9126352,1.4350621' 33 | # unit 'km' 34 | # ranges [ { to: 1 }, { from: 1, to: 5 }, { from: 5, to: 10 }, { from: 10 } ] 35 | # end 36 | # end 37 | # end 38 | # 39 | # See the integration test for a full example. 40 | # 41 | # @see https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-distance-agg.html 42 | # 43 | class GeoDistance 44 | include BaseAggregationComponent 45 | 46 | option_method :field 47 | option_method :origin 48 | option_method :ranges 49 | option_method :unit 50 | option_method :distance_type 51 | end 52 | 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/span_not_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::SpanNot do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(span_not: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | [ 'include', 40 | 'exclude', 41 | 'pre', 42 | 'post', 43 | 'dist' ].each do |option| 44 | 45 | describe "##{option}" do 46 | 47 | before do 48 | search.send(option, 'bar') 49 | end 50 | 51 | it 'applies the option' do 52 | expect(search.to_hash[:span_not][option.to_sym]).to eq('bar') 53 | end 54 | end 55 | end 56 | end 57 | 58 | describe '#initialize' do 59 | 60 | context 'when a block is provided' do 61 | 62 | let(:search) do 63 | described_class.new do 64 | include 'bar' 65 | end 66 | end 67 | 68 | it 'executes the block' do 69 | expect(search.to_hash[:span_not][:include]).to eq('bar') 70 | end 71 | end 72 | end 73 | end 74 | -------------------------------------------------------------------------------- /spec/elasticsearch/dsl/search/queries/span_near_spec.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | require 'spec_helper' 19 | 20 | describe Elasticsearch::DSL::Search::Queries::SpanNear do 21 | 22 | describe '#to_hash' do 23 | 24 | let(:search) do 25 | described_class.new 26 | end 27 | 28 | it 'can be converted to a hash' do 29 | expect(search.to_hash).to eq(span_near: {}) 30 | end 31 | end 32 | 33 | context 'when options methods are called' do 34 | 35 | let(:search) do 36 | described_class.new 37 | end 38 | 39 | [ 'span_near', 40 | 'slop', 41 | 'in_order', 42 | 'collect_payloads' ].each do |option| 43 | 44 | describe "##{option}" do 45 | 46 | before do 47 | search.send(option, 'bar') 48 | end 49 | 50 | it 'applies the option' do 51 | expect(search.to_hash[:span_near][option.to_sym]).to eq('bar') 52 | end 53 | end 54 | end 55 | end 56 | 57 | describe '#initialize' do 58 | 59 | context 'when a block is provided' do 60 | 61 | let(:search) do 62 | described_class.new do 63 | span_near 'bar' 64 | end 65 | end 66 | 67 | it 'executes the block' do 68 | expect(search.to_hash[:span_near][:span_near]).to eq('bar') 69 | end 70 | end 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/children.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A single-bucket aggregation which allows to aggregate from buckets on parent documents 24 | # to buckets on the children documents 25 | # 26 | # @example Return the top commenters per article category 27 | # 28 | # search do 29 | # aggregation :top_categories do 30 | # terms field: 'category' do 31 | # aggregation :comments do 32 | # children type: 'comment' do 33 | # aggregation :top_authors do 34 | # terms field: 'author' 35 | # end 36 | # end 37 | # end 38 | # end 39 | # end 40 | # end 41 | # 42 | # 43 | # See the integration test for a full example. 44 | # 45 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html 46 | # 47 | class Children 48 | include BaseAggregationComponent 49 | 50 | option_method :type 51 | end 52 | 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/filters/geo_polygon.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Filters 22 | 23 | # A filter which returns documents which fall into a specified geographical polygon 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # filtered do 30 | # filter do 31 | # geo_polygon :location do 32 | # points [ 33 | # [14.2244355,49.9419006], 34 | # [14.2244355,50.1774301], 35 | # [14.7067869,50.1774301], 36 | # [14.7067869,49.9419006], 37 | # [14.2244355,49.9419006] 38 | # ] 39 | # end 40 | # end 41 | # end 42 | # end 43 | # end 44 | # 45 | # See the integration test for a working example. 46 | # 47 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-filter.html 48 | # 49 | class GeoPolygon 50 | include BaseComponent 51 | 52 | option_method :points 53 | end 54 | 55 | end 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/fuzzy.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which uses a Levenshtein distance on string fields and plus-minus margin on numerical 24 | # fields to match documents 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # fuzzy :name do 31 | # value 'Eyjafjallajökull' 32 | # end 33 | # end 34 | # end 35 | # 36 | # @example 37 | # 38 | # search do 39 | # query do 40 | # fuzzy :published_on do 41 | # value '2014-01-01' 42 | # fuzziness '7d' 43 | # end 44 | # end 45 | # end 46 | # 47 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html 48 | # 49 | class Fuzzy 50 | include BaseComponent 51 | 52 | option_method :value 53 | option_method :boost 54 | option_method :fuzziness 55 | option_method :prefix_length 56 | option_method :max_expansions 57 | end 58 | 59 | end 60 | end 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/simple_query_string.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # A query which returns documents matching a simplified query string syntax 24 | # 25 | # @example 26 | # 27 | # search do 28 | # query do 29 | # simple_query_string do 30 | # query 'disaster -health' 31 | # fields ['title^5', 'abstract', 'content'] 32 | # default_operator 'and' 33 | # end 34 | # end 35 | # end 36 | # 37 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html 38 | # 39 | class SimpleQueryString 40 | include BaseComponent 41 | 42 | option_method :query 43 | option_method :fields 44 | option_method :default_operator 45 | option_method :analyzer 46 | option_method :flags 47 | option_method :analyze_wildcard 48 | option_method :lenient 49 | option_method :minimum_should_match 50 | option_method :quote_field_suffix 51 | option_method :all_fields 52 | end 53 | 54 | end 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/percentiles_bucket.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A sibling pipeline aggregation which calculates percentiles across all bucket of a specified metric in a sibling aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :sum_monthly_sales do 28 | # percentiles_bucket buckets_path: 'sales_per_month>sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :sum_monthly_sales do 34 | # percentiles_bucket do 35 | # buckets_path 'sales_per_month>sales' 36 | # percents [25.0 50.0 75.0] 37 | # end 38 | # end 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html 41 | # 42 | class PercentilesBucket 43 | include BaseAggregationComponent 44 | 45 | option_method :buckets_path 46 | option_method :gap_policy 47 | option_method :format 48 | option_method :percents 49 | end 50 | end 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/bucket_selector.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A parent pipeline aggregation which executes a script which determines whether the current bucket will be retained in the parent multi-bucket aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :sales_bucket_filter do 28 | # bucket_selector buckets_path: { totalSales: 'total_sales' }, script: 'totalSales <= 50' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :sales_bucket_filter do 34 | # bucket_selector do 35 | # buckets_path totalSales: 'total_sales' 36 | # script 'totalSales <= 50' 37 | # end 38 | # end 39 | # 40 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html 41 | # 42 | class BucketSelector 43 | include BaseAggregationComponent 44 | 45 | option_method :buckets_path 46 | option_method :script 47 | option_method :gap_policy 48 | end 49 | end 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/queries/exists.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Queries 22 | 23 | # Returns documents that have at least one non-null value in the field. 24 | # 25 | # @example Find documents with non-empty "name" property 26 | # 27 | # search do 28 | # query do 29 | # exists do 30 | # field 'name' 31 | # end 32 | # end 33 | # end 34 | # 35 | # @note The "Exists" query can be used as a "Missing" query in a "Bool" query "Must Not" context. 36 | # 37 | # @example Find documents with an empty "name" property 38 | # 39 | # search do 40 | # query do 41 | # bool do 42 | # must_not do 43 | # exists do 44 | # field 'name' 45 | # end 46 | # end 47 | # end 48 | # end 49 | # end 50 | # 51 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-exists-query.html 52 | # 53 | class Exists 54 | include BaseComponent 55 | 56 | option_method :field 57 | end 58 | end 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/terms.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A multi-bucket aggregation which returns the collection of terms and their document counts 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :tags do 28 | # terms field: 'tags' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # search do 34 | # aggregation :tags do 35 | # terms do 36 | # field 'tags' 37 | # end 38 | # end 39 | # end 40 | # 41 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html 42 | # 43 | class Terms 44 | include BaseAggregationComponent 45 | 46 | option_method :field 47 | option_method :size 48 | option_method :shard_size 49 | option_method :order 50 | option_method :min_doc_count 51 | option_method :shard_min_doc_count 52 | option_method :script 53 | option_method :include 54 | option_method :exclude 55 | end 56 | 57 | end 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/pipeline/extended_stats_bucket.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # A sibling pipeline aggregation which calculates a variety of stats across all bucket of a specified metric in a sibling aggregation. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation. 24 | # 25 | # @example Passing the options as a Hash 26 | # 27 | # aggregation :stats_monthly_sales do 28 | # extended_stats_bucket buckets_path: 'sales_per_month>sales' 29 | # end 30 | # 31 | # @example Passing the options as a block 32 | # 33 | # aggregation :stats_monthly_sales do 34 | # extended_stats_bucket do 35 | # buckets_path 'sales_per_month>sales' 36 | # end 37 | # end 38 | # 39 | # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-extended-stats-bucket-aggregation.html 40 | # 41 | class ExtendedStatsBucket 42 | include BaseAggregationComponent 43 | 44 | option_method :buckets_path 45 | option_method :gap_policy 46 | option_method :format 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/elasticsearch/dsl/search/aggregations/geo_bounds.rb: -------------------------------------------------------------------------------- 1 | # Licensed to Elasticsearch B.V. under one or more contributor 2 | # license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright 4 | # ownership. Elasticsearch B.V. licenses this file to you under 5 | # the Apache License, Version 2.0 (the "License"); you may 6 | # not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | 18 | module Elasticsearch 19 | module DSL 20 | module Search 21 | module Aggregations 22 | 23 | # An aggregation which will calculate the smallest bounding box required to encapsulate 24 | # all of the documents matching the query 25 | # 26 | # @example 27 | # 28 | # search do 29 | # query do 30 | # filtered do 31 | # filter do 32 | # geo_bounding_box :location do 33 | # top_left "40.8,-74.1" 34 | # bottom_right "40.4,-73.9" 35 | # end 36 | # end 37 | # end 38 | # end 39 | # 40 | # aggregation :new_york do 41 | # geohash_grid field: 'location' 42 | # end 43 | # 44 | # aggregation :map_zoom do 45 | # geo_bounds field: 'location' 46 | # end 47 | # end 48 | # 49 | # @see https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-bounds-agg.html 50 | # 51 | class GeoBounds 52 | include BaseComponent 53 | 54 | option_method :field 55 | option_method :wrap_longitude 56 | end 57 | 58 | end 59 | end 60 | end 61 | end 62 | --------------------------------------------------------------------------------