搜索适配器

To create a custom search adapter you will need to subclass the BaseSearch class. Then create an instance of the new class and pass that as the search keyword argument when you create the WebSupport object:

support = WebSupport(srcdir=srcdir,
                     builddir=builddir,
                     search=MySearch())

有关创建自定义搜索适配器的更多信息,请参见下方的:class:BaseSearch 类的文档。

class sphinxcontrib.websupport.search.BaseSearch[源代码]

定义用于搜索适配器的接口。

在 1.6 版本发生变更: BaseSearch类从sphinx.websupport.search移动到sphinxcontri.websupport。

方法

以下方法是在BaseSearch类中定义的。有些方法不需要重写,但有些(add_document() 和:meth:~BaseSearch.handle_query)必须在子类中重写。要获得一个工作示例,请查看用于whoosh的内置适配器。

BaseSearch.init_indexing(changed=[])[源代码]

Called by the builder to initialize the search indexer. changed is a list of pagenames that will be reindexed. You may want to remove these from the search index before indexing begins.

参数:

changed – a list of pagenames that will be re-indexed

BaseSearch.finish_indexing()[源代码]

Called by the builder when writing has been completed. Use this to perform any finalization or cleanup actions after indexing is complete.

BaseSearch.feed(pagename, filename, title, doctree)[源代码]

Called by the builder to add a doctree to the index. Converts the doctree to text and passes it to add_document(). You probably won’t want to override this unless you need access to the doctree. Override add_document() instead.

参数:
  • pagename – the name of the page to be indexed

  • filename – the name of the original source file

  • title – the title of the page to be indexed

  • doctree – is the docutils doctree representation of the page

BaseSearch.add_document(pagename, filename, title, text)[源代码]

Called by feed() to add a document to the search index. This method should should do everything necessary to add a single document to the search index.

pagename is name of the page being indexed. It is the combination of the source files relative path and filename, minus the extension. For example, if the source file is “ext/builders.rst”, the pagename would be “ext/builders”. This will need to be returned with search results when processing a query.

参数:
  • pagename – the name of the page being indexed

  • filename – the name of the original source file

  • title – the page’s title

  • text – the full text of the page

BaseSearch.query(q)[源代码]

Called by the web support api to get search results. This method compiles the regular expression to be used when extracting context, then calls handle_query(). You won’t want to override this unless you don’t want to use the included extract_context() method. Override handle_query() instead.

参数:

q – the search query string.

BaseSearch.handle_query(q)[源代码]

Called by query() to retrieve search results for a search query q. This should return an iterable containing tuples of the following format:

(<path>, <title>, <context>)

path and title are the same values that were passed to add_document(), and context should be a short text snippet of the text surrounding the search query in the document.

The extract_context() method is provided as a simple way to create the context.

参数:

q – the search query

BaseSearch.extract_context(text, length=240)[源代码]

Extract the context for the search query from the document’s full text.

参数:
  • text – the full text of the document to create the context for

  • length – the length of the context snippet to return.