A “Hello, World!” example is often the first step when exploring GroupDocs.Search for Python via .NET. It serves as a simple test to confirm that your development environment is correctly set up and that the library is functioning as expected.
Overview
GroupDocs.Search for Python via .NET indexes documents and searches them by their content. A wide range of supported formats makes it versatile for different use cases: one index can hold Word documents, spreadsheets, PDF files, emails and many more.
How to index and search documents
The following steps demonstrate how to index a folder of documents and search it using GroupDocs.Search for Python via .NET:
Import the Index class from groupdocs.search.
Create an Index in a folder of its own. The index is kept there, and opened again on the next run.
Add the folder of documents to the index with add().
Search the index with search().
Read the number of documents found and the occurrences in each, the most relevant document first.
Complete example
The example below indexes the documents of the documents folder and searches them for the word “physics”:
importosfromgroupdocs.searchimportIndexdefhello_world():# Create an index in the "index" folder, or open the one already therewithIndex("./index")asindex:# Index every supported document in the "documents" folderindex.add("./documents")# Search the index for a wordresult=index.search("physics")print("Documents found:",result.document_count)# Most relevant first: the result lists the documents in the order they were indexedfordocumentinsorted(result,key=lambdafound:found.relevance,reverse=True):print(os.path.basename(document.document_info.file_path),"-",document.occurrence_count,"occurrences")if__name__=="__main__":hello_world()
The documents are sorted by relevance because the result lists them in the order they were indexed, which is the order in which the file system lists the folder. That is name order on Windows but not on Linux, so without sorting the two documents can come the other way round there.
Run the script again and the index opens from the index folder: add() indexes only the documents that are new or have changed since the last run. To search other documents, put them into the documents folder, or pass another folder to add().
Additional resources
This demo references the GroupDocs.Search for Python via .NET code samples.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.