-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from bibendi/feature/chewy-connection
Elasticsearch Chewy connection
- Loading branch information
Showing
16 changed files
with
374 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
/tmp/ | ||
.pry_history | ||
.rspec_status | ||
*_history | ||
Gemfile.lock | ||
.lefthook-local/ | ||
lefthook-local.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
IRB.conf[:SAVE_HISTORY] = 1000 | ||
IRB.conf[:HISTORY_FILE] = "#{__dir__}/.irb_history" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Connections | ||
class Chewy < ::GraphQL::Pagination::RelationConnection | ||
private | ||
|
||
def load_nodes | ||
@nodes ||= limited_nodes.objects | ||
end | ||
|
||
def relation_count(relation) | ||
offset = relation_offset(relation) || 0 | ||
limit = relation_limit(relation) | ||
count = relation.count - offset | ||
|
||
if limit.nil? | ||
count | ||
else | ||
[count, limit].min | ||
end | ||
end | ||
|
||
def relation_limit(relation) | ||
relation.send(:raw_limit_value)&.to_i | ||
end | ||
|
||
def relation_offset(relation) | ||
relation.send(:raw_offset_value)&.to_i | ||
end | ||
|
||
def null_relation(relation) | ||
relation.none | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :message, class: "Message" do | ||
sequence(:username) { |n| Faker::Internet.unique.username(specifier: 3..32, separators: %w[.]) } | ||
body { Faker::Movies::LordOfTheRings.quote } | ||
|
||
trait :deleted do | ||
deleted { true } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
class MessagesIndex < Chewy::Index | ||
settings analysis: { | ||
filter: { | ||
autocomplete_filter: { | ||
type: "edge_ngram", | ||
min_gram: 2, | ||
max_gram: 20 | ||
} | ||
}, | ||
analyzer: { | ||
autocomplete: { | ||
type: "custom", | ||
tokenizer: "standard", | ||
filter: ["lowercase", "autocomplete_filter"] | ||
}, | ||
autocomplete_search: { | ||
type: "custom", | ||
tokenizer: "standard", | ||
filter: ["lowercase"] | ||
} | ||
} | ||
} | ||
|
||
index_scope Message.all | ||
|
||
field :username, :body, analyzer: "autocomplete", search_analyzer: "autocomplete_search" | ||
field :deleted, type: "boolean" | ||
field :updated_at, type: "date" | ||
|
||
class << self | ||
def active_members | ||
filter(term: {deleted: false}) | ||
end | ||
|
||
def search(query) | ||
query( | ||
multi_match: { | ||
query: query, | ||
type: "most_fields", | ||
fields: %w[username^2 body], | ||
fuzziness: 1 | ||
} | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class Message < ActiveRecord::Base | ||
update_index("messages_index") { self } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
test: | ||
host: <%= ENV["ELASTICSEARCH_URL"] %> | ||
prefix: 'test' | ||
index: | ||
number_of_shards: 1 | ||
number_of_replicas: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require "chewy" | ||
|
||
Chewy.settings = { | ||
transport_options: { | ||
ssl: { | ||
verify: false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.