@@ -223,8 +223,8 @@ Collections can be created like:
223223 }
224224
225225 This example creates a collection that, by default, allows JSON
226- documents to be stored. A non-unique B-tree index is created on the
227- ``address.city `` path to improve search performance.
226+ documents to be stored. A non-unique :ref: ` B-tree index < sodaindexes >` is
227+ created on the ``address.city `` path to improve search performance.
228228
229229If the collection name passed to
230230:meth: `sodaDatabase.createCollection() ` already exists, it
@@ -244,7 +244,7 @@ metadata.
244244
245245.. _accessingsodadocuments :
246246
247- Creating and Accessing SODA documents
247+ Creating and Accessing SODA Documents
248248=====================================
249249
250250To insert a document into an opened collection, a JavaScript object that
@@ -558,6 +558,137 @@ Some QBE examples are:
558558 See `Overview of QBE Spatial Operators <https://www.oracle.com/pls/topic/
559559 lookup?ctx=dblatest&id=GUID-12994E27-DA98-40C7-8D4F-84341106F8D9> `__.
560560
561+ .. _sodaindexes :
562+
563+ Creating and Dropping SODA Indexes
564+ ==================================
565+
566+ Indexing can improve the performance of SODA query-by-examples (QBE) or enable
567+ text searches. An index is defined by a specification, which is a JSON object
568+ that specifies how particular QBE patterns are to be indexed for quicker
569+ matching.
570+
571+ Note that a commit should be performed before attempting to create an
572+ index.
573+
574+ Each index specification is uniquely identified by the ``name `` field. The
575+ different index types that you can specify are:
576+
577+ - B-tree: Used to speed up query-by-example (QBE)
578+ :meth: `sodaOperation.filter() ` searches. For this index type, you must
579+ specify the ``fields `` field in the index specification.
580+
581+ - GeoSpatial: Used for speeding up QBEs that do GeoJSON queries. For this
582+ index type, you must specify the ``spatial `` field in the index
583+ specification.
584+
585+ - JSON search: Required for text searches using the ``$contains ``
586+ operator in QBEs. Also, improves QBE filter operation performance. For this
587+ index type, you must not specify the ``fields `` and ``spatial `` fields in
588+ the index specification. Note that a B-tree index will perform better for
589+ non-text searches.
590+
591+ See `Overview of SODA Indexing <https://www.oracle.com/pls/topic/lookup?ctx=
592+ dblatest&id=GUID-4848E6A0-58A7-44FD-8D6D-A033D0CCF9CB> `__.
593+
594+ As an example, if a collection has these documents::
595+
596+ {"name": "Chris"}
597+ {"name": "Venkat"}
598+ {"name": "Srinath"}
599+
600+ You must first specify the type of index that you want by creating a SODA
601+ index specification. For example, to create a B-tree index specification, you
602+ need to specify the ``fields `` field:
603+
604+ .. code-block :: javascript
605+
606+ indexSpec = {name: " myIndex" , fields: [{path: " name" }]};
607+
608+ Then use that index specification to create the B-tree index using
609+ :meth: `sodaCollection.createIndex() `:
610+
611+ .. code-block :: javascript
612+
613+ await collection .createIndex (indexSpec);
614+
615+ This index would improve the performance of QBEs like:
616+
617+ .. code-block :: javascript
618+
619+ d = await collection .find ().filter ({name: " Venkat" }).getOne ();
620+
621+ To drop a specific index on a SODA collection, use
622+ :meth: `sodaCollection.dropIndex() `:
623+
624+ .. code-block :: javascript
625+
626+ await collection .dropIndex (" myIndex" );
627+
628+ .. _listindexes :
629+
630+ Retrieving All Index Specifications in a Collection
631+ ---------------------------------------------------
632+
633+ You can retrieve all the index specifications defined for the documents in a
634+ collection using :meth: `sodaCollection.listIndexes() `. For example:
635+
636+ .. code-block :: javascript
637+
638+ // Create a new SODA collection
639+ const collection = await soda .createCollection (" mycollection" );
640+
641+ // Create new index specifications
642+ const indexArr = [
643+ {
644+ " name" : " HOME_IDX" ,
645+ " fields" : [
646+ {
647+ " path" : " home" ,
648+ " datatype" : " string" ,
649+ " order" : " asc"
650+ }
651+ ]
652+ },
653+ {
654+ " name" : " OFFICE_IDX" ,
655+ " fields" : [
656+ {
657+ " path" : " office" ,
658+ " datatype" : " string" ,
659+ " order" : " asc"
660+ }
661+ ]
662+ }
663+ ];
664+
665+ To create new indexes for each of the index specifications in ``IndexArr ``:
666+
667+ .. code-block :: javascript
668+
669+ await collection .createIndex (indexArr[0 ]);
670+ await collection .createIndex (indexArr[1 ]);
671+
672+ To retrieve all the index specifications in the collection:
673+
674+ .. code-block :: javascript
675+
676+ // Retrieve list of indexes in a collection
677+ const fetchedIndexArr = await collection .listIndexes ();
678+
679+ // Sort the index specification names in alphabetical order
680+ fetchedIndexArr .sort (function (a , b ) {
681+ return a .name .localeCompare (b .name );
682+ });
683+
684+ console .log (" fetchIndexArr-0 " + JSON .stringify (fetchedIndexArr[0 ]));
685+ console .log (" fetchIndexArr-1 " + JSON .stringify (fetchedIndexArr[1 ]));
686+
687+ This prints an output such as::
688+
689+ fetchIndexArr-0 {"name":"HOME_IDX","schema":"SCOTT","tableName":"MYCOLLECTION","tableSchemaName":"SCOTT","indexNulls":false,"unique":false,"lax":false,"scalarRequired":false,"fields":[{"path":"home","dataType":"VARCHAR2","maxLength":2000,"order":"ASC"}]}
690+ fetchIndexArr-1 {"name":"OFFICE_IDX","schema":"SCOTT","tableName":"MYCOLLECTION","tableSchemaName":"SCOTT","indexNulls":false,"unique":false,"lax":false,"scalarRequired":false,"fields":[{"path":"office","dataType":"VARCHAR2","maxLength":2000,"order":"ASC"}]}
691+
561692.. _sodatextsearches :
562693
563694SODA Text Searches
0 commit comments