Skip to content

Instantly share code, notes, and snippets.

@outsmartin
Created July 30, 2013 15:51
Show Gist options
  • Save outsmartin/6114186 to your computer and use it in GitHub Desktop.
Save outsmartin/6114186 to your computer and use it in GitHub Desktop.
class JobSearch
class << self
def create_index
Tire.index self.to_s.downcase do
delete
create mappings: {
job: {
properties: {
id: { type: 'string', index: 'not_analyzed', include_in_all: false},
title: { type: 'string',analyzer: 'stemming', boost: 3.0}, # TODO Titel with synonyms
description: {
type: 'multi_field',
fields: {
with_synonyms: {type:'string',analyzer: 'syn', boost: 0.5},
without_synonyms: {type:'string',analyzer: 'stemming', boost: 1.0},
}
},
tags: { type: 'string', boost: 2.0},
location: {type: 'geo_point'},
}
}
},settings:{
number_of_shards: 1,
number_of_replicas: 0,
similarity: {
default: {
type: "org.elasticsearch.index.similarity.CustomSimilarityProvider"
}
},
analysis: {
analyzer: {
stemming: {
type: 'snowball',
language: 'German',
filter: %w[standard lowercase]
},
syn: {
tokenizer: "whitespace",
filter: %w[standard lowercase synonym]
}
},
filter: {
german_stemmer: {
type: 'stemmer',
name: 'german2'
},
synonym: {
type: "synonym",
expand: 0, # =1 -> Ueberall wo Einkauf oder Logistik steht, steht dann "Einkauf Logistik"
synonyms: [
"einkauf, logistik", # alle Werte werden wie der erste behandelt
]
}
}
}
}
refresh
end
end
def add jobs
Tire.index self.to_s.downcase do
jobs = jobs.map do |job|
job.merge({type: 'job'})
end
import jobs
refresh
end
end
def search params
Tire.search self.to_s.downcase do
query do
dis_max do
query { string params, fields: ['title', 'description.with_synonyms', 'tags'], use_dis_max: false, default_operator: "AND" }
query { string params, fields: ['title', 'description.without_synonyms', 'tags'], use_dis_max: false, default_operator: "AND"}
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment