elasticfusion 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 938b72038fd4f89e02fd831abd666c1cde6bb8c6
4
+ data.tar.gz: 9710d296afd5c5723174a9e6cc6d4b1600ecbacb
5
+ SHA512:
6
+ metadata.gz: 02a3d11f81fee37f4f165f729759b75dc001a18189b96b147aad79092d2b41cfde213a6846aa6042dce3ec14ea8f215cc107c34b4e9f78ef089b2e11886df789
7
+ data.tar.gz: 9882c8b937b120c910db52df7542935a5ac3218f973e773c4bf86b89bfe870e968a234586871c6da844cc87b5d2e24e600d133512dda07296e8290a517906c48
@@ -0,0 +1,5 @@
1
+ engines:
2
+ rubocop:
3
+ enabled: true
4
+ config:
5
+ file: .rubocop.yml
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,67 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Lint/EndAlignment:
5
+ EnforcedStyleAlignWith: variable
6
+
7
+ Lint/AmbiguousRegexpLiteral:
8
+ Enabled: false
9
+
10
+ Metrics/AbcSize:
11
+ Max: 20
12
+
13
+ Metrics/BlockLength:
14
+ Max: 30
15
+ Exclude:
16
+ - test/**/*
17
+
18
+ Metrics/ClassLength:
19
+ Max: 200
20
+
21
+ Metrics/LineLength:
22
+ Max: 100
23
+ Exclude:
24
+ - test/**/*
25
+
26
+ Metrics/MethodLength:
27
+ Max: 20
28
+
29
+ # In search tests, parameters are aligned in a special way
30
+ # for the sake of readability.
31
+ Style/AlignParameters:
32
+ Exclude:
33
+ - test/unit/search/**/*
34
+
35
+ Style/AndOr:
36
+ Enabled: false
37
+
38
+ Style/Documentation:
39
+ Enabled: false
40
+
41
+ Style/EmptyMethod:
42
+ EnforcedStyle: expanded
43
+
44
+ # Guard clauses make parsing-related code actually harder to read.
45
+ Style/GuardClause:
46
+ Exclude:
47
+ - lib/elasticfusion/search/**/*
48
+
49
+ Style/NumericPredicate:
50
+ EnforcedStyle: comparison
51
+
52
+ Style/OptionalArguments:
53
+ Exclude:
54
+ - test/**/*
55
+
56
+ Style/Lambda:
57
+ EnforcedStyle: literal
58
+
59
+ Style/MethodName:
60
+ Exclude:
61
+ - lib/elasticfusion/search/visitors/es_visitor.rb
62
+
63
+ Style/MultilineMethodCallBraceLayout:
64
+ EnforcedStyle: same_line
65
+
66
+ Style/RedundantSelf:
67
+ Enabled: false
@@ -0,0 +1,22 @@
1
+ dist: trusty
2
+
3
+ sudo: false
4
+
5
+ rvm: 2.4.1
6
+
7
+ jdk: openjdk8
8
+
9
+ env:
10
+ - ES_VERSION=5.3.1 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
11
+
12
+ install:
13
+ - wget ${ES_DOWNLOAD_URL}
14
+ - tar -xzf elasticsearch-${ES_VERSION}.tar.gz
15
+ - ./elasticsearch-${ES_VERSION}/bin/elasticsearch &
16
+ - bundle install
17
+
18
+ cache:
19
+ - bundler
20
+
21
+ after_success:
22
+ - bundle exec codeclimate-test-reporter
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,116 @@
1
+ CC0 1.0 Universal
2
+
3
+ Statement of Purpose
4
+
5
+ The laws of most jurisdictions throughout the world automatically confer
6
+ exclusive Copyright and Related Rights (defined below) upon the creator and
7
+ subsequent owner(s) (each and all, an "owner") of an original work of
8
+ authorship and/or a database (each, a "Work").
9
+
10
+ Certain owners wish to permanently relinquish those rights to a Work for the
11
+ purpose of contributing to a commons of creative, cultural and scientific
12
+ works ("Commons") that the public can reliably and without fear of later
13
+ claims of infringement build upon, modify, incorporate in other works, reuse
14
+ and redistribute as freely as possible in any form whatsoever and for any
15
+ purposes, including without limitation commercial purposes. These owners may
16
+ contribute to the Commons to promote the ideal of a free culture and the
17
+ further production of creative, cultural and scientific works, or to gain
18
+ reputation or greater distribution for their Work in part through the use and
19
+ efforts of others.
20
+
21
+ For these and/or other purposes and motivations, and without any expectation
22
+ of additional consideration or compensation, the person associating CC0 with a
23
+ Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
24
+ and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
25
+ and publicly distribute the Work under its terms, with knowledge of his or her
26
+ Copyright and Related Rights in the Work and the meaning and intended legal
27
+ effect of CC0 on those rights.
28
+
29
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
30
+ protected by copyright and related or neighboring rights ("Copyright and
31
+ Related Rights"). Copyright and Related Rights include, but are not limited
32
+ to, the following:
33
+
34
+ i. the right to reproduce, adapt, distribute, perform, display, communicate,
35
+ and translate a Work;
36
+
37
+ ii. moral rights retained by the original author(s) and/or performer(s);
38
+
39
+ iii. publicity and privacy rights pertaining to a person's image or likeness
40
+ depicted in a Work;
41
+
42
+ iv. rights protecting against unfair competition in regards to a Work,
43
+ subject to the limitations in paragraph 4(a), below;
44
+
45
+ v. rights protecting the extraction, dissemination, use and reuse of data in
46
+ a Work;
47
+
48
+ vi. database rights (such as those arising under Directive 96/9/EC of the
49
+ European Parliament and of the Council of 11 March 1996 on the legal
50
+ protection of databases, and under any national implementation thereof,
51
+ including any amended or successor version of such directive); and
52
+
53
+ vii. other similar, equivalent or corresponding rights throughout the world
54
+ based on applicable law or treaty, and any national implementations thereof.
55
+
56
+ 2. Waiver. To the greatest extent permitted by, but not in contravention of,
57
+ applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
58
+ unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
59
+ and Related Rights and associated claims and causes of action, whether now
60
+ known or unknown (including existing as well as future claims and causes of
61
+ action), in the Work (i) in all territories worldwide, (ii) for the maximum
62
+ duration provided by applicable law or treaty (including future time
63
+ extensions), (iii) in any current or future medium and for any number of
64
+ copies, and (iv) for any purpose whatsoever, including without limitation
65
+ commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
66
+ the Waiver for the benefit of each member of the public at large and to the
67
+ detriment of Affirmer's heirs and successors, fully intending that such Waiver
68
+ shall not be subject to revocation, rescission, cancellation, termination, or
69
+ any other legal or equitable action to disrupt the quiet enjoyment of the Work
70
+ by the public as contemplated by Affirmer's express Statement of Purpose.
71
+
72
+ 3. Public License Fallback. Should any part of the Waiver for any reason be
73
+ judged legally invalid or ineffective under applicable law, then the Waiver
74
+ shall be preserved to the maximum extent permitted taking into account
75
+ Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
76
+ is so judged Affirmer hereby grants to each affected person a royalty-free,
77
+ non transferable, non sublicensable, non exclusive, irrevocable and
78
+ unconditional license to exercise Affirmer's Copyright and Related Rights in
79
+ the Work (i) in all territories worldwide, (ii) for the maximum duration
80
+ provided by applicable law or treaty (including future time extensions), (iii)
81
+ in any current or future medium and for any number of copies, and (iv) for any
82
+ purpose whatsoever, including without limitation commercial, advertising or
83
+ promotional purposes (the "License"). The License shall be deemed effective as
84
+ of the date CC0 was applied by Affirmer to the Work. Should any part of the
85
+ License for any reason be judged legally invalid or ineffective under
86
+ applicable law, such partial invalidity or ineffectiveness shall not
87
+ invalidate the remainder of the License, and in such case Affirmer hereby
88
+ affirms that he or she will not (i) exercise any of his or her remaining
89
+ Copyright and Related Rights in the Work or (ii) assert any associated claims
90
+ and causes of action with respect to the Work, in either case contrary to
91
+ Affirmer's express Statement of Purpose.
92
+
93
+ 4. Limitations and Disclaimers.
94
+
95
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
96
+ surrendered, licensed or otherwise affected by this document.
97
+
98
+ b. Affirmer offers the Work as-is and makes no representations or warranties
99
+ of any kind concerning the Work, express, implied, statutory or otherwise,
100
+ including without limitation warranties of title, merchantability, fitness
101
+ for a particular purpose, non infringement, or the absence of latent or
102
+ other defects, accuracy, or the present or absence of errors, whether or not
103
+ discoverable, all to the greatest extent permissible under applicable law.
104
+
105
+ c. Affirmer disclaims responsibility for clearing rights of other persons
106
+ that may apply to the Work or any use thereof, including without limitation
107
+ any person's Copyright and Related Rights in the Work. Further, Affirmer
108
+ disclaims responsibility for obtaining any necessary consents, permissions
109
+ or other rights required for any use of the Work.
110
+
111
+ d. Affirmer understands and acknowledges that Creative Commons is not a
112
+ party to this document and has no duty or obligation with respect to this
113
+ CC0 or use of the Work.
114
+
115
+ For more information, please see
116
+ <http://creativecommons.org/publicdomain/zero/1.0/>
@@ -0,0 +1,40 @@
1
+ # Elasticfusion
2
+
3
+ [![Build Status](https://travis-ci.org/little-bobby-tables/elasticfusion.svg?branch=master)](https://travis-ci.org/little-bobby-tables/elasticfusion)
4
+ [![Test Coverage](https://codeclimate.com/github/little-bobby-tables/elasticfusion/badges/coverage.svg)](https://codeclimate.com/github/little-bobby-tables/elasticfusion/coverage)
5
+ [![Gem Version](https://badge.fury.io/rb/elasticfusion.svg)](https://badge.fury.io/rb/elasticfusion)
6
+
7
+ Elasticfusion provides additional functionality on top of [*elasticsearch-rails*](https://github.com/elastic/elasticsearch-rails).
8
+
9
+ It includes:
10
+ * a keyword-based case-insensitive search engine supporting boolean expressions
11
+ (conjunction, disjunction, negation) and field queries (range);
12
+ * background jobs to carry out index updates.
13
+
14
+ It was written with a specific use case in mind and as such places a
15
+ number of restrictions on the allowed use of Elasticsearch.
16
+ For instance, partial updates are not supported, and `_source` field
17
+ in general is encouraged to be disabled.
18
+
19
+ ### Requirements
20
+
21
+ * Rails 5
22
+ * Elasticsearch 5
23
+
24
+ ### Setup
25
+
26
+ 1. Place your index definitions in *app/indexes* directory.
27
+
28
+ 2. Drop `Elasticfusion.load_index_definitions` in *app/models/application_record.rb*
29
+ (or any autoloaded file, really).
30
+
31
+ 3. Add `indexing` queue to your Active Job backend.
32
+
33
+ ### Usage examples
34
+
35
+ Elasticfusion was written specifically for [fanuniverse](https://www.github.com/little-bobby-tables/fanuniverse).
36
+ Refer to it for real-world usage examples.
37
+
38
+ ### Aknowledgements
39
+
40
+ This gem was largely inspired by search handling in *booru-on-rails* ([Derpibooru](https://www.derpibooru.org)).
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.warning = false
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
+
4
+ require 'elasticfusion/version'
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'elasticfusion'
7
+ spec.version = Elasticfusion::VERSION
8
+ spec.authors = ['little-bobby-tables']
9
+ spec.email = ['little-bobby-tables@users.noreply.github.com']
10
+
11
+ spec.summary = 'elasticsearch-rails extensions'
12
+ spec.homepage = 'https://github.com/little-bobby-tables/elasticfusion'
13
+ spec.license = 'CC0-1.0'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'rails', '>= 5.0'
21
+ spec.add_runtime_dependency 'chronic'
22
+ spec.add_runtime_dependency 'elasticsearch'
23
+ spec.add_runtime_dependency 'elasticsearch-rails'
24
+ spec.add_runtime_dependency 'elasticsearch-model'
25
+
26
+ spec.add_development_dependency 'openssl'
27
+ spec.add_development_dependency 'sqlite3'
28
+ spec.add_development_dependency 'minitest-reporters'
29
+ spec.add_development_dependency 'simplecov'
30
+ spec.add_development_dependency 'codeclimate-test-reporter'
31
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/all'
3
+ require 'elasticsearch/model'
4
+
5
+ require 'elasticfusion/hooks'
6
+ require 'elasticfusion/loader'
7
+ require 'elasticfusion/definition'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ require 'elasticfusion/model/settings'
3
+ require 'elasticfusion/model/indexing'
4
+ require 'elasticfusion/model/searching'
5
+
6
+ module Elasticfusion
7
+ def self.define(cls, &block)
8
+ cls.class_eval do
9
+ include Elasticsearch::Model
10
+
11
+ def self.elasticfusion(&block)
12
+ @elasticfusion ||= Model::Settings.new(self, &block)
13
+ end
14
+ end
15
+
16
+ cls.class_eval(&block)
17
+
18
+ # Model extensions may rely on settings set with the block
19
+ # and should only be included after evaluating it.
20
+ cls.class_eval do
21
+ include Model::Indexing
22
+ include Model::Searching
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ module Elasticfusion
3
+ module ActiveRecordAdapterPatch
4
+ # https://github.com/elastic/elasticsearch-rails/issues/608,
5
+ # might be related to https://github.com/elastic/elasticsearch-rails/issues/258,
6
+ # though in my experience it only affects Rails 5+.
7
+ def records
8
+ @_records_sorted ||= super.to_a
9
+ end
10
+ end
11
+ end
12
+
13
+ Elasticsearch::Model::Adapter::ActiveRecord::Records.prepend(
14
+ Elasticfusion::ActiveRecordAdapterPatch)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ module Elasticfusion
3
+ module Jobs
4
+ class ReindexJob < ActiveJob::Base
5
+ queue_as :indexing
6
+
7
+ def perform(model_class, id)
8
+ model_class.constantize.find(id).reindex_now
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ module Elasticfusion
3
+ # Call this method from within an autoloaded file (e.g. models/application_record.rb)
4
+ # to instantiate index definitions and refresh them in development, when Rails
5
+ # autoreloads constants.
6
+ def self.load_index_definitions
7
+ Dir.glob(Rails.root.join('app', 'indexes', '*.rb'), &method(:load))
8
+ end
9
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+ require 'elasticfusion/jobs/reindex_job'
3
+
4
+ module Elasticfusion
5
+ module Model
6
+ module Indexing
7
+ def self.included(model)
8
+ model.class_eval do
9
+ after_commit(on: :create) do
10
+ __elasticsearch__.index_document
11
+ end
12
+
13
+ after_commit(on: :destroy) do
14
+ __elasticsearch__.delete_document
15
+ end
16
+
17
+ if elasticfusion[:reindex_when_updated]
18
+ after_commit(on: :update) do |record|
19
+ if (record.previous_changes.keys.map(&:to_sym) &
20
+ record.class.elasticfusion[:reindex_when_updated]).any?
21
+ record.reindex_later
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def reindex_later
29
+ Elasticfusion::Jobs::ReindexJob.perform_later(self.class.to_s, self.id)
30
+ end
31
+
32
+ def reindex_now
33
+ __elasticsearch__.index_document
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require 'elasticfusion/search/wrapper'
3
+
4
+ module Elasticfusion
5
+ module Model
6
+ module Searching
7
+ def self.included(model)
8
+ model.class_eval do
9
+ def self.custom_search(query = nil, &block)
10
+ Search::Wrapper.new(self, query, &block)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end