-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoverrides.rb
112 lines (97 loc) · 2.32 KB
/
overrides.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# frozen_string_literal: true
##
# These examples walk you through operations specifically related to result overrides / curation
require_relative 'client_initialization'
# Delete the collection if it already exists
begin
@typesense.collections['companies'].delete
rescue Typesense::Error::ObjectNotFound
end
##
# Create a collection
schema = {
'name' => 'companies',
'fields' => [
{
'name' => 'company_name',
'type' => 'string'
},
{
'name' => 'num_employees',
'type' => 'int32'
},
{
'name' => 'country',
'type' => 'string',
'facet' => true
}
],
'default_sorting_field' => 'num_employees'
}
@typesense.collections.create(schema)
# Let's create a couple documents for us to use in our search examples
@typesense.collections['companies'].documents.create(
'id' => '124',
'company_name' => 'Stark Industries',
'num_employees' => 5215,
'country' => 'USA'
)
@typesense.collections['companies'].documents.create(
'id' => '127',
'company_name' => 'Stark Corp',
'num_employees' => 1031,
'country' => 'USA'
)
@typesense.collections['companies'].documents.create(
'id' => '125',
'company_name' => 'Acme Corp',
'num_employees' => 1002,
'country' => 'France'
)
@typesense.collections['companies'].documents.create(
'id' => '126',
'company_name' => 'Doofenshmirtz Inc',
'num_employees' => 2,
'country' => 'Tri-State Area'
)
##
# Create overrides
@typesense.collections['companies'].overrides.upsert(
'promote-doofenshmirtz',
rule: {
query: 'doofen',
match: 'exact'
},
includes: [{ 'id' => '126', 'position' => 1 }]
)
@typesense.collections['companies'].overrides.upsert(
'promote-acme',
rule: {
query: 'stark',
match: 'exact'
},
includes: [{ 'id' => '125', 'position' => 1 }]
)
##
# Search for documents
results = @typesense.collections['companies'].documents.search(
'q' => 'doofen',
'query_by' => 'company_name'
)
ap results
results = @typesense.collections['companies'].documents.search(
'q' => 'stark',
'query_by' => 'company_name'
)
ap results
results = @typesense.collections['companies'].documents.search(
'q' => 'Inc',
'query_by' => 'company_name',
'filter_by' => 'num_employees:<100',
'sort_by' => 'num_employees:desc'
)
ap results
##
# Cleanup
# Drop the collection
@typesense.collections['companies'].delete