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 +5 -5
- data/README.md +44 -16
- data/lib/mastico.rb +0 -1
- data/lib/mastico/query.rb +25 -25
- data/lib/mastico/version.rb +1 -1
- data/mastico.gif +0 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a71e46abdec31f568afd7a0e617dbba73df92acd7b02a99125a247ba6e9bb6ca
|
4
|
+
data.tar.gz: 8b583babd44479e1adf3a5e7a65fbacdd67cc6ece8df3d990505da44ef05d7e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3493cf5c54e66b30340e01d63328940e7890083fd955d7a6f832236821a4967ead5a0cb75735206ce4f8ee7052719e3e5711fcec8c4fdff416a4313b542b2be
|
7
|
+
data.tar.gz: '04298599aa136610a449f47e43f3dd97430caa75fc7a438afcd3406f4b0e50c2965bd3a1df1e287a431f0314a9c0c3806a3903adcced83e4788fa53330be9615'
|
data/README.md
CHANGED
@@ -1,13 +1,16 @@
|
|
1
|
+

|
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
|
-
|
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
|
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(
|
21
|
+
query = Mastico::Query.new(
|
22
|
+
fields: [:title, :description], query: "ciao"
|
23
|
+
).apply(query)
|
19
24
|
```
|
20
|
-
|
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
|
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 "
|
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(
|
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(
|
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.
|
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`.
|
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
|
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
|
-
|
data/lib/mastico.rb
CHANGED
data/lib/mastico/query.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
module Mastico
|
2
2
|
class Query
|
3
3
|
DEFAULT_OPTIONS = {
|
4
|
-
|
4
|
+
word_boost: 1.0,
|
5
5
|
prefix_boost: 0.7,
|
6
6
|
infix_boost: 0.4,
|
7
7
|
fuzzy_boost: 0.2,
|
8
|
-
|
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 = [:
|
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
|
-
|
41
|
-
|
42
|
-
weight = word_weight.call(
|
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(
|
44
|
+
chewy_term_query(word: word, weight: weight)
|
45
45
|
end
|
46
|
-
|
47
|
-
case
|
46
|
+
word_subqueries = word_subqueries.compact
|
47
|
+
case word_subqueries.size
|
48
48
|
when 0
|
49
49
|
nil
|
50
50
|
when 1
|
51
|
-
|
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:
|
55
|
+
{bool: {must: word_subqueries}} # AND query
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
def chewy_term_query(
|
60
|
+
def chewy_term_query(word:, weight:)
|
61
61
|
parts = []
|
62
62
|
|
63
|
-
if
|
64
|
-
relevant_fields(:
|
65
|
-
parts <<
|
66
|
-
query:
|
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[:
|
68
|
+
boost: options[:word_boost] * weight * field_boost
|
69
69
|
)
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
-
if
|
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:
|
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
|
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:
|
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
|
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:
|
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
|
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
|
data/lib/mastico/version.rb
CHANGED
data/mastico.gif
ADDED
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.
|
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-
|
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.
|
112
|
+
rubygems_version: 2.7.6
|
112
113
|
signing_key:
|
113
114
|
specification_version: 4
|
114
115
|
summary: A chewy helper
|