question about allocations

24 views
Skip to first unread message

railsnmet

unread,
Aug 21, 2012, 8:01:25 PM8/21/12
to Picky-Ruby
I've been playing around with the all-in-one app, which is great but
what I was wondering on how to do is to populate the allocations array
with the results. So far I've been getting bupkis in the returned json
documents but I do have data being produced via the ruby objects. Can
someone tell me how to get picky to produce results in json format
(via the standard picky server)?

Picky / Florian Hanke

unread,
Aug 21, 2012, 8:14:10 PM8/21/12
to picky...@googlegroups.com
Hi there,

To answer your question:

In the standalone server, the code is

  get '/books' do
    results = books.search params[:query], params[:ids] || 20, params[:offset] || 0
    # ...
    results.to_json
  end

So you simply call #to_json on the results object.

I assume this is not what you are looking for, even though it answers your question. If it isn't, can you help us help you by describing what you have (best in code), and what you need.

Cheers,
   Florian

railsnmet

unread,
Aug 21, 2012, 8:36:05 PM8/21/12
to Picky-Ruby
What I'm really looking for is to actually be able to see the result
arrays when I put a request in. Essentially, what I wanted to see is
if I place a query, I want to be able to see the result of that query
in the json packet. Instead I get this for example: {"allocations":
[],"offset":0,"duration":5.5e-05,"total":0}.

On Aug 21, 5:14 pm, Picky / Florian Hanke <florian.ha...@gmail.com>
wrote:

Florian Hanke

unread,
Aug 22, 2012, 2:05:58 AM8/22/12
to picky...@googlegroups.com
Thanks for the example. This means that Picky wasn't able to allocate the query. In other words: No results.

After indexing, have you checked /index whether there is data in the indexes? If there is, could you simplify your configuration and try simpler queries?

Don't hesitate to post your configuration, if it doesn't contain sensitive data.

Cheers,
Florian

railsnmet

unread,
Aug 22, 2012, 2:13:12 AM8/22/12
to picky...@googlegroups.com
I appreciate the help, the indexes were produced, this is what I changed from the default server. I had more before, but I tried to simplify it just to get an example going.
   def each
      instance = Struct.new :id, :longTitle, :shortTitle
      
      @csv.each do |row|
        yield instance.new *row[0..2]
      end
    end

  end

  # Define an index.
  #
  books_index = Index.new :books do
    source { Books.new }
    indexing removes_characters: /[^a-z0-9\s\/\-\_\:\"\&\.]/i,
             stopwords:          /\b(and|the|of|it|in|for)\b/i,
             splits_text_on:     /[\s\/\-\_\:\"\&\.]/
             
    category :longTitle,
             similarity: Similarity::DoubleMetaphone.new(4),
             partial: Partial::Substring.new(from: 1) # Default is from: -3.
    category :shortTitle,
             similarity: Similarity::DoubleMetaphone.new(3),
             partial: Partial::Substring.new(from: 1) # Default is from: -3.
  end

Picky / Florian Hanke

unread,
Aug 22, 2012, 8:33:42 AM8/22/12
to picky...@googlegroups.com
Thanks. And can you confirm that the index files that are produced in the /index subdirectories (see the "prepared" files) are sane and look ok?

railsnmet

unread,
Aug 22, 2012, 12:58:26 PM8/22/12
to picky...@googlegroups.com
The only ones that are empty are the config.memory.json files, I checked this with my instance and the  example all-in-one app, which also suffers from the same issue out of the box.

railsnmet

unread,
Aug 22, 2012, 2:59:48 PM8/22/12
to picky...@googlegroups.com
If it helps any, I experience the same trouble when putting a request to /search on cocoapods.

railsnmet

unread,
Aug 23, 2012, 1:16:53 AM8/23/12
to picky...@googlegroups.com
Also, when I use the client I do get results so thats why I am confused as to why the allocations array is empty.

railsnmet

unread,
Aug 23, 2012, 4:32:58 AM8/23/12
to picky...@googlegroups.com
Never mind, I fixed it.

Picky / Florian Hanke

unread,
Aug 23, 2012, 8:55:59 AM8/23/12
to picky...@googlegroups.com
I'm glad to hear it. So everything is ok now?

railsnmet

unread,
Aug 23, 2012, 5:42:21 PM8/23/12
to picky...@googlegroups.com
Everything is fine now, but I was wondering if there was a way to add data from the source into the allocations array?

Picky / Florian Hanke

unread,
Aug 23, 2012, 5:51:59 PM8/23/12
to picky...@googlegroups.com
Sure! One idea is to see how the client example does it. That one uses this code (from the Picky client):

results.extend Picky::Convenience
results.populate_with Book do |book|
  book.to_s
end

to populate the ids in the allocations with the Book model instances (the "rendered" instances). If you don't want the rendered instances, just use populate_with without a block:

results.extend Picky::Convenience
results.populate_with Book

That inserts the model instances into the allocations.

For the Picky convenience code to be able to find the right instance, your model needs to implement a eg. Book.find ids method, something ActiveRecord or other ORM classes usually already do. (See the client example code to see how it does it without using ActiveRecord)

Don't hesitate to simply use a Hash { id => data } and add a find method to it. That's what I would do if I wasn't using ActiveRecord (we do that on cocoapods.org, for example – the code of which is on Github).

I hope this helps. Cheers!

Picky / Florian Hanke

unread,
Aug 23, 2012, 5:53:26 PM8/23/12
to picky...@googlegroups.com
P.S: You might be wondering, why isn't Picky storing the data for me? Precisely because it is so simple, and I want the user to be free and flexible in how they do it. (If I did it in Picky, I'd simply add a Hash with id => data object).

railsnmet

unread,
Aug 23, 2012, 8:14:49 PM8/23/12
to picky...@googlegroups.com
I was mainly using the server example, so my final question would be if I wanted the allocations array to have pairs of ids instead of just the normal id, how would I go about doing that?

Picky / Florian Hanke

unread,
Aug 24, 2012, 3:39:29 AM8/24/12
to picky...@googlegroups.com
One idea is to do it like that (a complete working example, just run it in eg. TextMate):

require 'picky'
require 'picky-client'

index = Picky::Index.new :index do 
  category :name
end
data = Picky::Search.new index

class Name < Struct.new(:id, :name)
  
  @@names = {}
  
  def self.register id, name
    @@names[id] = new id, name
  end
  
  def self.find ids, options = {}
    return ids.map { |id| @@names[id] }
  end
  
end

index.add Name.register(1, 'hello world')
index.add Name.register(2, 'hello mars')

results = data.search 'hello'
results = results.to_hash

results.extend Picky::Convenience

# Instead of just a single name, we
# insert an array of a name and its id.
#
# (Silly example, but it illustrates how to do it)
#
results.populate_with Name do |name|
  [name.id, name]
end

# A list of the "rendered" names.
#
puts "The entries:"
p results.entries

# The results hash.
#
puts "The results hash:"
p results

Don't hesitate to look at Picky's client/server code, too :)

Cheers,
   Florian
Reply all
Reply to author
Forward
0 new messages