It seems to me as if it would make sense to simply install these categories as fixed, in the index definition – since it seems they are not truly dynamic, but do have expected values.
If this is not the case, check this out. You can copy it and run it in a Ruby script. Note that there is a problem at the end, which I will need to fix (explanation at the very end).
require 'picky'
require 'json'
# Some request.
#
Request = Struct.new :id, :url, :some_headers_as_json
# Define the url category.
#
data = Picky::Index.new :request do
category :url, indexing: { splits_text_on: /[\/\?\&\=]/ }
end
# Add a request.
#
data.add Request.new(1, '/hello/test', '{"a":"b","c":"d"}')
# Check out some internals (just for fun).
#
p data[:url].exact.inverted # The index
p data[:url].tokenizer.split("a/b/c") # How the tokenizer of url splits.
# Prepare a search interface.
#
requests = Picky::Search.new data
# Try a search.
#
p requests.search 'test'
# Dynamically add category.
#
request = Request.new(2, '/with/json', '{"a":"b","c":"d"}')
json = JSON.parse request.some_headers_as_json
# Check it out.
#
p json
json.each do |key, value|
key = key.to_sym
p "Adding value #{value} to category #{key}"
data[key] rescue data.category(key)
end
# Let's search for it:
#
p requests.search 'b'
p requests.search 'c:d' # Whoops. This is a problem!
I get this as output:
~/temp/picky-examples $ ruby google_groups_julian_porta.rb
{"hello"=>[1], "test"=>[1]}
["a", "b", "c"]
>|2012-08-27 21:00:59|0.000098|test | 1| 0| 1|
{"a"=>"b", "c"=>"d"}
"Adding value b to category a"
"Adding value d to category c"
>|2012-08-27 21:00:59|0.000046|b | 1| 0| 1|
>|2012-08-27 21:00:59|0.000017|c:d | 0| 0| 0|
Regarding the error:
Why does "c:d" fail? Picky sets up the "category mapping" once, at the beginning, so only categories which are defined at the beginning are included in the qualifiers, a qualifier being the thing that tells d what it must be, eg. "c:d" tells Picky that you want to find d in c, and that Picky must find it in there and give up if it isn't.
So in the next version this needs to be done dynamically, after adding each category instead of at the beginning. Sorry about that – Picky has only recently become a realtime system, so this has not yet been a concern.
Please tell me if all this helps.
Cheers,
Florian