-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from theablefew/feature/match
Fix Issue #102: Add `.match` and support for multi match
- Loading branch information
Showing
4 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ def registry | |
:or_filter, | ||
:extending, | ||
:skip_callbacks, | ||
:match, | ||
:neural_sparse, | ||
:neural, | ||
:hybrid, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Stretchy | ||
module Relations | ||
module QueryMethods | ||
module Match | ||
|
||
# This method is used to add conditions to the query. | ||
# | ||
# ### Parameters | ||
# | ||
# - `query:` (Required) - Text, number, boolean value or date you wish to find in the provided field. The match query analyzes any provided text before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term. | ||
# - `options:` (Optional) - A hash of options to customize the match query. Options include: | ||
# - `analyzer:` (string) - Analyzer used to convert the text in the query value into tokens. Defaults to the index-time analyzer mapped for the field. If no analyzer is mapped, the index’s default analyzer is used. | ||
# - `auto_generate_synonyms_phrase_query:` (Boolean) - If true, match phrase queries are automatically created for multi-term synonyms. Defaults to true. | ||
# - `boost:` (float) - Floating point number used to decrease or increase the relevance scores of the query. Defaults to 1.0. | ||
# - `fuzziness:` (string) - Maximum edit distance allowed for matching. | ||
# - `max_expansions:` (integer) - Maximum number of terms to which the query will expand. Defaults to 50. | ||
# - `prefix_length:` (integer) - Number of beginning characters left unchanged for fuzzy matching. Defaults to 0. | ||
# - `fuzzy_transpositions:` (Boolean) - If true, edits for fuzzy matching include transpositions of two adjacent characters (ab → ba). Defaults to true. | ||
# - `fuzzy_rewrite:` (string) - Method used to rewrite the query. | ||
# - `lenient:` (Boolean) - If true, format-based errors, such as providing a text query value for a numeric field, are ignored. Defaults to false. | ||
# - `operator:` (string) - Boolean logic used to interpret text in the query value. Valid values are: OR (Default), AND. | ||
# - `minimum_should_match:` (string) - Minimum number of clauses that must match for a document to be returned. | ||
# - `zero_terms_query:` (string) - Indicates whether no documents are returned if the analyzer removes all tokens, such as when using a stop filter. Valid values are: none (Default), all. | ||
# | ||
# ### Returns | ||
# | ||
# Returns a Stretchy::Relation with the specified conditions applied. | ||
# | ||
# ### Examples | ||
# | ||
# ```ruby | ||
# Model.match(path: "/new/things") | ||
# ``` | ||
# | ||
def match(opts = :chain, *rest) | ||
return MatchChain.new(spawn) if opts == :chain | ||
return self if opts.blank? | ||
|
||
spawn.match!(opts, *rest) | ||
end | ||
|
||
def match!(opts, *rest) # :nodoc: | ||
self.match_values = [Hash[*opts.shift], **opts] | ||
self | ||
end | ||
|
||
QueryMethods.register!(:match) | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'spec_helper' | ||
require 'models/test_model' | ||
|
||
describe Stretchy::Relations::QueryMethods::Match do | ||
|
||
context 'api' do | ||
let(:model) {TestModel} | ||
let(:relation) { Stretchy::Relation.new(model, {}) } | ||
let(:value_key) { described_class.name.demodulize.underscore.to_sym } | ||
let(:relation_values) { relation.values[value_key] } | ||
|
||
it 'registers' do | ||
expect(Stretchy::Relations::QueryMethods.registry).to include(:match) | ||
end | ||
|
||
context 'serializes values' do | ||
it 'without options' do | ||
relation.match(path: 'attributes/types') | ||
expect(relation_values).to eq([{path: 'attributes/types'}]) | ||
end | ||
|
||
it 'with options' do | ||
relation.match(path: 'attributes/types', fuzziness: 3, operator: "AND") | ||
expect(relation_values).to eq([{path: 'attributes/types'}, {fuzziness: 3, operator: "AND"}]) | ||
end | ||
end | ||
|
||
end | ||
|
||
describe Stretchy::Relations::QueryBuilder do | ||
let(:attribute_types) { double('model', attribute_types: { "path": Stretchy::Attributes::Type::Text.new })} | ||
let(:values) { {} } | ||
let(:clause) { subject.to_elastic.deep_symbolize_keys.dig(:query, :match) } | ||
|
||
before do | ||
allow(attribute_types).to receive(:[]) | ||
end | ||
|
||
context 'when built' do | ||
subject { described_class.new(values, attribute_types) } | ||
|
||
context 'without a match query' do | ||
it 'match is not present' do | ||
values[:where] = [{first_name: 'Irving' }] | ||
expect(clause).to be_falsey | ||
end | ||
end | ||
|
||
context 'with a match query' do | ||
it 'match is present' do | ||
values[:match] = [{ path: 'attributes/types'}, {fuzziness: 3, operator: "AND"}] | ||
expect(clause).to eq({ path: { query: 'attributes/types', fuzziness: 3, operator: "AND" } }) | ||
end | ||
|
||
end | ||
end | ||
|
||
end | ||
|
||
end |