[Xapian-tickets] [Xapian] #503: Add Python PostingSource example from Xappy to docs

Xapian nobody at xapian.org
Wed Nov 17 06:32:13 GMT 2010


#503: Add Python PostingSource example from Xappy to docs
--------------------------------------+-------------------------------------
 Reporter:  jcassee                   |       Owner:  richard
     Type:  enhancement               |      Status:  new    
 Priority:  normal                    |   Milestone:  1.2.4  
Component:  Xapian-bindings (Python)  |     Version:  1.2.2  
 Severity:  normal                    |    Keywords:         
Blockedby:                            |    Platform:  All    
 Blocking:                            |  
--------------------------------------+-------------------------------------

Old description:

> The Xappy source code contains a perfect example of a
> [http://code.google.com/p/xappy/source/browse/trunk/xappy/searchconnection.py?r=620#1591
> weight-only (non-filtering) PostingSource written in Python]. This would
> be a good addition to the [source:trunk/xapian-
> core/docs/postingsource.rstpostingsource docs]. I have slightly edited
> the original.
>
> {{{
> class ExternalWeightPostingSource(xapian.PostingSource):
>     """
>     A Xapian posting source reading from an ExternalWeightSource.
>     """
>     def __init__(self, xapdb, wtsource):
>         xapian.PostingSource.__init__(self)
>         self.xapdb = xapdb
>         self.wtsource = wtsource
>
>     def init(self, xapdb):
>         self.alldocs = xapdb.postlist('')
>
>     def reset(self, xapdb):
>         # backwards compatibility
>         self.init(xapdb)
>
>     def get_termfreq_min(self): return 0
>     def get_termfreq_est(self): return self.xapdb.get_doccount()
>     def get_termfreq_max(self): return self.xapdb.get_doccount()
>
>     def next(self, minweight):
>         try:
>             self.current = self.alldocs.next()
>         except StopIteration:
>             self.current = None
>
>     def skip_to(self, docid, minweight):
>         try:
>             self.current = self.alldocs.skip_to(docid)
>         except StopIteration:
>             self.current = None
>
>     def at_end(self):
>         return self.current is None
>
>     def get_docid(self):
>         return self.current.docid
>
>     def get_maxweight(self):
>         return self.wtsource.get_maxweight()
>
>     def get_weight(self):
>         xapdoc = self.xapdb.get_document(self.current.docid)
>         doc = ProcessedDocument(self.conn._field_mappings, xapdoc)
>         return self.wtsource.get_weight(doc)
> }}}

New description:

 The Xappy source code contains a perfect example of a
 [http://code.google.com/p/xappy/source/browse/trunk/xappy/searchconnection.py?r=620#1591
 weight-only (non-filtering) PostingSource written in Python]. This would
 be a good addition to the [source:trunk/xapian-core/docs/postingsource.rst
 postingsource docs]. I have slightly edited the original.

 {{{
 class ExternalWeightPostingSource(xapian.PostingSource):
     """
     A Xapian posting source returning weights from an external source.
     """
     def __init__(self, db, wtsource):
         xapian.PostingSource.__init__(self)
         self.db = db
         self.wtsource = wtsource

     def init(self, db):
         self.alldocs = db.postlist('')

     def get_termfreq_min(self): return 0
     def get_termfreq_est(self): return self.db.get_doccount()
     def get_termfreq_max(self): return self.db.get_doccount()

     def next(self, minweight):
         try:
             self.current = self.alldocs.next()
         except StopIteration:
             self.current = None

     def skip_to(self, docid, minweight):
         try:
             self.current = self.alldocs.skip_to(docid)
         except StopIteration:
             self.current = None

     def at_end(self):
         return self.current is None

     def get_docid(self):
         return self.current.docid

     def get_maxweight(self):
         return self.wtsource.get_maxweight()

     def get_weight(self):
         doc = self.db.get_document(self.current.docid)
         return self.wtsource.get_weight(doc)
 }}}

--

Comment(by olly):

 I've made some further edits - fixing a typo in the wiki markup, removing
 the reset() method, renaming xapdb to db, and removing the
 !ProcessedDocument reference (which I think must be a xappyism).

 Richard said on IRC he'd like to have this actually tested (I think he
 means dynamically) so that the docs don't have an incorrect example.

-- 
Ticket URL: <http://trac.xapian.org/ticket/503#comment:6>
Xapian <http://xapian.org/>
Xapian



More information about the Xapian-tickets mailing list