Skip to content

Commit

Permalink
improve local es setup
Browse files Browse the repository at this point in the history
  • Loading branch information
croyfish committed Nov 25, 2024
1 parent e1b5d84 commit 17fecc7
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 5 deletions.
39 changes: 34 additions & 5 deletions app/models/elastic_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def es_host(config)
# Create elastic search document by id and body. Eg: id: "1234567", body: {id: "1234567", title: "test"}
def create_document(id, body)
response = @client.create index: @index, type: @type, id: id, body: body
LogWrapper.log('DEBUG', {'message' => "ES document successfully created. Id: #{id}",
LogWrapper.log('DEBUG', {'message' => "ES document successfully created. Id: #{id}",
'method' => 'create_document'})
response
end

# Delete elastic search document by id. Eg: id: "1234567"
def delete_document_by_id(id)
response = @client.delete index: @index, type: @type, id: id
LogWrapper.log('DEBUG', {'message' => "ES document successfully deleted. Id: #{id}",
LogWrapper.log('DEBUG', {'message' => "ES document successfully deleted. Id: #{id}",
'method' => 'delete_document_by_id'})

response
Expand Down Expand Up @@ -169,7 +169,7 @@ def group_by_facets_query(aggregation_hash)
# aggregation_hash["availability"] = { "terms": { "field": "availability.raw", :size => 10, :order => {:_key => "asc"} } }
aggregation_hash["area of study"] = { terms: { field: "area_of_study", :size => 100, :order => {:_key => "asc"} } }

aggregation_hash["subjects"] = {:nested => {:path => "subjects"},
aggregation_hash["subjects"] = {:nested => {:path => "subjects"},
:aggregations => {:subjects => {:composite => {:size => 3000, :sources => [{:id => {:terms => {:field => "subjects.id"}}},
{:title => {:terms => {:field => "subjects.title.keyword"}}}]}}}}
aggregation_hash
Expand Down Expand Up @@ -292,10 +292,10 @@ def teacher_sets_sort_order(sort_order = 0)
end
query
end

# Search elastic documents based on the query.Eg: body: {id: "1234567", title: "test"}
def search_by_query(body)
LogWrapper.log('INFO', {'message' => "Elastic search query: #{body}",
LogWrapper.log('INFO', {'message' => "Elastic search query: #{body}",
'method' => 'search_by_query'})
results = {}
resp = @client.search(index: @index, body: body)
Expand Down Expand Up @@ -332,4 +332,33 @@ def update_document_by_id(id, query)
def delete_by_query(query)
@client.delete_by_query(index: @index, body: query)
end

def create_or_update_index(index_name, body)
begin
if @client.indices.exists?(index: index_name)
puts "Index #{index_name} already exists. Updating..."
@client.indices.put_mapping(index: index_name, body: body[:mappings])
else
puts "Creating index #{index_name}..."
@client.indices.create(index: index_name, body: body)
end
puts "Index #{index_name} created/updated successfully."
rescue StandardError => e
puts "Error: #{e.message}"
end
end

def delete_index(index_name)
begin
if @client.indices.exists?(index: index_name)
puts "Deleting index #{index_name}..."
@client.indices.delete(index: index_name)
puts "Index #{index_name} deleted successfully."
else
puts "Index #{index_name} does not exist."
end
rescue StandardError => e
puts "Error: #{e.message}"
end
end
end
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ services:
bash -c "
RAILS_ENV=test bundle exec rake db:set_up_for_local;
RAILS_ENV=development bundle exec rake db:set_up_for_local;
RAILS_ENV=development bundle exec rake elasticsearch:delete_teacherset_index;
RAILS_ENV=development bundle exec rake elasticsearch:create_teacherset_index;
RAILS_ENV=development bundle exec rake seeds:teacher_sets;
bundle exec rails server -b 0.0.0.0
"
Expand Down
138 changes: 138 additions & 0 deletions lib/tasks/elasticsearch.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
namespace :elasticsearch do
desc 'Create Elasticsearch mappings and index for teachersets'
task create_teacherset_index: :environment do
require 'elasticsearch'

body = {
settings: {
analysis: {
analyzer: {
ts_analyzer: {
tokenizer: 'standard',
filter: %w[lowercase stop asciifolding]
}
}
}
},
mappings: {
properties: {
title: {
type: 'text',
fields: {
keyword: {
type: 'keyword'
}
},
analyzer: 'default'
},
description: {
type: 'text',
fields: {
keyword: {
type: 'keyword'
}
},
analyzer: 'default'
},
contents: {
type: 'text',
fields: {
keyword: {
type: 'keyword'
}
},
analyzer: 'default'
},
grade_begin: { type: 'long' },
grade_end: { type: 'long' },
id: { type: 'long' },
details_url: { type: 'text' },
availability: {
type: 'text',
fields: {
raw: { type: 'keyword' }
}
},
total_copies: { type: 'long' },
call_number: {
type: 'text',
fields: {
keyword: { type: 'keyword' }
}
},
language: {
type: 'text',
fields: {
keyword: { type: 'keyword' }
}
},
physical_description: {
type: 'text',
fields: {
keyword: { type: 'keyword' }
}
},
primary_language: {
type: 'text',
fielddata: true
},
available_copies: { type: 'long' },
bnumber: {
type: 'text',
fields: {
keyword: { type: 'keyword' }
}
},
set_type: {
type: 'text',
fielddata: true
},
area_of_study: {
type: 'text',
fielddata: true
},
created_at: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis'
},
updated_at: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis'
},
subjects: {
type: 'nested',
properties: {
id: { type: 'long' },
title: {
type: 'text',
fields: {
keyword: { type: 'keyword' }
}
},
created_at: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis'
},
updated_at: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis'
}
}
}
}
}
}

index_name = 'teacherset'
ElasticSearch.new.create_or_update_index(index_name, body)
end

desc 'Create Elasticsearch mappings and index for teacherset subjects'
task delete_teacherset_index: :environment do
require 'elasticsearch'

index_name = 'teacherset'
ElasticSearch.new.delete_index(index_name)
end
end

0 comments on commit 17fecc7

Please sign in to comment.