module LibraryApplicationElasticsearchSearchable
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
index_name "#{Rails.configuration.gex_config[:elasticsearch_prefix]}library_application"
settings index: { number_of_shards: 1 } do
mappings dynamic: 'true' do
indexes :id, :index => :not_analyzed, :type => 'integer'
indexes :name, :analyzer => 'standard', :boost => 100
indexes :title, :analyzer => 'standard', :boost => 80
indexes :image_url, :analyzer => 'standard', :boost => 50
indexes :git_repo, :analyzer => 'standard', :boost => 60
indexes :description, :analyzer => 'standard', :boost => 70
end
end
def self.search(filter)
#
q = Gexcore::ElasticSearchHelpers.sanitize_string(filter.v('q')) || ""
#
__elasticsearch__.search(
{
min_score: 0.5,
query: {
filtered: {
query:{
query_string: {
query: "*" + q + '*',
fields: ['name', 'title', 'image_url', 'git_repo', 'description']
}
},
filter: {
bool: {
must: get_terms(filter),
}
}
}
},
highlight: lib_highlight(filter),
#size: 5,
sort: get_order(filter)
}
)
end
def self.get_terms(filter)
a = []
# name
name = filter.v(:name)
a << {match_phrase_prefix: {name:name}} if name.present?
# title
title = filter.v(:title)
a << {match_phrase_prefix: {title: title}} if title.present?
a
end
def self.get_order(filter)
h = filter.order.to_h
# score
return [ '_score' ] if h['score'].present?
# basic
res = h.map {|colname, dir| {colname => {:order => dir}}}
res
end
def self.lib_highlight(filter)
if filter.v('q').present?
{
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: {
name: {},
title: {},
image_url: {fragment_size: 80, number_of_fragments: 3},
git_repo: {fragment_size: 80, number_of_fragments: 3},
description: {fragment_size: 80, number_of_fragments: 3}
}
}
end
end
end
end
http://stackoverflow.com/questions/30917043/elasticsearch-searching-with-hyphens
...
def self.search(query)
__elasticsearch__.search(
{
query: {
filtered: {
query:{
query_string: {
query: query+"*",
analyze_wildcard:true,
fields: ["username", 'firstname', 'lastname', 'about']
}
},
filter: {
bool: {
should: [
{
term: {status: "1"}
},
]
}
}
}
}
}
)
end