我們很高興宣布 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 執行環境。
此次發佈的新功能
- [功能] 透過 .NET 的 GroupDocs.Search for Python 初始發佈 (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,並提供「您是否指的是」的提示,而不是建立引擎永遠不會看到的僅 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) 代表一到五個字元)、^ 後的正則表達式、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
- Package: groupdocs-search-net 26.10.0
- Install command:
pip install groupdocs-search-net==26.10.0
直接下載
從 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