我们很高兴宣布 GroupDocs.Search for Python v26.10 已于 2026 年 11 月 正式发布。这是该产品的首次发布,将 GroupDocs.Search 26.10 引擎的完整功能带给 Python 开发者。它能够在文档文件夹上构建并维护全文索引——包括 PDF、Word、Excel、PowerPoint、电子邮件、电子书、压缩包以及其他 80 多种格式——并使用高级查询类型进行搜索:关键字、短语、通配符、正则、模糊、布尔、分面以及日期范围搜索。此版本支持 Windows x64、Linux x64(glibc 2.27+)和 macOS 13+(Intel 与 Apple Silicon)上的 Python 3.5–3.14,并嵌入 .NET 10.0.14 运行时。

此版本的新功能

  • [Feature] GroupDocs.Search for Python via .NET 的首次发布 (SEARCHPYTHONNET-40)

公共 API 更改

这是首次发布,因此没有可升级的旧版本。API 遵循 GroupDocs.Search for .NET 26.10:类保留其 .NET 名称,方法、属性和参数采用 snake_case(Index.Search 为 index.search,SearchResult.DocumentCount 为 result.document_count),枚举成员使用大写(OutputFormat.HTML)。为类不存在的属性赋值会抛出 AttributeError 并提供 “Did you mean” 提示,而不是创建引擎永远看不到的仅 Python 使用的属性。

模块

  • groupdocs.search: Index, IndexSettings, IndexRepository, IndexUpdater, SearchQuery, Extractor, License, Metered, GroupDocsSearchException, LicenseError, CancellationTokenSource.
  • groupdocs.search.options: IndexingOptions, SearchOptions, FuzzySearchOptions, SimilarityLevel, UpdateOptions, HighlightOptions, OutputFormat, SeparateProcessOptions, ImageSearchOptions.
  • groupdocs.search.results: SearchResult, FoundDocument, FoundDocumentField, DocumentInfo, FileType, FormatFamily.
  • groupdocs.search.highlighters: DocumentHighlighter, FragmentHighlighter.
  • groupdocs.search.common: FileOutputAdapter, StringOutputAdapter, StreamOutputAdapter, Document, DocumentField, ExtractedData, FileLogger, AttributeChangeBatch.
  • groupdocs.search.dictionaries: SynonymDictionary, HomophoneDictionary, StopWordDictionary, SpellingCorrector, AliasDictionary, CharacterReplacementPair.
  • groupdocs.search.events: EventHub, FileIndexingEventArgs, IndexErrorEventArgs, OperationProgressEventArgs, PasswordRequiredEventArgs.
  • groupdocs.search.exceptions: SearchException, LicenseRestrictionException.
  • groupdocs.search.scaling: SearchNetworkNode, Indexer, Searcher.
  • groupdocs.search.scaling.configuring: Configuration, Configurator, TcpSettings.

新功能

索引文档并搜索它们

Index(folder) 在文件夹中创建索引,或打开已有的索引。add() 对文件夹、单个文件、路径列表、流或已提取数据进行索引,search() 接受 GroupDocs.Search 查询语言的查询:关键字、用引号括起的短语、通配符(? 表示单字符,?(1~5) 表示 1 到 5 个字符)、^ 后的正则表达式、AND、OR、NOT,以及 content: Bern、filename: einstein 等字段查询,还支持日期和数字范围。SearchQuery 以对象形式构建相同的查询。

from groupdocs.search import Index

with Index("./index") as index:              # creates the index folder, or opens it
    index.add("./documents")                 # indexes every supported file in the folder
    for query in ["relativity", '"theory of relativity"', "relativ?ty",
                  "^relativ.*", "Einstein AND NOT Newton", "content: Bern"]:
        result = index.search(query)
        print("%-26s %d documents, %d occurrences"
              % (query, result.document_count, result.occurrence_count))

模糊、同义词和词形搜索

模糊搜索容忍输入和 OCR 错误。同义词、同音词和词形搜索通过内置的英文词典扩展查询,index.dictionaries 可对其进行编辑,拼写和键盘布局纠正也是搜索选项。

from groupdocs.search import Index
from groupdocs.search.options import SearchOptions, SimilarityLevel

with Index("./index") as index:
    index.add("./documents")

    fuzzy = SearchOptions()
    fuzzy.fuzzy_search.enabled = True
    fuzzy.fuzzy_search.fuzzy_algorithm = SimilarityLevel(0.8)   # 0.0 (loose) .. 1.0 (strict)
    print("relativty:", index.search("relativty").document_count,
          "->", index.search("relativty", fuzzy).document_count)

    synonyms = SearchOptions()
    synonyms.use_synonym_search = True
    print("picture:", index.search("picture").document_count,
          "->", index.search("picture", synonyms).document_count)

    word_forms = SearchOptions()
    word_forms.use_word_forms_search = True
    print("letter:", index.search("letter").document_count,
          "->", index.search("letter", word_forms).document_count)

