検索アダプタ

カスタムの検索アダプタを作成したい場合にには、 BaseSearch クラスのサブクラスを作成する必要があります。その後、新しいクラスのインスタンスを作成し、 WebSupport オブジェクトのを作る時に、 search キーワード引数で渡します:

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

カスタムの検索アダプタを作る際のより詳しい情報は、これから説明する BaseSearch クラスのドキュメントを参照してください。

class sphinxcontrib.websupport.search.BaseSearch[ソース]

検索アダプタのインタフェースを定義しています。

バージョン 1.6 で変更: BaseSearch クラスは sphinx.websupport.search からsphinxcontrib.websupport.search へ移動しました。

メソッド

これらのメソッドがBaseSearchクラスに定義されています。いくつかのメソッドはオーバーライドする必要はありませんが、サブクラスでオーバーライドしなければならないもの(add_document() , 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.