• whoosh----索引|搜索文本类库


    先了解基本概念和步骤

    Quick Start

       Whoosh是一个索引文本和搜索文本的类库,他可以为你提供搜索文本的服务,比如如果你在创建一个博客的软件,你可以用whoosh为它添加添加一个搜索功能以便用户来搜索博客的入口

    下面是一个简短的例子:

    >>>from whoosh.index importcreate_in

    >>>from whoosh.fields import *

    >>>schema = Schema(title =TEXT(stored = True),path = ID(stored=True),content=TEXT)

    >>>ix =create_in("indexer",schema)(这里的“indexer”实际上是一个目录,因此按照这个步骤来会出错,你得先创建目录,译者注)

    >>>writer = ix.writer()

    >>>writer.add_document(title=u"Firstdocument",path=u"/a",

                        content = u"this isthe first document we've add!")

    >>>writer.add_document(title=u"Second document", path=u"/b",

                            ...                     content=u"The secondone is even more interesting!")

    >>> writer.commit()

    >>> from whoosh.qparser importQueryParser

    >>> with ix.searcher() assearcher:

       query = QueryParser("content",ix.schema).parse("first")

       results = searcher.search(query)

       results[0]

    {"title": u"Firstdocument", "path": u"/a"}

    Index和Schema对象

    [H1] 

     开始使用whoosh之前,你需要一个index对象,在你第一次创建index对象时你必须定义一个Schema对象,Schema对象列出了 Index的所有域。一个域就是Index对象里面每个document的一个信息,比如他的题目或者他的内容。一个域能够被索引(就是能被搜索到)或者被存储(就是得到索引之后的结果,这对于标题之类的索引非常有用)

    下面这个schema对象有两个域,“title”和“content”

    from whoosh.fields import Schema,TEXT

    schema = Schema(title=TEXT,content=TEXT)

    当你创建index的时候你创建一次schema对象就够了,schema是序列化并且和index存储的。

    当你创建索引对象的时候,需要用到关键字参数来将域名和类型建立映射关系,域的名字和类型决定了你在索引什么和什么事可搜索的。whoosh有很多非常有用的预定义域类型,你可以非常容易的创建他们。如下:

    whoosh.fields.ID

    whoosh.fileds.STROED

    whoosh.fileds.KEYWORD

    whoosh.fields.TEXT

    whoosh。fields.NUMERIC

    whoosh.fields.BOOLEAN

    whoosh.fields.DATETIME

    whoosh.fields.NGRAM和whoosh.fields.NGRAMWORDS

    这种类型把文本分成n-grams(以后详细介绍)

    (为了简便,你可以不给域类型传递任何参数,你可以仅仅给出类型名字whoosh会为你创造类的实例)

    from whoosh.fields importSchema,STROED,ID,KEYWORD,TEXT

    schema =Schema(title=TEXT(stored=True),content=TEXT,

                    path=ID(stored=True),tags=KEYWORD,icon=STROED)

    一旦你有了schema对象,你可以用create_in函数创建索引:

    import os.path

    from whoosh.fields import create_in

    if not os.path.exists("index"):

       os.mkdir("index")

        ix= create_in("index",schema)

    (这创造了一个存储对象来包含索引,存储对象代表存储索引的媒介,通常是文件存储,也就是存在目录的一系列文件)

    在你创建索引之后,你可以通过open_dir函数方便地打开它

    from whoosh.index import open_dir

    ix = open_dir("index")

    IndexWriter对象

    好了我们有了索引对象现在我们可以添加文本。索引对象的writer()方法返回一个能够让你给索引添加文本的IndexWriter对象,IndexWriter对象的add_document(**kwargs)方法接受关键字参数:

    writer = ix.writer()

    writer.add_document(title=u"Mydocument", content=u"This is my document!",

                       path=u"/a",tags=u"first short",icon=u"/icons/star.png")

    writer.add_document(title=u"Secondtry", content=u"This is the second example.",                   path=u"/b",tags=u"second short", icon=u"/icons/sheep.png")

    writer.add_document(title=u"Thirdtime's the charm", content=u"Examples are many."                 path=u"/c",tags=u"short", icon=u"/icons/book.png")

    writer.commit()

    两个重点:

    1.你不必要把每一个域都填满,whoosh不关系你是否漏掉某个域

    2.被索引的文本域必须是unicode值,被存储但是不被索引的域(STROED域类型)可以是任何和序列化的对象

    如果你需要一个既要被索引又要被存储的文本域,你可以索引一个unicode值但是存储一个不同的对象(某些时候非常有用)

    writer.add_document(title=u"Title tobe indexed",_stored_title=u"Stored title")

    使用commit()方法让IndexWriter对象将添加的文本保存到索引

    writer.commit()

    Searcher对象

    在搜索文本之前,我们需要一个Searcher对象

    searcher = ix.searcher()

    推荐用with表达式来打开搜索对象以便searcher对象能够自动关闭(searcher对象代表打开的一系列文件,如果你不显式的关闭他们,系统会慢慢回收他们以至于有时候会用尽文件句柄)

    with in.searcher() as searcher:

        ...

    这等价于:

    try:

       searcher = ix.searcher()

       ...

    finally:

        searcher.close()

    Searcher对象的search方法使用一个Query对象,你可以直接构造query对象或者用一个query parser来得到一个查询语句

    #Construct query objects directly

    from whoosh.query import *

    myquery =And([Term("content",u"apple"),Term("content","bear")])

    用parser得到一个查询语句,你可以使用默认的qparser模块里面的query parser。QueryParser构造器的第一个参数是默认的搜索域,这通常是“正文文本”域第二个可选参数是用来理解如何parse这个域的schema对象:

    #Parse a query string

    from whoosh.qparser import QueryParser

    parser =QueryParser("content",ix.schema)

    myquery = parser.parse(querystring)

    一旦你有了Searcher和query对象,你就可以使用Searcher对象的search()方法进行查询并且得到一个结果对象

    >>>results =searcher.search(myquery)

    >>>print len(results)

    1

    >>>print results[0]

    {"title": "Second try","path": /b,"icon":"/icon/sheep.png"}

    默认的QueryParser实现了一种类似于lucene的查询语言,他允许你使用连接词AND和OR以及排除词NOT和组合词together来搜索,他默认使用AND将从句连结起来(因此默认你所有的查询词必须都在问当中出现)

    >>>print(parser.parse(u"render shade animate"))

    And([Term("content","render"), Term("content", "shade"),Term("content", "animate")])

    >>>print(parser.parse(u"render OR (title:shade keyword:animate)"))

    Or([Term("content","render"), And([Term("title", "shade"),Term("keyword", "animate")])])

    >>>print(parser.parse(u"rend*"))

    Prefix("content","rend")

    Whoosh处理结果包含额外的特征,例如:

    1.按照结果索引域的值排序,而不是按照相关度

    2.高亮显示原文档中的搜索词

    3.扩大查询词给予文档中找到的少数值

    4.分页显示(例如显示1-20条结果,第一到第四页的结果)

    ===================】================================】

    Searcher

    class Searcher(object): 由index对象得到,接收query对象,返回res对象。

    Wraps an:class:`~whoosh.reading.IndexReader` object and provides methods for searchingthe index. # 本质上是对 index reader的封装。

    def __init__(self,reader, weighting=scoring.BM25F, closereader=True,

            fromindex=None, parent=None):

    """

    :param reader:An :class:`~whoosh.reading.IndexReader` object for

    the index to search.

    :param weighting:A :class:`whoosh.scoring.Weighting` object to use to

    score found documents.(评分模型传入到searcher里面

    :param closereader:Whether the underlying reader will be closed when

    the searcher is closed.

    :param fromindex:An optional reference to the index of the underlying

    reader. This is required for:meth:`Searcher.up_to_date` and :meth:`Searcher.refresh` to work.

    """

    下面讲解里面的函数

    ------------------------------------------------------------------------------

    1, 首先是入口函数def search(self, q, **kwargs):,首先调用c =self.collector(**kwargs),对文档进行过滤,self.search_with_collector(q,c),再在过滤后的文档集合c上处理查询语句q。然后转移到collector.prepare(self, q, context),和collector.run(),由collector对象完成结果对象的获取入口。Contex字典里面包含如评分函数之类的参数。

    2, 里面还封装了如读取文档某个field的总长度的函数,很多和q无关的但是可以读取index的信息。如:doc_count_all,field_length,avg_field_length。如对特定文档信息的查询:defdocument_numbers(self,**kw): """Returns a generator of the document numbers fordocuments matching the given keyword arguments, where the keyword keys arefield names and the values are terms that must appear in the field,返回符合要求的文档id集合。def document(self, **kw):返回符合要求的一个文档。def idf(self, fieldname, text):计算单词的idf,计算是通过调用idf = self.weighting.idf(self, fieldname, text)不过会缓存结果cache[term] = idf

    3, 因为index在不断的更新,所以search也需要跟新,和up_to_date和refresh函数都有关,所以会造成subsearchers,相应的每个subsearcher都有一个offset,表示docnum的偏移量。

    4, def postings(self, fieldname, text, weighting=None, qf=1):是一个对底层ixreader.postings,另外还会考虑subsearchers的因素和设置评分函数,返回Matcher或者MultiMatcher。这里接收的是text,不是query对象。具体可以看ixreader.postings函数

    Result对象:是结果对象

    Hit对象:是result里面的一条记录

    Query解析:qparser解析字符串,得到query对象。或者自己直接构建query对象

    不同的parser适应不同的场景,不同的parser解析和生成不同的query,就像插件一样进行添加,就可以获取相应的功能。

    phrase和sequence是不兼容的,其它的是兼容的。

    # 这个只需要whoosh.qparser.FuzzyTermPlugin()模糊匹配就可以了

    queryParser.add_plugin(whoosh.qparser.FuzzyTermPlugin())

    Q = '"BM25F function"'

    Q = '"BM25F function"~2' #使用SequencePlugin后,他就不是phrase,而是sequence

    Q = '"BM25F~2 function"~2'

    # 这个需要whoosh.qparser.SequencePlugin()来处理复杂的逻辑,phrase里面还包含模糊匹配。不然只能处理简单的phrase

    queryParser.add_plugin(whoosh.qparser.FuzzyTermPlugin())  # 这个可别忘了

    queryParser.remove_plugin_class(whoosh.qparser.PhrasePlugin)

    queryParser.add_plugin(whoosh.qparser.SequencePlugin())  #phrase 和sequence是不兼容的,所以必须去掉前者,才能发挥sequence的复杂功能

    Query类:query像是一个树或者python的list,层层封装包含

    class Term(qcore.Query):

    Matches documents containing the given term(fieldname+text pair).

    >>> Term("content",u"render")

    Term是非常基本的query对象,只包含一个单词,以及对应要检索的field。

    Range包含NumericRange和TermRange,属于模糊匹配。如:

    >>> TermRange("id",u"apple", u"pear" )# to 'apple' and less than or equal to'pear'.

    >>> nr =NumericRange("number", 10, 5925) # Match numbers from 10 to 5925 inthe "number" field.

    下面讲解里面的子类

    ------------------------------------------------------------------------------

    WrappingQuery用来给一个query对象绑定一个权重。

    CompoundQuery用来封装query之间的逻辑关系,如or,and,将简单的query封装成复杂的query。如Sequence和Ordered都继承自CompoundQuery。

    Span是一个计算单词位置信息的类,SpanQuery是一个可以利用span信息的query类。来计算两个单词之间的距离是否满足一定的窗口。

    Sequence和Phrase都是基于位置信息的query,但是内部都会

    # Create the equivalent SpanNear2 queryfrom the terms

    q = SpanNear2(terms, slop=self.slop,ordered=True, mindist=1)

    # Get the matcher

    m = q.matcher(searcher, context)

    将查询语句变成spanquery对象,得到匹配的doc,然后再进行评分。

    Spanquery的规则只能应用到match中,不能体现在score中。

    Collector:收集文档结果,并返回results对象,可以cache很多中间结果信息

    This module contains "collector"objects. Collectors provide a way to gather"raw" results from a :class:`whoosh.matching.Matcher` object,implement sorting, filtering, collation,etc., and produce a :class:`whoosh.searching.Results` object

    下面讲解里面的子类

    ------------------------------------------------------------------------------

    TopCollector:Returns the top N matching results sorted by score, usingblock-quality optimizations to skip blocks of documents that can't contributeto the top N。会直接排除掉不能排进前n名的文档。

    FilterCollector:A collector that lets you allow and/or restrict certain documentnumbers in the results。

    TimeLimitCollector:A collector that raises a :class:`TimeLimit` exception if the searchdoes not complete within a certain number of second。

    TermsCollector:A collector that remembers which terms appeared in which termsappeared in each matched document。

    SortingCollector:Returns all matching results sorted by a:class:`whoosh.sorting.Facet` object。根据特定的规则对结果进行排序。

    Here's an example of a simple collectorthat instead of remembering the matched documents just counts up the number ofmatches::

       class CountingCollector(Collector):

           def prepare(self, top_searcher, q, context):

               # Always call super method in prepare

               Collector.prepare(self, top_searcher, q, context)

               self.count = 0

           def collect(self, sub_docnum):

               self.count += 1

        c= CountingCollector()

        mysearcher.search_with_collector(myquery, c)

       print(c.count)

    简单的计数collector的例子。可以看出,matcher负责找到结果,collector负责进行排序,过滤,是对结果再处理

    Collector也会嵌套,比如普通collector作为TimeLimitCollector的self.child属性来存在。TimeLimitCollector封装了对普通collector的时间要求限制。

    前面search讲了,search会跳转到collector的run函数上面。下面可以看到最终还是跳转到query对象的match函数上,进而任务落到query返回的matcher身上

       def run(self):

          for subsearcher, offset in self.top_searcher.leaf_searchers():

             self.set_subsearcher(subsearcher,offset)

             self.collect_matches()

       def set_subsearcher(self,subsearcher, offset):

           self.subsearcher = subsearcher

           self.offset = offset

           self.matcher = self.q.matcher(subsearcher,self.context)

       def collect_matches(self):

           collect = self.collect

           for sub_docnum in self.matches(): #matches会获取_step_through_matches中的id

                collect(sub_docnum)

    def _step_through_matches(self):

           matcher = self.matcher

           while matcher.is_active():

               yield matcher.id()

               matcher.next()

    --------------------------------------------------------------------------

    以Collector的子类ScoredCollector,以及ScoredCollector的子类TopCollector为例:

    上面的分析可以看到通过yield协程技术,返回一个迭代器对象。迭代器一次返回docnum,然后调用collect来处理docnum。而collect函数首先调用matcher的score函数计算得到文档的评分,在调用final_fn函数计算文档的评分,最后在根据collector的不同调用_collect函数做不同的处理。如TopCollector就会根据分数score的大小是否排名前k,来觉得是否保存该文档docnum,如果是UnlimitedCollector就会直接保存docnum。(注意:可以看到也是协程技术,所以相当于matcher每产生一个docnum,它就接着计算对应文档的score,所以可以看到函数self.matcher.score()不需要传入任何参数,因为matcher里面还保存这它返回的docnum的上下文)。因为UnlimitedCollector和TopCollector都是ScoredCollector的子类,所以先用collect实现它们共有的部分,在使用_collect实现特例的部分,也实现代码之间分离。所以任务都转移到matcher.id如何产生docnum,以及matcher.score如何计算分数。接下来一段会先分析matcher是如何来的。

       def collect(self, sub_docnum):

           global_docnum = self.offset + sub_docnum

           score = self.matcher.score()

           if self.final_fn:

               score =self.final_fn(self.top_searcher, global_docnum, score)

           return self._collect(global_docnum, score)

       def _collect(self, global_docnum, score):

           items = self.items

           self.total += 1

           if len(items) < self.limit:

                heappush(items, (score, 0 -global_docnum))

               return 0 - score

           elif score > items[0][0]:

               heapreplace(items, (score, 0 - global_docnum))

               self.minscore = items[0][0]

               return 0 - score

           else:

               return 0

     

    Query类:

    以query的子类sequence为例:

       def matcher(self, searcher,context=None):

           subs = self.subqueries

           if not subs:

               return matching.NullMatcher()

           if len(subs) == 1:

               m = subs[0].matcher(searcher, context)

           else:

               m = self._matcher(subs,searcher, context)

           return m

    def _matcher(self,subs, searcher, context):

           from whoosh.query.spans import SpanNear

           context = context.set(needs_current=True)

           m = self._tree_matcher(subs,SpanNear.SpanNearMatcher, searcher,

                                   context, None,slop=self.slop,

                                  ordered=self.ordered)

           return m

       def _tree_matcher(self,subs,mcls, searcher, context, q_weight_fn,**kwargs):

           subms = [q.matcher(searcher, context) forq in subs]

           if len(subms) == 1:

               m = subms[0]

           elif q_weight_fn is None:

               m = make_binary_tree(mcls, subms, **kwargs)

           else:

               w_subms = [(q_weight_fn(q), m) for q, m in zip(subs, subms)]

               m = make_weighted_tree(mcls, w_subms, **kwargs)

           # If this query had a boost, add a wrapping matcher to apply the boost

           if self.boost != 1.0:

               m = matching.WrappingMatcher(m, self.boost)

           return m

    可以看到,标红的代码部分构成递归循环,根据当前的查询语句q和searcher和context,最终返回一个树状结构的matcher。这里也可以看到query是如何被解析为一个树状结构的,也可以看到WrappingMatcher是对多个matcher封装成一个复合matcher。和collector得部分结合起来,最终进入到matcher.id()函数中去。(需要注意的是:构建make_weighted_tree的时候是需要SpanNearMatcher的,这为实现查询语句窗口提供了基础)

    ---------------------------------------------------------------------------

    当查询语句是复合查询语句的时候,会存在subqueries。否则调用matcher后生成单一的matcher,而不是树状结构的,也就是生成树状结构matchers里面的叶子节点。以query的子类Term为例:

       def matcher(self, searcher, context=None):

           fieldname = self.fieldname

           text = self.text

           if fieldname not in searcher.schema:

               return matching.NullMatcher()

           field = searcher.schema[fieldname]

           try:

               text = field.to_bytes(text)

           except ValueError:

               return matching.NullMatcher()

           if (self.fieldname, text) in searcher.reader():

               if context is None:

                    w = searcher.weighting

               else:

                    w = context.weighting

               m =searcher.postings(self.fieldname, text, weighting=w)

               if self.minquality:

                   m.set_min_quality(self.minquality)

               if self.boost != 1.0:

                    m = matching.WrappingMatcher(m,boost=self.boost)

               return m

           else:

               return matching.NullMatcher()

    可以看到对于包含一个term的查询语句matcher函数,最终会调用searcher.postings来生成最终的matcher,单个matcher会转移到searcher.posting,如前面所说,进而转移到ixreader.postings函数两点注意:一个是评分函数weighting,另一个是单词的权重boost都在这里进行设置后者传入

    ---------------------------------------------------------------------------

    查看whoosh.util 中的make_binary_tree和make_weighted_tree

    def make_binary_tree(fn, args, **kwargs):

       count = len(args)

       if not count:

           raise ValueError("Called make_binary_tree with empty list")

       elif count == 1:

           return args[0]

       half = count // 2

       return fn(make_binary_tree(fn, args[:half], **kwargs),

                  make_binary_tree(fn, args[half:],**kwargs), **kwargs)

    内容很简单,就是利用fn递归构建一个课树

    def make_weighted_tree(fn, ls, **kwargs):

       """Takes a function/class that takes two positionalarguments and a list of

       (weight, argument) tuples and returns a huffman-like weighted tree of

       results/instances.

       """

       if not ls:

           raise ValueError("Called make_weighted_tree with empty list")

       ls.sort()

       while len(ls) > 1:

           a = ls.pop(0)

           b = ls.pop(0)

           insort(ls, (a[0] + b[0], fn(a[1], b[1])))

       return ls[0][1]

    是构建一个哈夫曼树,那么权重调整了构建树方式的不同,权重应该是为了加快计算速度的一个指标。总而言之,这一步只是对子查询语句的返回的matchers构建一个树状的matcher。然后再用fn进行封装,也就是SpanNearMatcher,它返回一个SpanNearMatcher对象,但底层都还是需要归结到ixreader.postings函数来生成matcher。到现在为止,从query的解析,传入searcher,调用collector,最后又返回到searcher.postings,也就是查询的过程最终都归结于searcher.postings对ixreader.postings得封装,也就是给定field和查询text对索引进行查询,返回匹配的docnum。Collector和query对象只不过是处理了查询语句中的位置信息、单词权重boost信息、与或非等的逻辑关系。接下来进入ixreader.postings。

    whoosh.reading.IndexReader看名字也知道是访问index的接口:

    里面包含了很多访问index属性的方法,如:

    all_terms(self): # Yields (fieldname, text) tuples for every term in the index.返回所有的(域名,单词)对。

    all_doc_ids(self):# Returns an iterator of all (undeleted) document IDs in the reader.Matcher返回一个迭代器,一次产出所有的文档id

    frequency(self,fieldname, text):# Returns the total number ofinstances of the given term in the collection.返回text在特定fieldname里面的频率。

    field_length(self,fieldname):# Returns the total number of terms inthe given field.返回某个field的总长度。

    doc_field_length(self,docnum, fieldname, default=0):# Returns the numberof terms in the given field in the given document.返回某个文档的field的长度。

    most_frequent_terms(self,fieldname, number=5, prefix=''): Returns the top'number' most frequent terms in the given field as a list of (frequency, text)tuples.返回出现次数最多前k个单词。

    ------------------------------------------------------------------------

    以IndexReader 的子类classSegmentReader(IndexReader)为例:

    defpostings(self, fieldname, text, scorer=None):

           from whoosh.matching.wrappers import FilterMatcher

           if self.is_closed:

               raise ReaderClosed

           if fieldname not in self.schema:

               raise TermNotFound("No  field%r" % fieldname)

           text = self._text_to_bytes(fieldname, text)

           format_ = self.schema[fieldname].format

           matcher = self._terms.matcher(fieldname, text,format_, scorer=scorer)

           deleted = frozenset(self._perdoc.deleted_docs())

            if deleted:

               matcher = FilterMatcher(matcher, deleted, exclude=True)

           return matcher

    好吧,转移到了_terms的matcher函数上了,从名字也可以看出,这是针对单个单词的matcher。而_terms是self._terms= self._codec.terms_reader(self._storage, segment)的实例,self._codec= codec if codec else segment.codec()等等,先到此为止,先来看看matcher类

    Matcher类:处理posting lists。最基础的第一个matcher对象由如:whoosh.filedb.filepostings.FilePostingReader给出。(Posting list应该是一个term的倒排索引上文档组成的链表)

    This module contains "matcher"classes. Matchers deal with posting lists. The most basic matcher, which readsthe list of postings for a term, will be provided by the backend implementation(for example, :class:`whoosh.filedb.filepostings.FilePostingReader`).

    -------------------------------------------------------------------------

    Matcher实现了对postinglists的封装访问,以Matcher的子类UnionMatcher为例,分析其中的函数:

    def replace(self, minquality=0):返回化简版本的matcher,比如UnionMatcher执行的是“或”的语义操作,如果其中一个子matcher失效了,那么就只剩下另一个了,这个时候将另一个返回即可。如果是IntersectionMatcher执行的是“与”的语义操作,如果一个子matcher失效了,那么久返回一个NullMatcherClass,因为“与”操作,必须两个matcher中都存在才可以,一个没有了,就说明整个都没有了。

    def id(self):

           _id = self._id

           if _id is not None:

               return _id

           a = self.a

           b = self.b

           if not a.is_active():

               _id = b.id()

           elif not b.is_active():

               _id = a.id()

           else:

               _id = min(a.id(), b.id())

           self._id = _id

           return _id

           可以看到,因为执行的是“或”语义,返回每一个子matcher遇到的posting,按照从小到大的顺序,先返回其中比较小的。

    def next(self):从当前posting移动下一个posting。因为是“或”操作,所以是移动id比较小的matcher。如果是IntersectionMatcher,那么肯定是移动到两个matcher共有的且下一个最小的posting上去。

       def score(self):

           a = self.a

           b = self.b

           if not a.is_active():

               return b.score()

           if not b.is_active():

               return a.score()

           id_a = a.id()

           id_b = b.id()

           if id_a < id_b:

               return a.score()

           elif id_b < id_a:

               return b.score()

           else:

               return (a.score() + b.score())

           可以看到返回两个子matcher的评分的和。进一步从LeafMatcher的score如下所示,可以看到对于叶子节点的matcher,返回的是scorer的score函数,参数是当前自己matcher。

    def score(self):

           return self.scorer.score(self)

           也可以看出两个子matcher在计算分数的时候是相互分开的,这个是很致命的比如要统计查询语句Q = '"BM25Ffunction"~2'在文档中出现的次数来作为评分,这是不可能的,因为单词BM25F形成LeafMatcher的实例lm1,单词function形成LeafMatcher的实例lm2,lm1和lm2是分开计算分数的,所以只会得到BM25F在文档中的频率加上function在文档中的频率之和来作为分数,而不是BM25F function在窗口2内的在文档中的频率。如文档:“BM25F BM25F BM25F termfunction function”。正常的算出来的分数应该是1,而whoosh算出来的分数是5

       def spans(self):

           if not self.a.is_active():

               return self.b.spans()

           if not self.b.is_active():

                return self.a.spans()

           id_a = self.a.id()

           id_b = self.b.id()

           if id_a < id_b:

               return self.a.spans()

           elif id_b < id_a:

               return self.b.spans()

           else:

               return sorted(set(self.a.spans()) | set(self.b.spans()))

           返回span对象这个很重要,记录位置窗口信息。以后再详细讲。

    结合我们之前讲的部分,可以看到,有matcher从posting中得到文档id,并matcher调用scorer的score来计算分数,传递的参数是matcher自身

    Scoring:有两中类,评分模型和评分函数类

    以BM25F评分模型为例

    class BM25F(WeightingModel):

       def __init__(self, B=0.75, K1=1.2, **kwargs):

           self.B = B

           self.K1 = K1

           self._field_B = {}

           for k, v in iteritems(kwargs):

               if k.endswith("_B"):

                    fieldname = k[:-2]

                   self._field_B[fieldname] = v

       def supports_block_quality(self):

           return True

       def scorer(self, searcher, fieldname, text, qf=1):

           if not searcher.schema[fieldname].scorable:

               return WeightScorer.for_(searcher, fieldname, text)

           if fieldname in self._field_B:

               B = self._field_B[fieldname]

           else:

               B = self.B

           return BM25FScorer(searcher, fieldname, text, B, self.K1, qf=qf)

    class BM25FScorer(WeightLengthScorer):

       def __init__(self, searcher, fieldname, text, B, K1, qf=1):

           parent = searcher.get_parent()  #Returns self if no parent

           self.idf = parent.idf(fieldname, text)

           self.avgfl = parent.avg_field_length(fieldname) or 1

           self.B = B

           self.K1 = K1

           self.qf = qf

           self.setup(searcher, fieldname, text)

           defscore(self, matcher):

           return self._score(matcher.weight(), self.dfl(matcher.id()))

       def _score(self, weight, length):

           s = bm25(self.idf, weight, length, self.avgfl, self.B, self.K1)

           return s

    现来看如何得到searcher,searcher=ix.searcher(weighting= BM25F ())可以看到,首先在定义searcher的时候的时候,传入的是评分模型BM25F,这个时候可以设置B、K1等模型的自由参数。前面介绍过res= searcher.search(query,terms=True)最终都会回归到searcher.postings函数上。如下:

       def postings(self, fieldname, text, weighting=None, qf=1):

           weighting = weighting or self.weighting

           globalscorer = weighting.scorer(self, fieldname,text, qf=qf)

           if self.is_atomic():

               return self.ixreader.postings(fieldname, text,scorer=globalscorer)

           else:

               from whoosh.matching import MultiMatcher

               matchers = []

               docoffsets = []

               term = (fieldname, text)

               for subsearcher, offset in self.subsearchers:

                    r = subsearcher.reader()

                    if term in r:

                        scorer= weighting.scorer(subsearcher, fieldname, text, qf=qf)

                        m =r.postings(fieldname, text, scorer=scorer)

                        matchers.append(m)

                        docoffsets.append(offset)

               if not matchers:

                    raise TermNotFound(fieldname,text)

               return MultiMatcher(matchers, docoffsets, globalscorer)

    可以看到,searcher.postings会调用评分对象的scorer函数,传递进去searcher对象、检索的域fieldname和查询文本text。结合BM25F类中的scorer函数,将bm25的参数以及searcher生成并返回BM25Fscorer对象。这样就可以看到评分函数对象BM25Fscorer在评分的时候,可以接收到的信息有对index访问的searcher对象,被检索的域fieldname,检索查询单词文本text,自由参数B和K1,还有词频信息qf。另外matcher对象是在调用BM25Fscorer的score函数时传递进去的。

    总结一下:searcher是访问索引信息的接口,可以获取到域的长度、特定文档域的长度、单词的总词频等信息。Matcher是访问满足查询语句要求的postings(即文档)的信息的接口,可以获取当前文档的id,当前单词在文档中的span,当前单词的权重weight。

    Span:是实现窗口查询语句的基础,span本身就是两个数表示一个区间。如[2,6]表示文档第2个位置到第6个位置的跨度区间

    在需要位置信息的查询语句中都是需要span的,比如:查询在“text”域中,单词“whoosh”出现在单词“library”前面,而且它们之间的距离小于3。

           from whoosh import query, spans

           t1 = query.Term("text", "whoosh")

           t2 = query.Term("text", "library")

           q = spans.SpanNear2([t1, t2], slop=3, ordered=True)

    slop表示窗口插槽的大小,ordered表示单词之间是否有序,比如如果ordered=False,那么在text field中“whoosh and library”和“library and whoosh”都是合法的,都会被检索到。

    以Sequence类为例:

    class Sequence(compound.CompoundQuery):

        def _matcher(self, subs, searcher,context):

            from whoosh.query.spans import SpanNear

            context =context.set(needs_current=True)

            m = self._tree_matcher(subs,SpanNear.SpanNearMatcher, searcher,

                                  context, None, slop=self.slop,

                                  ordered=self.ordered)

            return m

    可以看到Sequence类的构造的树装matcher是SpanNear.SpanNearMatcher类的实例。接下来转移到SpanNearMatcher类中:

        class SpanNearMatcher(SpanWrappingMatcher):

            def __init__(self, a, b, slop=1,ordered=True, mindist=1):

                self.a = a

                self.b = b

                self.slop = slop

                self.ordered = ordered

                self.mindist = mindist

                isect =binary.IntersectionMatcher(a, b)

                super(SpanNear.SpanNearMatcher,self).__init__(isect)

                  def _find_next(self):

                   ifnot self.is_active():

                       return

                   child= self.child

                   r= False

                   spans= self._get_spans()

                   while child.is_active() and not spans:

                       r = child.next() or r

                       if not child.is_active():

                           return True

                       spans = self._get_spans()

                   self._spans= spans

                   returnr

               defnext(self):

                   self.child.next()

                   self._find_next()

            def _get_spans(self):

                slop = self.slop

                mindist = self.mindist

                ordered = self.ordered

                spans = set()

                bspans = self.b.spans()

                for aspan in self.a.spans():

                    for bspan in bspans:

                        if (bspan.end <aspan.start - slop

                            or (ordered andaspan.start > bspan.start)):

                            # B is too far in frontof A, or B is in front of A

                            # *at all* when orderedis True

                            continue

                        if bspan.start >aspan.end + slop:

                            # B is too far from A.Since spans are listed in

                            # start position order,we know that all spans after

                            # this one will also be too far.

                            break

                        # Check the distancebetween the spans

                        dist =aspan.distance_to(bspan)

                        if mindist <= dist <=slop:

                            spans.add(aspan.to(bspan))

                return sorted(spans)

    从SpanNearMatcher的初始化函数中,可以看到,它是本质上是对IntersectionMatcher类的一个封装,也就是单词之间执行的是“与”逻辑。要求文档同时包含所有的单词。然后将IntersectionMatcher设置为SpanNearMatcher的child属性(标红的代码部分)。也就是self.child来获取IntersectionMatcher对象。这个可以看SpanWrappingMatcher的构造函数。

    之前的分析,我们知道每次通过调用matcher的next函数来使得matcher从当前posting移动到下一个posting。由上面的代码可以看到(标绿的代码部分),先要调用self.child.next()函数,进行移动,因为child是IntersectionMatcher对象,执行的是“与”逻辑,也就是会移动下一个包含所有查询单词的posting上。但是接着又调用了_find_next函数。

    可以看到_find_next中主体是一个while循环,循环退出的条件是spans不为空,或者child.is_active()死掉了,也就是IntersectionMatcher对象找不到下一个符合条件的posting对象了。循环的主体是调用self.child.next()移动到下一个符合条件的posting上,并获取当前posting的spans,这个spans保存了当前posting(文档)中符合窗口条件的所有位置,如果spans为空,说明当前posting(文档)虽然包含了所有的查询单词,但是单词的位置不满足窗口条件。

    _get_spans可以看到是如何计算spans的,计算spans的过程,也是检查是否符合窗口条件的过程。可以看到对于两个单词的spans,先获取各自在文档中的spans,也就是在文档中出现位置的,起始和结束位置对,[起点,终点],然后依次计算a的span和b中的span之间的距离是否满足slop距离。a和b中的span都是从小打到按照顺序遍历的。

    -------------------------------------------------------------------------

    到此我们可以看到,whoosh是如何找到满足特定窗口条件查询语句的文档,但是span的应用仅限于查找文档。也就是matcher.next()和matcher.id()。当调用matcher.scorer对文档进行评分的时候,都是按照查找树的结构递归下去,每个单词单独计算评分,然后再进行汇总,也就是我们之前说的,无法计算“whoosh query~2”在对文档中出现的次数来作为文档的评分。因为spans信息跟就没有传送到评分模型类中。

    当然也不可以天真的认为match对象会传送给Scorer类,然后通过传递的matcher来获取span信息,因为传进去的matcher是这个单词的matcher,里面只包含了当前text在当前docnum中的spans信息,它的评分层次是基于单个单词,没有提供基于几个单词的,或者phrase或者sequence整体层次的评分接口。如果要实现的话,得修改SpanWrappingMatcher中的scorer函数

  • 相关阅读:
    【iOS】找工作的面试题集锦
    APP项目优化--启动速度优化篇
    【Swift】Timer定时器到底准不准确?
    leetcode刷题 495~
    leetcode刷题 464~
    leetcode刷题 441~
    leetcode刷题 420~
    leetcode刷题 396~
    leetcode刷题 373~
    leetcode刷题 307~
  • 原文地址:https://www.cnblogs.com/kiko0o0/p/8417141.html
Copyright © 2020-2023  润新知