SODA for In-Database JavaScriptによるコレクション内のドキュメントの索引付け
索引は、NoSQLスタイルのSODA APIとリレーショナル・アプローチのどちらを使用するかに関係なく、データ・アクセスを高速化できます。SODAコレクション内のドキュメントの索引付けには、SodaCollection.createIndex()を使用します。そのIndexSpecパラメータはテキストのJSON索引指定です。
既存の索引は、SodaCollection.dropIndex()を使用して削除できます。
JSON検索索引は、全文問合せおよび非定型構造問合せに使用され、永続的な記録およびJSONデータ・ガイド情報の自動更新に使用されます。
関連項目:
-
SODA索引の使用の概要については、Oracle AI Database Simple Oracle Document Access (SODA)の概要を参照
-
SODA索引の仕様に関する情報は、Oracle AI Database Simple Oracle Document Access (SODA)の概要を参照
-
JSON検索索引の詳細は、『Oracle AI Database JSON開発者ガイド』を参照
-
JSON検索索引の一部である永続データ・ガイド情報の詳細は、『Oracle AI Database JSON開発者ガイド』を参照
例8-18 SODA for In-Database JavaScriptを使用したJSONフィールドに対するBツリー索引の作成
この例では、コレクションemployeesCollection (例8-8で作成)内のJSONドキュメントの数値フィールドdepartment_idに対して一意ではないBツリー索引を作成します。
export function createBTreeIndex(){
// open the collection
const col = soda.openCollection('employeesCollection');
if(col === null){
throw new Error("'employeesCollection' does not exist");
}
// define the index...
const indexSpec = {
"name": "DEPARTMENTS_IDX",
"fields": [
{
"path": "departmentId",
"datatype": "number",
"order": "asc"
}
]
};
//... and create it
try{
col.createIndex(indexSpec);
} catch(err){
throw new Error(
`could not create the index: ${err}`
)
}
}
例8-19 SODA for In-Database JavaScriptを使用したJSON検索索引の作成
この例では、コレクションemployeesCollection (例8-8で作成)のドキュメントを索引付けするためのJSON検索索引の作成方法を示します。これは、非定型問合せおよび全文検索(QBE演算子$containsを使用する問合せ)に使用できます。これによって、JSONドキュメントに関するデータ・ガイド情報(集計構造情報および型情報)が自動的に蓄積および更新されます。索引仕様には、フィールドnameのみが含まれます(例8-18のBツリー索引とは異なり、fieldフィールドは含まれません)。
export function createSearchIndex(){
// open the collection
const col = soda.openCollection("employeesCollection");
if(col === null){
throw new Error("'employeesCollection' does not exist");
}
// define the index properties...
cost indexSpec = {
"name": "SEARCH_AND_DATA_GUIDE_IDX",
"dataguide": "on",
"search_on": "text_value"
}
//...and create it
try{
col.createIndex(indexSpec);
} catch(err){
throw new Error(
`could not create the search and Data Guide index: ${err}`
);
}
}
非定型(検索)の索引付けのみを高速化する場合は、フィールドdataguideに値"off"を指定する必要があります。dataguide索引付け機能が不要な場合は、同じ方法でこれをオフにできます。
例8-20 SODA for In-Database JavaScriptを使用した索引の削除
この例は、SodaCollection.dropIndex()およびforceオプションを使用してコレクションの既存の索引を削除する方法を示しています。
この例で使用されているemployeesCollectionの作成方法の詳細は、例8-8を参照してください。
export function dropIndex(indexName){
// open the collection
const col = soda.openCollection("employeesCollection");
if(col === null){
throw new Error("'employeesCollection' does not exist");
}
// drop the index
const result = col.dropIndex(indexName, {"force": true});
if(!result.dropped){
throw `Could not drop SODA index '${indexName}'`;
}
}
SodaCollection.dropIndex()は、単一のフィールドdroppedを含む結果オブジェクトを戻します。索引が削除された場合、その値はtrueで、それ以外の場合はfalseです。どちらの場合でもメソッドは正常に終了します。
オプションのパラメータ・オブジェクトをメソッドに指定できます。forceをtrueに設定すると、基礎となるOracle AI Databaseドメイン索引で通常の削除が許可されない場合に、JSON索引の削除が強制されます。