Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Elasticsearch with elasticsearch-rails #2

Open
wants to merge 1 commit into
base: before-elastic
Choose a base branch
from

Conversation

dbackeus
Copy link
Contributor

@dbackeus dbackeus commented May 2, 2023

Why

The goal of the rails-example application is to serve as an example web application for learning how to deploy apps to the Reclaim the Stack platform. Currently it uses Postgres and Redis but since the platform also supports Elasticsearch we want the demo application to use this as well.

What

Indexes the Post and Link models in Elasticsearch to allow for doing full text search across both models. Each document in the index will use a title field with a high priority and a content field with lower priority (taken from Post#body and Link#description respectively).

Feel free to compare with the 0 dependency version at #1.

Lines of Code

Implementation: ~160 LOC
Dependencies: 16,698 LOC

Time spent

Didn't keep track but approximately 2 hours (would have been closer to 1 hour if it wasn't for the issues).

Issues

Had to monkey patch Elasticsearch::Client#verify_with_version_or_header because elastic/elasticsearch-ruby#1429 (comment) (to allow compatibility with the fully open source licensed OpenSearch fork of Elasticsearch)

Also had to enable eager_load for all environments to accurately keep track of which models have included the Searchable module. Fun fact: I tried using an explicit manually managed Searchable::MODELS constant to work around this but this caused some terrible issue where the included do block would only run for one of the searchable models 🤦

Learnings

I copy pasted some things out of the elasticsearch-rails and elasticsearch-model README files to get started. I still know nothing of what is going on with Elasticsearch under the hood.

Dependencies

Explicit dependencies [3127 LOC]

elasticsearch-model-7.2.1

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                   28         2995         1156         1497          342
===============================================================================
 Total                  28         2995         1156         1497          342
===============================================================================

elasticsearch-rails-7.2.1

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 CSS                     1           76           65            0           11
 JSON                    1            1            1            0            0
 Ruby                   20         2675         1593          571          511
 Ruby HTML               2          356          312            0           44
===============================================================================
 Total                  24         3108         1971          571          566
===============================================================================

Secondary dependencies [83 LOC]

elasticsearch-7.17.7

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    3          154           83           51           20
===============================================================================
 Total                   3          154           83           51           20
===============================================================================

Tertiary dependencies [10816 LOC]

elasticsearch-api-7.17.7

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                  188        12459         5184         6063         1212
===============================================================================
 Total                 188        12459         5184         6063         1212
===============================================================================

elasticsearch-transport-7.17.7

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                   18         2293         1226          830          237
===============================================================================
 Total                  18         2293         1226          830          237
===============================================================================

hashie-5.0.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                   41         3286         2087          826          373
===============================================================================
 Total                  41         3286         2087          826          373
===============================================================================

faraday-1.10.3

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                   39         3956         2322         1084          550
===============================================================================
 Total                  39         3956         2322         1084          550
===============================================================================

Quaternary dependencies [2432 LOC]

multi_json

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                   18         1206          935          128          143
===============================================================================
 Total                  18         1206          935          128          143
===============================================================================

faraday-em_http-1.0.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    4          369          289           30           50
===============================================================================
 Total                   4          369          289           30           50
===============================================================================

faraday-em_synchrony-1.0.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    4          241          180           22           39
===============================================================================
 Total                   4          241          180           22           39
===============================================================================

faraday-excon-1.1.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    3          142          106            8           28
===============================================================================
 Total                   3          142          106            8           28
===============================================================================

faraday-httpclient-1.0.1

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    3          172          116           24           32
===============================================================================
 Total                   3          172          116           24           32
===============================================================================

faraday-multipart-1.0.4

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    5          329          182          110           37
===============================================================================
 Total                   5          329          182          110           37
===============================================================================

faraday-net_http-1.0.1

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    3          230          186           12           32
===============================================================================
 Total                   3          230          186           12           32
===============================================================================

faraday-net_http_persistent-1.2.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    3          134          103           13           18
===============================================================================
 Total                   3          134          103           13           18
===============================================================================

faraday-patron-1.0.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    3          150          115           10           25
===============================================================================
 Total                   3          150          115           10           25
===============================================================================

faraday-rack-1.0.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    3           93           61           18           14
===============================================================================
 Total                   3           93           61           18           14
===============================================================================

faraday-retry-1.0.3

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                    4          257          159           63           35
===============================================================================
 Total                   4          257          159           63           35
===============================================================================

Quinary dependencies [240 LOC]

multipart-post-2.3.0

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Ruby                   11          480          240          169           71
===============================================================================
 Total                  11          480          240          169           71
===============================================================================

@dbackeus dbackeus changed the base branch from master to before-elastic May 2, 2023 14:58
@dbackeus dbackeus changed the title Elasticsearch indexing with elasticsearch-(model|rails) Elasticsearch indexing with elasticsearch-rails May 2, 2023
@dbackeus dbackeus force-pushed the elasticsearch-rails branch 2 times, most recently from f91dc9e to 306dce1 Compare May 2, 2023 19:13
@dbackeus dbackeus changed the title Elasticsearch indexing with elasticsearch-rails Add Elasticsearch with elasticsearch-rails May 3, 2023
@@ -0,0 +1,25 @@
class IndexerJob < ApplicationJob
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: The implementation of this Job was based on the implementation suggested in the elasticsearch-model gem: https://github.com/elastic/elasticsearch-rails/tree/main/elasticsearch-model#asynchronous-callbacks

I would not have written this with an operation argument and case statement if I did it from scratch.

@dbackeus dbackeus force-pushed the elasticsearch-rails branch from 306dce1 to 331e45a Compare May 3, 2023 09:51
@dbackeus dbackeus force-pushed the elasticsearch-rails branch from 331e45a to 497fa74 Compare May 3, 2023 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant