Skip to content

Commit

Permalink
Merge 6f04176 into 349e08c
Browse files Browse the repository at this point in the history
  • Loading branch information
izakp committed May 21, 2019
2 parents 349e08c + 6f04176 commit 8479b1a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 37 deletions.
26 changes: 16 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-01-27 12:52:56 -0500 using RuboCop version 0.47.1.
# on 2019-05-20 16:29:39 -0400 using RuboCop version 0.60.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Offense count: 3
Metrics/AbcSize:
Max: 18

# Offense count: 3
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 134

# Offense count: 30
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 135

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 103

# Offense count: 1
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 13
Max: 12

# Offense count: 3
# Cop supports --auto-correct.
Expand Down Expand Up @@ -65,8 +65,14 @@ Style/MutableConstant:

# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: only_raise, only_fail, semantic
Style/SignalException:
Exclude:
- 'lib/estella/parser.rb'

# Offense count: 30
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 135
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ before_script:
- bundle exec danger

env:
- ELASTICSEARCH=https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.4.4/elasticsearch-2.4.4.deb
- ELASTICSEARCH=https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.1.1/elasticsearch-2.1.1.deb
- ELASTICSEARCH=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.6.deb
- ELASTICSEARCH=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.5.2.deb
- ELASTICSEARCH=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.16.deb

before_install:
- gem update --system
- gem update bundler
- curl -O $ELASTICSEARCH && sudo dpkg -i --force-confnew elasticsearch-*.deb
- "echo 'script.inline: on' | sudo tee -a /etc/elasticsearch/elasticsearch.yml"
- sudo /etc/init.d/elasticsearch start
- until curl --silent -XGET --fail http://localhost:9200; do printf '.'; sleep 1; done

Expand Down
2 changes: 1 addition & 1 deletion estella.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |gem|

gem.add_runtime_dependency 'activemodel'
gem.add_runtime_dependency 'activesupport'
gem.add_runtime_dependency 'elasticsearch-model', '~> 2.0'
gem.add_runtime_dependency 'elasticsearch-model', '~> 5.0'

gem.add_development_dependency 'activerecord'
gem.add_development_dependency 'rake', '~> 11.0'
Expand Down
8 changes: 4 additions & 4 deletions lib/estella/analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ module Analysis
}

DEFAULT_FIELDS = {
default: { type: 'string', analyzer: 'default_analyzer' },
snowball: { type: 'string', analyzer: 'snowball_analyzer' },
shingle: { type: 'string', analyzer: 'shingle_analyzer' },
ngram: { type: 'string', analyzer: 'ngram_analyzer', search_analyzer: 'search_analyzer' }
default: { type: 'text', analyzer: 'default_analyzer' },
snowball: { type: 'text', analyzer: 'snowball_analyzer' },
shingle: { type: 'text', analyzer: 'shingle_analyzer' },
ngram: { type: 'text', analyzer: 'ngram_analyzer', search_analyzer: 'search_analyzer' }
}

DEFAULT_FIELD_FACTORS = {
Expand Down
37 changes: 23 additions & 14 deletions lib/estella/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ def initialize(params)
@params = params
@query = {
_source: false,
query: {},
filter: {
bool: { must: [], must_not: [] }
},
query: { bool: { must: [], must_not: [], filter: [] } },
aggregations: {}
}
add_query
Expand All @@ -21,14 +18,22 @@ def initialize(params)
end

def must(filter)
query[:filter][:bool][:must] << filter
if query[:query][:function_score]
query[:query][:function_score][:query][:bool][:filter] << filter
else
query[:query][:bool][:filter] << filter
end
end

def exclude(filter)
query[:filter][:bool][:must_not] << filter
if query[:query][:function_score]
query[:query][:function_score][:query][:bool][:must_not] << filter
else
query[:query][:bool][:must_not] << filter
end
end

def query_definition
def term_query_definition
{
multi_match: {
type: 'most_fields',
Expand All @@ -50,18 +55,22 @@ def add_pagination
end

def add_query
if params[:term] && params[:indexed_fields]
add_term_query
else
query[:query] = { match_all: {} }
end
return unless params[:term] && params[:indexed_fields]

add_term_query
end

# fulltext search across all string fields
def add_term_query
query[:query] = {
function_score: {
query: query_definition
query: {
bool: {
must: term_query_definition,
filter: [],
must_not: []
}
}
}
}

Expand All @@ -88,7 +97,7 @@ def add_field_boost
# boost them by factor if provided
def term_search_fields
params[:indexed_fields]
.select { |_, opts| opts[:type].to_s == 'string' }
.select { |_, opts| opts[:type].to_s == 'text' }
.reject { |_, opts| opts[:analysis].nil? }
.map do |field, opts|
opts[:analysis].map do |analyzer|
Expand Down
2 changes: 1 addition & 1 deletion lib/estella/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module ClassMethods
# indexes slug attribute by default
def index_slug
if defined? slug
indexed_fields.merge!(slug: { type: :string, index: :not_analyzed })
indexed_fields.merge!(slug: { type: :text, index: :not_analyzed })
indexed_json.merge!(slug: :slug)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/searchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def self.slug
end

searchable do
field :title, type: :string, analysis: Estella::Analysis::FULLTEXT_ANALYSIS, factor: 1.0
field :keywords, type: :string, analysis: %i[default snowball], factor: 0.5
field :title, type: :text, analysis: Estella::Analysis::FULLTEXT_ANALYSIS, factor: 1.0
field :keywords, type: :text, analysis: %i[default snowball], factor: 0.5
field :follows_count, type: :integer
field :published, type: :boolean, filter: true

Expand Down

0 comments on commit 8479b1a

Please sign in to comment.