Skip to content

Commit

Permalink
fix(query): remove deprecated _all for fields term
Browse files Browse the repository at this point in the history
  • Loading branch information
caspiano committed Jan 23, 2020
1 parent 720c0a6 commit 2e76478
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: neuroplastic
version: 1.3.2
version: 1.3.3

authors:
- Caspian Baska <[email protected]>
Expand Down
16 changes: 8 additions & 8 deletions spec/query_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ describe Neuroplastic::Query do
teeth = [1, 3, 5, 7, 11]

query.should({"teeth" => teeth})
filter_field = query.build[:filter]
filter_field = query.build[:filter].not_nil!

expected = teeth.map { |t| ({:term => {"teeth" => t}}) }
filter_field.dig(:bool, :should).should eq expected
filter_field.dig(:filter, :bool, :should).should eq expected
end

it "#must" do
query = Goat.elastic.query({"q" => "stands on mountain"})
query.must({"name" => ["billy"]})
filter_field = query.build[:filter]
filter_field = query.build[:filter].not_nil!

filter_field.dig(:bool, :must).should eq [{:term => {"name" => "billy"}}]
filter_field.dig(:filter, :bool, :must).should eq [{:term => {"name" => "billy"}}]
end

it "#must_not" do
query = Goat.elastic.query({"q" => "makes good cheese"})
query.must_not({"name" => ["gruff"]})
filter_field = query.build[:filter]
filter_field = query.build[:filter].not_nil!

filter_field.dig(:bool, :must_not).should eq [{:term => {"name" => "gruff"}}]
filter_field.dig(:filter, :bool, :must_not).should eq [{:term => {"name" => "gruff"}}]
end

it "#range" do
Expand All @@ -63,8 +63,8 @@ describe Neuroplastic::Query do
:lte => 5,
},
})
filter_field = query.build[:filter]
filter_field.dig(:bool, :filter).should eq [{range: {"teeth" => {:lte => 5}}}]
filter_field = query.build[:filter].not_nil!
filter_field.dig(:filter, :bool, :filter).should eq [{range: {"teeth" => {:lte => 5}}}]
end
end
end
4 changes: 2 additions & 2 deletions src/neuroplastic/elastic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ class Neuroplastic::Elastic(T)
filter = opt[:filter]
sort = (opt[:sort]? || [] of Array(String)) + SCORE

# Only merge in filter if it contains fields
query_context = {
bool: filter ? query.merge({filter: filter}) : query,
# Only merge in filter if it contains fields
bool: filter ? query.merge(filter) : query,
}

{
Expand Down
11 changes: 6 additions & 5 deletions src/neuroplastic/query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ module Neuroplastic
alias FilterValue = Array(Int32) | Array(Float32) | Array(Bool) | Array(String) | Nil
alias Filter = Hash(String, FilterValue)

property :offset, :limit, :sort, :fields, :query_settings
property :offset, :limit, :sort, :fields, :query_settings, :search

@query_settings : Hash(String, String)?
@sort = [] of Sort

def initialize(params : HTTP::Params | Hash(String, String) | Hash(Symbol, String) = {} of String => String)
params = params.transform_keys(&.to_s) if params.is_a?(Hash(Symbol, String))
@fields = ["_all"]
@fields = [] of String
@filters = Filter.new

@search = "#{params["q"]?}*"
query = params["q"]? || "*"
@search = query.ends_with?('*') ? query : "#{query}*"

@limit = params["limit"]?.try(&.to_i) || 20
@limit = 500 if @limit > 500
Expand Down Expand Up @@ -145,7 +146,7 @@ module Neuroplastic
# Generates a bool field in the query context
protected def build_query
# Query all documents if no search term
return {must: {match_all: {} of Symbol => String}} unless @search.size > 1
return {must: {match_all: {} of Nil => Nil}} unless @search.size > 1

base_query = {
:simple_query_string => {
Expand Down Expand Up @@ -225,7 +226,7 @@ module Neuroplastic
:should => should,
}.compact

{bool: bool}
{filter: {bool: bool}} unless bool.empty?
end

# Generate filter field
Expand Down

0 comments on commit 2e76478

Please sign in to comment.