Document Search
The Vyasa Layar API's SourceDocumentApi class gives you access to every document across all of your data sources in a single easy-to-use endpoint. With the SourceDocumentApi you can search, create, edit, annotate, and count documents as well as extract tables and images, concepts, and previews. It is an incredibly powerful tool. In this guide, we will focus on the searching capabilities and associated filtering capabilities.
Setting Up
The first step for using the search_documents
method is to instantiate the API. Make sure you have already followed the instructions for importing dependencies and authentication from the Getting Started Guide. Once you have completed that, instantiate the SourceDocumentApi and DataProvidersApi as shown below.
documents = layar_api.SourceDocumentsApi(client)
data_providers = layar_api.DataProvidersApi(client)
Check Your References
Make sure you have imported the
layar_api
and built out yourclient
object before instantiating the SourceDocumentApi. If you need help setting these up check out Installing the Layar API and Configure Your Instance in our Getting Started Guide
The next thing you'll need is a list of all the data providers you would like to search within. You can retrieve your available data providers and then store them into a comma-delimited list by using the search_data_providers
method on the DataProvidersApi.
get_providers = data_providers.search_data_providers()
providers = ''
for provider, index in get_providers:
if index == (get_providers.len() - 1):
providers = providers + provider
else:
providers = providers + provider + ', '
# Check provider string (optional)
print(providers)
We can now use the list of data providers for the x-vyasa-data-providers
parameter in the search_documents
method which will limit our search to the data providers we selected. We still need a SourceDocumentSearchCommand which will be where we do the rest of our filtering.
SourceDocumentSearchCommand
The SourceDocumentSearchCommand is an object that will hold all of our filtering parameters. There are nearly forty available parameters so we will only go over a few of the more common ones here. The full listing of available filtering parameters is available in the Search for documents in Layar reference page.
body = layar_api.SourceDocumentSearchCommand()
Search By IDs
If you have access to the specific document Layar IDs you would like to request, perhaps from another API call, you can add them as a list of strings to the ids
property.
body.ids = ['document_id_1', 'document_id_2']
Search By Terms
You can use keyword searching just as you would in Layar by adding a list of strings in the terms
property. You can even optionally specify a term_ operator
to specify whether you would like the search to match all or some of the terms.
body.terms = ['term1', 'term2', 'term3'] # The terms to search for
body.term_operator = 'AND' # 'AND' will require all terms are found, 'OR' will return results with one or more terms
Search By Saved Lists
If you would like to limit your search to the documents in a saved list or a few saved lists, you can add the list IDs to the saved_list_ids
property.
body.saved_list_ids = ['list_id_1', 'list_id_2']
Search By NER
With NER filtering you can find documents that are tagged with specific concepts or with concept types. The named_entities
property accepts a list of NamedEntity objects that you can use to filter your document search results.
body.named_entities = [layar_api.NamedEntity(concept='fibromyalgia'), layar_api.NamedEntity(type_id='DISEASE')]
Perform the Search
Once you have built your SourceDocumentSearchCommand with the filter parameters you want, it's time to call the method and run the search.
result = documents.search_documents(body=body, x-vyasa-data-providers=providers)
# Check your result (optional)
pprint(result)
Updated 12 months ago