Search code examples
azure-cognitive-search

How to get generic json string from Azure search SearchClient


I am using azure search SDK SearchClient to query documents from index. To use the SearchClient.Search(), method, I must firstly defined a type T for my index document. For example

searchClient.Search<Hotel>("*", options);

Is it possible just let SearchClient.Search return general Json string so that I can parse it with my own code?


Solution

  • Instead of using a specific type, you can use SearchDocument which represents an untyped document returned from a search or document lookup. It can be accessed as either a dynamic object or a dictionary.

    So your code would be:

    searchClient.Search<SearchDocument>("*", options);
    

    From this link:

    Search and SearchAsync methods support mapping of search field types to .NET types via the type parameter T. You can provide your own type T or use the dynamic SearchDocument.