高亮显示已找到的内容

DocumentHighlighter 将整个文档写入文件,并标记出找到的词汇;FragmentHighlighter 只输出这些词汇所在的片段,可生成 HTML 或纯文本,写入文件、字符串或二进制文件对象。

from groupdocs.search import Index
from groupdocs.search.common import FileOutputAdapter
from groupdocs.search.highlighters import DocumentHighlighter
from groupdocs.search.options import OutputFormat

with Index("./index") as index:
    index.add("./documents")
    result = index.search("relativity")
    if result.document_count > 0:
        found = result.get_found_document(0)
        adapter = FileOutputAdapter(OutputFormat.HTML, "./highlighted.html")
        index.highlight(found, DocumentHighlighter(adapter))
        print(found.document_info.file_path, "-> highlighted.html")

.NET 事件作为 Python 处理程序

索引的每个事件都是 Python 事件:使用 += 订阅,使用 -= 取消订阅。处理程序可以跳过文档、为其添加属性或提供密码,并可跟踪索引和搜索的进度、错误和阶段。

from groupdocs.search import Index

skipped = []

def on_file_indexing(sender, args):
    if args.document_full_path.endswith(".zip"):
        args.skip_indexing = True            # leave archives out of the index
        skipped.append(args.document_full_path)

with Index("./index") as index:
    index.events.file_indexing += on_file_indexing
    index.add("./documents")
    index.events.file_indexing -= on_file_indexing
    print(len(index.get_indexed_documents()), "documents indexed,", len(skipped), "archives skipped")

用于引擎扩展点的 Python 对象

自定义文本提取器(用于自有格式)、日志记录器、分词器、词形提供器、文档加载器以及 OCR 连接器都可以是普通的 Python 对象或函数,引擎通过其接口调用它们。

from groupdocs.search import Index, IndexSettings

class LogExtractor(object):
    """Text of .log files, which the engine does not read on its own."""
    extensions = [".log"]

    def get_fields(self, source):             # a file path, or the engine's stream
        if isinstance(source, str):
            with open(source, encoding="utf-8") as f:
                text = f.read()
        else:
            text = source.read().decode("utf-8")
        return [("Content", text)]

settings = IndexSettings()
settings.custom_extractors.add(LogExtractor())
with Index("./log-index", settings) as index:
    index.add("./logs")
    print(index.search("quota").document_count, "log file(s) mention a quota")

保持索引最新

索引本质上是一个文件夹:构建一次后可在后续运行中重新打开。update() 只重新索引自上次 add() 或 update() 以来发生变化的文档,optimize() 合并索引段,delete() 按路径删除文档。

from groupdocs.search import Index

with Index("./index") as index:
    index.add("./documents")
    # ... files in ./documents are added, changed or removed ...
    index.update()        # re-indexes only what changed
    index.optimize()      # merges index segments so searches stay fast
    print(len(index.get_indexed_documents()), "documents in the index")

从环境获取许可证

将 GROUPDOCS_LIC_PATH 设置为许可证文件的路径或 URL,许可证将在首次调用引擎时自动生效;在代码中使用 License().set_license(path) 也可实现同样效果。若许可证无法生效,则从首次调用起抛出 LicenseError,而不会回退到评估模式。未提供许可证时,索引最多只能容纳 100 个文档,且一次搜索只能读取前 10 个找到的文档。

export GROUPDOCS_LIC_PATH="/path/to/GroupDocs.Search.lic"

代码示例

请参阅上文的 “索引文档并搜索它们”、 “模糊、同义词和词形搜索”、 “高亮显示已找到的内容”、 “.NET 事件作为 Python 处理程序”、 “用于引擎扩展点的 Python 对象” 以及 “保持索引最新” 部分,以获取可运行的代码示例。

如何获取更新

PyPI

直接下载

从 GroupDocs.Search for Python via .NET 26.10 页面下载适用于您平台的 wheel 文件,然后安装:

  • Windows x64: pip install groupdocs_search_net-26.10.0-py3-none-win_amd64.whl
  • Linux x64: pip install groupdocs_search_net-26.10.0-py3-none-manylinux_2_27_x86_64.whl
  • macOS Intel: pip install groupdocs_search_net-26.10.0-py3-none-macosx_13_0_x86_64.whl
  • macOS Apple Silicon: pip install groupdocs_search_net-26.10.0-py3-none-macosx_13_0_arm64.whl

资源