Skip to content

Commit 454e287

Browse files
Aaron Gundersonkarmi
authored andcommitted
[DSL] Added the match_phrase and match_phrase_prefix queries
Elasticsearch 5.x now has match_phrase and match_phrase_prefix queries that are not currently available in the DSL. Closes #441 Closes #448
1 parent 32c0173 commit 454e287

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

lib/elasticsearch/dsl/search/queries/match.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Match
3535
option_method :lenient
3636
option_method :zero_terms_query
3737
option_method :cutoff_frequency
38+
option_method :max_expansions
3839
end
3940

4041
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Elasticsearch
2+
module DSL
3+
module Search
4+
module Queries
5+
6+
# A query that analyzes the text and creates a phrase query out of the analyzed text
7+
#
8+
# @example
9+
#
10+
# search do
11+
# query do
12+
# match_phrase :content do
13+
# query 'example content'
14+
# analyzer 'standard'
15+
# end
16+
# end
17+
# end
18+
#
19+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
20+
#
21+
class MatchPhrase
22+
include BaseComponent
23+
24+
option_method :query
25+
option_method :analyzer
26+
option_method :boost
27+
option_method :slop
28+
end
29+
30+
end
31+
end
32+
end
33+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Elasticsearch
2+
module DSL
3+
module Search
4+
module Queries
5+
6+
# The same as match_phrase, except that it allows for prefix matches on the last term in the text
7+
#
8+
# @example
9+
#
10+
# search do
11+
# query do
12+
# match_phrase_prefix :content do
13+
# query 'example content'
14+
# max_expansions 10
15+
# end
16+
# end
17+
# end
18+
#
19+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html
20+
#
21+
class MatchPhrasePrefix
22+
include BaseComponent
23+
24+
option_method :query
25+
option_method :boost
26+
option_method :max_expansions
27+
end
28+
29+
end
30+
end
31+
end
32+
end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'test_helper'
2+
3+
module Elasticsearch
4+
module Test
5+
module Queries
6+
class MatchPhrasePrefixTest < ::Test::Unit::TestCase
7+
include Elasticsearch::DSL::Search::Queries
8+
9+
context "Match Phrase Prefix Query" do
10+
subject { MatchPhrasePrefix.new }
11+
12+
should "be converted to a Hash" do
13+
assert_equal({ match_phrase_prefix: {} }, subject.to_hash)
14+
end
15+
16+
should "take a concrete value" do
17+
subject = MatchPhrasePrefix.new message: 'test'
18+
19+
assert_equal({match_phrase_prefix: {message: "test"}}, subject.to_hash)
20+
end
21+
22+
should "have option methods" do
23+
subject = MatchPhrasePrefix.new
24+
25+
subject.query 'bar'
26+
subject.boost 10
27+
subject.max_expansions 1
28+
29+
assert_equal %w[ boost max_expansions query ],
30+
subject.to_hash[:match_phrase_prefix].keys.map(&:to_s).sort
31+
assert_equal 'bar', subject.to_hash[:match_phrase_prefix][:query]
32+
end
33+
34+
should "take a Hash" do
35+
subject = MatchPhrasePrefix.new message: { query: 'test' }
36+
37+
assert_equal({match_phrase_prefix: {message: {query: "test" }}}, subject.to_hash)
38+
end
39+
40+
should "take a block" do
41+
subject = MatchPhrasePrefix.new :message do
42+
query 'test'
43+
boost 2
44+
max_expansions 1
45+
end
46+
47+
assert_equal({match_phrase_prefix: {message: {query: "test", max_expansions: 1, boost: 2 }}},
48+
subject.to_hash)
49+
end
50+
51+
should "take a method call" do
52+
subject = MatchPhrasePrefix.new :message
53+
subject.query 'test'
54+
55+
assert_equal({match_phrase_prefix: {message: {query: "test" }}}, subject.to_hash)
56+
end
57+
58+
end
59+
end
60+
end
61+
end
62+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'test_helper'
2+
3+
module Elasticsearch
4+
module Test
5+
module Queries
6+
class MatchPhraseTest < ::Test::Unit::TestCase
7+
include Elasticsearch::DSL::Search::Queries
8+
9+
context "Match Phrase Query" do
10+
subject { MatchPhrase.new }
11+
12+
should "be converted to a Hash" do
13+
assert_equal({ match_phrase: {} }, subject.to_hash)
14+
end
15+
16+
should "take a concrete value" do
17+
subject = MatchPhrase.new message: 'test'
18+
19+
assert_equal({match_phrase: {message: "test"}}, subject.to_hash)
20+
end
21+
22+
should "have option methods" do
23+
subject = MatchPhrase.new
24+
25+
subject.query 'bar'
26+
subject.analyzer 'standard'
27+
subject.boost 10
28+
subject.slop 1
29+
30+
assert_equal %w[ analyzer boost query slop ],
31+
subject.to_hash[:match_phrase].keys.map(&:to_s).sort
32+
assert_equal 'bar', subject.to_hash[:match_phrase][:query]
33+
end
34+
35+
should "take a Hash" do
36+
subject = MatchPhrase.new message: { query: 'test' }
37+
38+
assert_equal({match_phrase: {message: {query: "test" }}}, subject.to_hash)
39+
end
40+
41+
should "take a block" do
42+
subject = MatchPhrase.new :message do
43+
query 'test'
44+
slop 1
45+
boost 2
46+
end
47+
48+
assert_equal({match_phrase: {message: {query: "test", slop: 1, boost: 2 }}},
49+
subject.to_hash)
50+
end
51+
52+
should "take a method call" do
53+
subject = MatchPhrase.new :message
54+
subject.query 'test'
55+
56+
assert_equal({match_phrase: {message: {query: "test" }}}, subject.to_hash)
57+
end
58+
59+
end
60+
end
61+
end
62+
end
63+
end

0 commit comments

Comments
 (0)