picky 1.1.5 → 1.1.6
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.
- data/lib/picky/index_api.rb +86 -18
- metadata +2 -2
data/lib/picky/index_api.rb
CHANGED
@@ -1,13 +1,22 @@
|
|
1
|
-
# This class defines the indexing and index API that is exposed to the user
|
1
|
+
# This class defines the indexing and index API that is exposed to the user
|
2
|
+
# as the #index method inside the Application class.
|
3
|
+
#
|
2
4
|
# It provides a single front for both indexing and index options.
|
3
5
|
#
|
4
6
|
# Note: An Index holds both an *Indexed*::*Index* and an *Indexing*::*Type*.
|
5
7
|
#
|
6
|
-
class IndexAPI
|
8
|
+
class IndexAPI
|
7
9
|
|
8
10
|
attr_reader :name, :indexing, :indexed
|
9
11
|
|
10
|
-
#
|
12
|
+
# Create a new index with a given source.
|
13
|
+
#
|
14
|
+
# Parameters:
|
15
|
+
# * name: A name that will be used for the index directory and in the Picky front end.
|
16
|
+
# * source: Where the data comes from, e.g. Sources::CSV.new(...)
|
17
|
+
#
|
18
|
+
# Options:
|
19
|
+
# * after_indexing: Currently only used in the db source. Executes the given after_indexing as SQL after the indexing process.
|
11
20
|
#
|
12
21
|
def initialize name, source, options = {}
|
13
22
|
@name = name
|
@@ -19,7 +28,18 @@ class IndexAPI # :nodoc:all
|
|
19
28
|
Indexes.register self
|
20
29
|
end
|
21
30
|
|
22
|
-
#
|
31
|
+
# Defines a searchable category on the index.
|
32
|
+
#
|
33
|
+
# Parameters:
|
34
|
+
# category_name: This identifier is used in the front end, but also to categorize query text. For example, “title:hobbit” will narrow the hobbit query on categories with the identifier :title.
|
35
|
+
#
|
36
|
+
# Options:
|
37
|
+
# * partial: Partial::None.new or Partial::Substring.new(from: starting_char, to: ending_char). Default is Partial::Substring.new(from: -3, to: -1).
|
38
|
+
# * similarity: Similarity::None.new or Similarity::Phonetic.new(similar_words_searched). Default is Similarity::None.new.
|
39
|
+
# * qualifiers: An array of qualifiers with which you can define which category you’d like to search, for example “title:hobbit” will search for hobbit in just title categories. Example: qualifiers: [:t, :titre, :title] (use it for example with multiple languages). Default is the name of the category.
|
40
|
+
# * qualifier: Convenience options if you just need a single qualifier, see above. Example: qualifiers => :title. Default is the name of the category.
|
41
|
+
# * source: Use a different source than the index uses. If you think you need that, there might be a better solution to your problem. Please post to the mailing list first with your application.rb :)
|
42
|
+
# * from: Take the data from the data category with this name. Example: You have a source Sources::CSV.new(:title, file:'some_file.csv') but you want the category to be called differently. The you use from: define_category(:similar_title, :from => :title).
|
23
43
|
#
|
24
44
|
def define_category category_name, options = {}
|
25
45
|
category_name = category_name.to_sym
|
@@ -33,33 +53,82 @@ class IndexAPI # :nodoc:all
|
|
33
53
|
end
|
34
54
|
alias category define_category
|
35
55
|
|
36
|
-
#
|
56
|
+
# HIGHLY EXPERIMENTAL
|
37
57
|
#
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
58
|
+
# Make this category range searchable.
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# You have data values inside 1..100, and you want to have Picky return
|
62
|
+
# not only the results for 47 if you search for 47, but also results for
|
63
|
+
# 45, 46, or 47.2, 48.9, in a range of 2 around 47, so (45..49).
|
64
|
+
#
|
65
|
+
# Then you use:
|
66
|
+
# my_index.define_location :values_inside_1_100, 2
|
67
|
+
#
|
68
|
+
# Optionally, you give it a precision value to reduce the error margin
|
69
|
+
# around 47 (Picky is a bit liberal).
|
70
|
+
# my_index.define_location :values_inside_1_100, 2, precision: 5
|
71
|
+
#
|
72
|
+
# This will force Picky to maximally be wrong 5% of the given range value
|
73
|
+
# (5% of 2 = 0.1).
|
74
|
+
#
|
75
|
+
# We suggest not to use more than 5 as a higher precision is more performance intensive.
|
76
|
+
#
|
77
|
+
# Parameters:
|
78
|
+
# * name: The name as used in #define_category.
|
79
|
+
# * radius: The range (in km) around the query point which we search for results.
|
80
|
+
#
|
81
|
+
# -----|<- range ->*------------|-----
|
82
|
+
#
|
83
|
+
# Options
|
84
|
+
# * precision # Default 1 (20% error margin, very fast), up to 5 (5% error margin, slower) makes sense.
|
85
|
+
# * from: # The data category to take the data for this category from.
|
86
|
+
#
|
87
|
+
def define_location name, range, options = {}
|
42
88
|
precision = options[:precision]
|
43
89
|
|
44
90
|
options = { partial: Partial::None.new }.merge options
|
45
91
|
|
46
92
|
define_category name, options do |indexing, indexed|
|
47
|
-
indexing.source = Sources::Wrappers::Location.new indexing, grid:
|
93
|
+
indexing.source = Sources::Wrappers::Location.new indexing, grid: range, precision: precision
|
48
94
|
indexing.tokenizer = Tokenizers::Index.new
|
49
95
|
|
50
|
-
exact_bundle = Indexed::Wrappers::Bundle::Location.new indexed.exact, grid:
|
96
|
+
exact_bundle = Indexed::Wrappers::Bundle::Location.new indexed.exact, grid: range, precision: precision
|
51
97
|
indexed.exact = exact_bundle
|
52
98
|
indexed.partial = exact_bundle # A partial token also uses the exact index.
|
53
99
|
end
|
54
100
|
end
|
55
101
|
alias location define_location
|
56
102
|
|
103
|
+
# HIGHLY EXPERIMENTAL Not correctly working yet. Try it if you feel "beta".
|
104
|
+
#
|
105
|
+
# Also a range search see #define_location, but on the earth's surface.
|
106
|
+
#
|
107
|
+
# Parameters:
|
108
|
+
# * name: The name as used in #define_category.
|
109
|
+
# * radius: The distance (in km) around the query point which we search for results.
|
110
|
+
#
|
111
|
+
# Note: Picky uses a square, not a circle. We hope that's ok for most usages.
|
112
|
+
#
|
113
|
+
# -----------------------------
|
114
|
+
# | |
|
115
|
+
# | |
|
116
|
+
# | |
|
117
|
+
# | |
|
118
|
+
# | |
|
119
|
+
# | *<- radius ->|
|
120
|
+
# | |
|
121
|
+
# | |
|
122
|
+
# | |
|
123
|
+
# | |
|
124
|
+
# | |
|
125
|
+
# -----------------------------
|
126
|
+
#
|
57
127
|
# Options
|
58
|
-
# *
|
128
|
+
# * precision: Default 1 (20% error margin, very fast), up to 5 (5% error margin, slower) makes sense.
|
129
|
+
# * from: The data category to take the data for this category from.
|
59
130
|
#
|
60
|
-
def define_map_location name, options = {}
|
61
|
-
radius = options[:radius] || raise("Option :radius needs to be set on define_map_location, it defines the search radius.")
|
62
|
-
|
131
|
+
def define_map_location name, radius, options = {}
|
63
132
|
# The radius is given as if all the locations were on the equator.
|
64
133
|
#
|
65
134
|
# TODO Need to recalculate since not many locations are on the equator ;) This is just a prototype.
|
@@ -69,8 +138,7 @@ class IndexAPI # :nodoc:all
|
|
69
138
|
# A degree on the equator is equal to ~111,319.9 meters.
|
70
139
|
# So a km on the equator is equal to 0.00898312 degrees.
|
71
140
|
#
|
72
|
-
|
73
|
-
|
74
|
-
define_location name, options
|
141
|
+
define_location name, radius * 0.00898312, options
|
75
142
|
end
|
143
|
+
alias map_location define_map_location
|
76
144
|
end
|