{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Text search\n",
    "\n",
    "Search a Granta MI database for records containing a specified text value.\n",
    "\n",
    "This example demonstrates:\n",
    "\n",
    "* Creating a connection to a Granta MI server\n",
    "* Retrieving Granta MI databases from the server\n",
    "* Searching a Granta MI database with the *SimpleTextSearch* operation\n",
    "\n",
    "## Create a Granta MI Session\n",
    "\n",
    "Import the GRANTA_MIScriptingToolkit package, and create a connection to a Granta MI server."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import GRANTA_MIScriptingToolkit as gdl\n",
    "\n",
    "session = gdl.GRANTA_MISession('http://localhost/mi_servicelayer', autoLogon=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select a database\n",
    "\n",
    "View the databases available on your Granta MI server."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 2 databases on the Granta MI Server\n",
      "Database key: MI_Pro, Database name: MI Pro\n",
      "Database key: MI_Training, Database name: MI Training\n"
     ]
    }
   ],
   "source": [
    "databases = session.browseService.GetDatabases().databases\n",
    "\n",
    "print(\"Found {0} databases on the Granta MI Server\".format(len(databases)))\n",
    "for d in databases:\n",
    "    print(\"Database key: {0.DBKey}, Database name: {0.name}\".format(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Search a database\n",
    "\n",
    "Search in the MI Training database for records containing the text *Leather*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dbKey = 'MI_Training'\n",
    "searchText = 'leather'\n",
    "\n",
    "simpleTextSearch = gdl.SimpleTextSearch(searchValue=searchText, DBKey=dbKey)\n",
    "simpleTextSearchResponse = session.searchService.SimpleTextSearch(simpleTextSearch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Print the results returned by the search."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Leather\n",
      "Potassium chromate [7789-00-6]\n",
      "Alkanes, C10-13, chloro;  (Short chain chlorinated paraffins ) [85535-84-8]\n",
      "Exercise Model Answers.ipynb\n"
     ]
    }
   ],
   "source": [
    "for result in simpleTextSearchResponse.searchResults:\n",
    "    print('{0}'.format(result.shortName))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
