[Grok-dev] How do I look up a grok.Indexes utility so I can call methods on an index?

Sebastian Ware sebastian at urbantalk.se
Wed Aug 25 10:43:00 EDT 2010


Thanks! Don't know if I am doing it right, but this works:

  # I have performed a query using hurry.query and
  # I have a result set called "res". Now I...
  
  # 1 Get the catalog
  content_catalog = getUtility(ICatalog, 'content_index')

  # 2 Perform the sort using the ids in the resultset
  tmp = content_catalog['published'].sort(res.uids, limit=10)
  
  # 3 Create a list of objects using the uidutil in the result set
  objs = [res.uidutil.getObject(o) for o in tmp]

Only wish I had understood how this works a year ago... :)

Mvh Sebastian

25 aug 2010 kl. 15.38 skrev Souheil CHELFOUH:

> grok Indexes are used only at the application creation.
> What it does is :
> 
> - lookup for the catalog or create it if it's not found
> zope.catalog.interfaces.ICatalog
> 
> - List the indexes registered for the application (grok.site(...)) and
> create the indexes inside the catalog found or newly created
> 
> you need simply to query your catalog (created only if you have a
> grok.Indexes registered before you create the app) with :
> queryUtility(ICatalog)  (unnamed it is)
> 
> and work with the returned object
> 
> 2010/8/25 Sebastian Ware <sebastian at urbantalk.se>:
>> That is the route I have been trying, but I am at a loss as to what interface to look up the catalog with. I can't find what grok.Indexes implements. I have been trying different versions of:
>> 
>>   getUtility( ???, 'content_index')
>> 
>> Mvh Sebastian
>> 
>> 
>> 25 aug 2010 kl. 15.18 skrev Souheil CHELFOUH:
>> 
>>> the index is in the catalog itself.
>>> get the catalog, the stored values are the indexes which, themselves,
>>> store the values needed for the indexation.
>>> 
>>> 
>>> 2010/8/25 Sebastian Ware <sebastian at urbantalk.se>:
>>>> I have a value index from zc.catalog and I want to use the implemented sort method. However, I don't understand how to look up the actual index. The index is defined as:
>>>> 
>>>> class SitePublishCatalog(grok.Indexes):
>>>>    grok.site(Testvalueindex)
>>>>    grok.context(IPublished)
>>>>    grok.name('content_index')
>>>> 
>>>>    published = Value()
>>>> 
>>>> I want to be able to do call index.sort(res) on my results that come from hurry.query, in the way explained in the zc.catalog doctest.
>>>> 
>>>> """
>>>> Sorting
>>>> -------
>>>> 
>>>> Value indexes supports sorting, just like zope.index.field.FieldIndex.
>>>> 
>>>>    >>> index.clear()
>>>> 
>>>>    >>> index.index_doc(1, 9)
>>>>    >>> index.index_doc(2, 8)
>>>>    >>> index.index_doc(3, 7)
>>>>    >>> index.index_doc(4, 6)
>>>>    >>> index.index_doc(5, 5)
>>>>    >>> index.index_doc(6, 4)
>>>>    >>> index.index_doc(7, 3)
>>>>    >>> index.index_doc(8, 2)
>>>>    >>> index.index_doc(9, 1)
>>>> 
>>>>    >>> list(index.sort([4, 2, 9, 7, 3, 1, 5]))
>>>>    [9, 7, 5, 4, 3, 2, 1]
>>>> """
>>>> 
>>>> Mvh Sebastian
>>>> 
>>>> 
>>>> 
>>>> _______________________________________________
>>>> Grok-dev mailing list
>>>> Grok-dev at zope.org
>>>> https://mail.zope.org/mailman/listinfo/grok-dev
>>>> 
>> 
>> 



More information about the Grok-dev mailing list