mastico 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 10574d0b9586998f4d7fd7543d5c7ef614e28a29
4
- data.tar.gz: bd2cad87ba6821b050e3483d857acdf2c4f1cd8a
2
+ SHA256:
3
+ metadata.gz: a71e46abdec31f568afd7a0e617dbba73df92acd7b02a99125a247ba6e9bb6ca
4
+ data.tar.gz: 8b583babd44479e1adf3a5e7a65fbacdd67cc6ece8df3d990505da44ef05d7e3
5
5
  SHA512:
6
- metadata.gz: 3899db6f982100299f90d94db7fb8e81c6b97513906ef3aaa2247bb1859cfaee7341341b293892e7c41613e69701ad458cf43315afd26867c72cdd2bf2fe44a1
7
- data.tar.gz: 6133e753e4409c3fe2e9482d02b198a99f4eae19acdfb82bce766ddc685293ba25b4de95d678624c8420238142116762a646b79ef3957ee93d708a5033c35e6a
6
+ metadata.gz: a3493cf5c54e66b30340e01d63328940e7890083fd955d7a6f832236821a4967ead5a0cb75735206ce4f8ee7052719e3e5711fcec8c4fdff416a4313b542b2be
7
+ data.tar.gz: '04298599aa136610a449f47e43f3dd97430caa75fc7a438afcd3406f4b0e50c2965bd3a1df1e287a431f0314a9c0c3806a3903adcced83e4788fa53330be9615'
data/README.md CHANGED
@@ -1,13 +1,16 @@
1
+ ![Mastico Logo](https://raw.github.com/cantierecreativo/mastico/master/mastico.gif)
2
+
1
3
  # Mastico
2
4
 
3
5
  Make common queries simple, and make **some** complex queries possible.
4
6
 
7
+ ## Why does this exist?
5
8
 
6
- ## Why does this exists?
7
-
8
- Creating Elasticsearch queries requires a specialist or hours of comparing Stack Overflow posts.
9
+ Using a basic client, creating Elasticsearch queries requires specialist
10
+ knowledge or hours of comparing Stack Overflow posts.
9
11
 
10
- Chewy makes managing and updating indexes simple, but the query interface remains the same.
12
+ Chewy makes managing and updating indexes simple, but the query interface
13
+ remains the same.
11
14
 
12
15
  ## Example 1:
13
16
 
@@ -15,25 +18,33 @@ Mastico creates queries based on a list of fields:
15
18
 
16
19
  ```ruby
17
20
  query = FooIndex.query
18
- query = Mastico::Query.new(fields: [:title, description], query: "ciao").apply(query)
21
+ query = Mastico::Query.new(
22
+ fields: [:title, :description], query: "ciao"
23
+ ).apply(query)
19
24
  ```
20
- This creates a series of queries: word, prefix, infix and fuzzy match of the word `ciao` against the supplied fields.
25
+
26
+ This creates a series of queries: `word`, `prefix`, `infix` and `fuzzy` matches
27
+ of the word `ciao` against the supplied fields.
21
28
 
22
29
  ## Example 2:
23
30
 
24
- What if I want to block some words ("Stop words") of give ore weight to others?
31
+ What if I want to block some words ("Stop words") of give more weight to others?
25
32
 
26
33
  ```ruby
27
34
  def weight(word)
28
35
  case word
29
- when "I"
36
+ when "information"
30
37
  0
31
38
  else
32
39
  1.0
33
40
  end
34
41
  end
35
42
 
36
- query = Mastico::Query.new(fields: [:title, description], word_weight: method(:weight), query: "I like cheese").apply(query)
43
+ query = Mastico::Query.new(
44
+ fields: [:title, description],
45
+ word_weight: method(:weight),
46
+ query: "Information is power"
47
+ ).apply(query)
37
48
  ```
38
49
 
39
50
  ## Example 3:
@@ -41,10 +52,21 @@ query = Mastico::Query.new(fields: [:title, description], word_weight: method(:w
41
52
  What if I don't want all the different types of matching?
42
53
 
43
54
  ```ruby
44
- query = Mastico::Query.new(fields: {title: {boost: 4.0, types: [:term]} }, query: "Simple").apply(query)
55
+ query = Mastico::Query.new(
56
+ fields: {title: {boost: 4.0, types: [:word]} },
57
+ query: "Simple"
58
+ ).apply(query)
45
59
  ```
46
- This will return only the `term` type search for the attribute `title`.
47
60
 
61
+ This will return only the `word` type search (i.e. whole words) for the
62
+ attribute `title`.
63
+
64
+ The types of matching that are available are:
65
+
66
+ * `:word` - whole words,
67
+ * `:prefix` - the beginning of words,
68
+ * `:infix` - some part of a word,
69
+ * `:fuzzy` - similar words.
48
70
 
49
71
  ## Installation
50
72
 
@@ -64,16 +86,22 @@ Or install it yourself as:
64
86
 
65
87
  ## Development
66
88
 
67
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
89
+ After checking out the repo, run `bin/setup` to install dependencies.
90
+ Then, run `rake spec` to run the tests. You can also run `bin/console` for an
91
+ interactive prompt that will allow you to experiment.
68
92
 
69
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+ To install this gem onto your local machine, run `bundle exec rake install`.
94
+ To release a new version, update the version number in `version.rb`, and then
95
+ run `bundle exec rake release`, which will create a git tag for the version,
96
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
97
 
71
98
  ## Contributing
72
99
 
73
- Bug reports and pull requests are welcome on GitHub at https://github.com/cantierecreativo/mastico. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
74
-
100
+ Bug reports and pull requests are welcome on GitHub at
101
+ https://github.com/cantierecreativo/mastico. This project is intended to be a
102
+ safe, welcoming space for collaboration, and contributors are expected to adhere
103
+ to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
75
104
 
76
105
  ## License
77
106
 
78
107
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
79
-
@@ -1,5 +1,4 @@
1
1
  require "mastico/version"
2
- require "mastico/version"
3
2
  require "mastico/query"
4
3
 
5
4
  module Mastico
@@ -1,18 +1,18 @@
1
1
  module Mastico
2
2
  class Query
3
3
  DEFAULT_OPTIONS = {
4
- term_boost: 1.0,
4
+ word_boost: 1.0,
5
5
  prefix_boost: 0.7,
6
6
  infix_boost: 0.4,
7
7
  fuzzy_boost: 0.2,
8
- minimum_term_length: 2,
8
+ minimum_word_length: 2,
9
9
  minimum_prefix_length: 3,
10
10
  minimum_infix_length: 3,
11
11
  minimum_fuzzy_length: 5,
12
12
  fuzziness: 4
13
13
  }.freeze
14
14
 
15
- QUERY_TYPES = [:term, :prefix, :infix, :fuzzy].freeze
15
+ QUERY_TYPES = [:word, :prefix, :infix, :fuzzy].freeze
16
16
 
17
17
  attr_reader :query
18
18
  attr_reader :fields
@@ -37,63 +37,63 @@ module Mastico
37
37
  @parts ||=
38
38
  begin
39
39
  clean = query.strip.gsub(/\s+/, ' ').downcase
40
- terms = clean.split(" ")
41
- term_parts = terms.map do |term|
42
- weight = word_weight.call(term)
40
+ words = clean.split(" ")
41
+ word_subqueries = words.map do |word|
42
+ weight = word_weight.call(word)
43
43
  next nil if weight == 0.0
44
- chewy_term_query(term: term, weight: weight)
44
+ chewy_term_query(word: word, weight: weight)
45
45
  end
46
- term_parts = term_parts.compact
47
- case term_parts.size
46
+ word_subqueries = word_subqueries.compact
47
+ case word_subqueries.size
48
48
  when 0
49
49
  nil
50
50
  when 1
51
- term_parts[0]
51
+ word_subqueries[0]
52
52
  else
53
53
  # We have multiple words in the query -
54
54
  # all of them **must** match
55
- {bool: {must: term_parts}} # AND query
55
+ {bool: {must: word_subqueries}} # AND query
56
56
  end
57
57
  end
58
58
  end
59
59
 
60
- def chewy_term_query(term:, weight:)
60
+ def chewy_term_query(word:, weight:)
61
61
  parts = []
62
62
 
63
- if term.length >= options[:minimum_term_length]
64
- relevant_fields(:term).each do |field, field_boost|
65
- parts << chewy_multi_match_term_query(
66
- query: term,
63
+ if word.length >= options[:minimum_word_length]
64
+ relevant_fields(:word).each do |field, field_boost|
65
+ parts << chewy_multi_match_word_query(
66
+ query: word,
67
67
  fields: [field],
68
- boost: options[:term_boost] * weight * field_boost
68
+ boost: options[:word_boost] * weight * field_boost
69
69
  )
70
70
  end
71
71
  end
72
72
 
73
- if term.length >= options[:minimum_prefix_length]
73
+ if word.length >= options[:minimum_prefix_length]
74
74
  relevant_fields(:prefix).each do |field, field_boost|
75
75
  parts << chewy_multi_match_prefix_query(
76
- query: term,
76
+ query: word,
77
77
  fields: [field],
78
78
  boost: options[:prefix_boost] * weight * field_boost
79
79
  )
80
80
  end
81
81
  end
82
82
 
83
- if term.length >= options[:minimum_infix_length]
83
+ if word.length >= options[:minimum_infix_length]
84
84
  relevant_fields(:infix).each do |field, field_boost|
85
85
  parts << chewy_multi_match_infix_query(
86
- query: term,
86
+ query: word,
87
87
  fields: [field],
88
88
  boost: options[:infix_boost] * weight * field_boost
89
89
  )
90
90
  end
91
91
  end
92
92
 
93
- if term.length >= options[:minimum_fuzzy_length]
93
+ if word.length >= options[:minimum_fuzzy_length]
94
94
  relevant_fields(:fuzzy).each do |field, field_boost|
95
95
  parts << chewy_multi_match_fuzzy_query(
96
- query: term,
96
+ query: word,
97
97
  fields: [field],
98
98
  boost: options[:fuzzy_boost] * weight * field_boost
99
99
  )
@@ -109,7 +109,7 @@ module Mastico
109
109
 
110
110
  def relevant_fields(search_type)
111
111
  if fields.is_a? Array
112
- fields.map {|field| [field, 1]}.to_h
112
+ fields.map { |field| [field, 1] }.to_h
113
113
  else
114
114
  fields.select do |field, options|
115
115
  if options[:types]
@@ -123,7 +123,7 @@ module Mastico
123
123
  end
124
124
  end
125
125
 
126
- def chewy_multi_match_term_query(query:, fields:, boost: nil)
126
+ def chewy_multi_match_word_query(query:, fields:, boost: nil)
127
127
  return nil if fields.empty?
128
128
 
129
129
  lower = query.downcase
@@ -1,3 +1,3 @@
1
1
  module Mastico
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mastico
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Manzo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-23 00:00:00.000000000 Z
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -87,6 +87,7 @@ files:
87
87
  - lib/mastico/query.rb
88
88
  - lib/mastico/version.rb
89
89
  - mastico.gemspec
90
+ - mastico.gif
90
91
  homepage: https://github.com/cantierecreativo/mastico
91
92
  licenses:
92
93
  - MIT
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  requirements: []
110
111
  rubyforge_project:
111
- rubygems_version: 2.5.1
112
+ rubygems_version: 2.7.6
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: A chewy helper