-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathlogstash.conf
211 lines (179 loc) · 6.12 KB
/
logstash.conf
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
input {
# read the items file
file {
# TODO: fill in the absolute path to the file, or use a relative path to Logstash's home directory
# path => "/PATH/TO/meta_sports_20k_sample.json"
# read the file from the beginning
start_position => "beginning"
# on Logstash restart, forget where we left off and start over again
sincedb_path => "/dev/null"
add_field => { "document_type" => "item" }
}
# read the reviews file
file {
# TODO: fill in the absolute path to the file, or use a relative path to Logstash's home directory
# path => "/PATH/TO/reviews_sports_24k_sample.json"
start_position => "beginning"
sincedb_path => "/dev/null"
# this file is actually line-delimited JSON, so we use the json codec
codec => "json"
add_field => { "document_type" => "review" }
}
}
filter {
#############
### ITEMS ###
#############
if [document_type] == "item" {
# parse the python literal format
ruby {
code => '
require "json"
python_literal = event.get("message")
parsed_data = eval(python_literal)
# Copy each field to the root level, converting symbol keys to strings
parsed_data.each do |key, value|
event.set(key.to_s, value)
end
'
}
# add a random timestamp to the document, up to one year ago
ruby {
code => "
current_time = Time.now.to_i
one_year_ago = current_time - (60 * 60 * 24 * 365)
event.set('timestamp', rand(one_year_ago..current_time))
"
}
# In the data set, categories are of the form
# [ ["Sports & Outdoors", "Accessories", "Sport Watches"] ]
# For filtering on categories, these should be matched exactly, so we transform to
# [ "Sports & Outdoors", "Sports & Outdoors|Accessories", "Sports & Outdoors|Accessories|Sport Watches"]
# because there are multiple subcategories with the same name, and
# we want to maintain the category hierarchy for grouping.
# For free text search however, we want to match on stemmed terms.
# We have another field for this, and reverse the categories for better relevance:
# "Sport Watches Accessories Sports & Outdoors"
ruby {
code => "
if event.get('categories')
categories = event.get('categories')
if categories.is_a?(Array)
# Transform categories into the desired format
transformed = []
categories_text = []
categories.each do |cat_array|
current_path = ''
# Build the hierarchical paths
cat_array.each do |category|
current_path = current_path.empty? ? category : current_path + '|' + category
transformed << current_path
end
# Add reversed categories for text search
categories_text << cat_array.reverse.join(' ')
end
# Set the transformed categories back to the event
event.set('categories', transformed)
event.set('categories_text', categories_text.join(' '))
end
end
"
}
# Flatten related products arrays into a single array
ruby {
code => "
if event.get('related')
related = []
event.get('related').each do |_, products|
related.concat(products)
end
event.set('related', related.uniq)
end
"
}
# Convert imUrl to images array
ruby {
code => "
if imUrl = event.get('imUrl')
event.set('images', [imUrl])
event.remove('imUrl')
end
"
}
# initialize rating_stars and rating_count to 0
mutate {
add_field => { "rating_stars" => 0 }
add_field => { "rating_count" => 0 }
}
# the Vespa ID is the product ID
mutate {
add_field => { "vespa_id" => "%{asin}" }
}
}
###############
### REVIEWS ###
###############
if [document_type] == "review" {
# Check for illegal characters and drop the document if found
ruby {
code => "
illegal_char = '\u001a'
if event.get('reviewer_name')&.include?(illegal_char) ||
event.get('title')&.include?(illegal_char) ||
event.get('text')&.include?(illegal_char)
event.cancel
end
"
}
# rename fields to match the Vespa schema
mutate {
rename => { "reviewerID" => "reviewer_id" }
rename => { "reviewerName" => "reviewer_name" }
rename => { "unixReviewTime" => "timestamp" }
rename => { "reviewText" => "text" }
rename => { "summary" => "title" }
rename => { "overall" => "stars" }
convert => { "stars" => "integer" }
}
# Convert helpful array to upvotes and downvotes
ruby {
code => "
if helpful = event.get('helpful')
upvotes = helpful[0].to_i
total = helpful[1].to_i
event.set('upvotes', upvotes)
event.set('downvotes', total - upvotes)
event.remove('helpful')
end
"
}
# the Vespa ID is the product ID + the reviewer ID
mutate {
add_field => { "vespa_id" => "%{asin}-%{reviewer_id}" }
}
}
##############
### COMMON ###
##############
mutate {
# remove unnecessary fields
remove_field => ["@timestamp", "@version", "event", "host", "log",
"message", "file", "original", "salesRank", "reviewTime"]
}
}
output {
#stdout { codec => rubydebug }
# write to Vespa
vespa_feed {
vespa_url => "http://localhost:8080"
document_type => "%{document_type}"
namespace => "%{document_type}"
operation => "put"
id_field => "vespa_id"
# remove the id field from the document itself (we have this info in other fields of the documents anyway)
remove_id => true
# remove the "document_type" field from the document, which provides the document type (and the namespace)
# this is the metadata field that we added in the "input" section
remove_document_type => true
}
}