MDATA Section
You use an MDATA section to reference user-defined metadata for a document.
MDATA sections can speed up mixed queries, and there is no limit to the number of MDATA sections that can be returned in a query.
Consider the case where you want to query according to text content and document type (magazine, newspaper, or novel). You can create an index with a column for text and a column for the document type, and then perform a mixed query of this form. In this case, search for all novels with the phrase Adam Thorpe (author of the novel Ulverton):
SELECT id FROM documents
WHERE doctype = 'novel'
AND CONTAINS(text, 'Adam Thorpe')>0;
However, it is usually faster to incorporate the attribute (in this case, the document type) in a field section, rather than using a separate column, and then using a single CONTAINS query.
SELECT id FROM documents
WHERE CONTAINS(text, 'Adam Thorpe AND novel WITHIN doctype')>0;
This approach has two drawbacks:
-
Each time the attribute is updated, the entire text document must be reindexed, resulting in increased index fragmentation and slower rates of data manipulation language (DML) processing.
-
Field sections tokenize the section value. Tokenization has several effects. Special characters in metadata, such as decimal points or currency characters, are not easily searchable; value searching (searching for John Smith but not John Smith, Jr.) is difficult; multiword values are queried by phrase, which is slower than single-token searching; and multiword values do not show up in browsed words, making author browsing or subject browsing impossible.
For these reasons, using MDATA sections instead of field sections may be worthwhile. MDATA sections are indexed like field sections, but you can add and remove metadata values from documents without the need to reindex the document text. Unlike field sections, MDATA values are not tokenized. Additionally, MDATA section indexing generally takes up less disk space than field section indexing.
Starting with Oracle Database 12c Release 2 (12.2), the MDATA section can be updatable or nonupdatable depending on the value of its read-only tag, which can be set to either FALSE or TRUE.
Use CTX_DDL.ADD_MDATA_SECTION to add an MDATA section to a section group. By default, the value of a read-only MDATA section is FALSE. It implies that you want to permit calling CTX_DDL.ADD_MDATA() and CTX_DDL.REMOVE_MDATA() for this MDATA section, otherwise you can set it to TRUE. When set to FALSE, the queries on the MDATA section run less efficiently because a cursor must be opened on the index table to track the deleted values for that MDATA section. This example adds an MDATA section called AUTHOR and gives it the value Soseki Natsume (author of the novel Kokoro).
ctx_ddl.create.section.group('htmgroup', 'HTML_SECTION_GROUP');
ctx_ddl.add_mdata_section('htmgroup', 'author', 'Soseki Natsume');
You can change MDATA values with CTX_DDL.ADD_MDATA, and you can remove them with CTX_DDL.REMOVE_MDATA. Also, MDATA sections can have multiple values. Only the owner of the index may call CTX_DDL.ADD_MDATA and CTX_DDL.REMOVE_MDATA.
Neither CTX_DDL.ADD_MDATA nor CTX_DDL.REMOVE_MDATA is supported for CTXCAT and CTXRULE indexes.
MDATA values are not passed through a lexer. Instead, all values undergo the following simplified normalization:
-
Leading and trailing whitespace on the value is removed.
-
The value is truncated to 255 bytes.
-
The value is indexed as a single value; if the value consists of multiple words, it is not broken up.
-
Case is preserved. If the document is dynamically generated, you can implement case-insensitivity by uppercasing
MDATAvalues and making sure to search only in uppercase.
After you add MDATA metadata to a document, you can query for that metadata by using the CONTAINS query operator:
SELECT id FROM documents
WHERE CONTAINS(text, 'Tokyo and MDATA(author, Soseki Natsume)')>0;
This query is only successful if an AUTHOR tag has the exact value Soseki Natsume (after simplified tokenization). Soseki or Natsume Soseki returns no rows.
The following are considerations for MDATA:
-
MDATAvalues are not highlightable, do not appear in the output ofCTX_DOC.TOKENS,and do not appear when you enableFILTER PLAINTEXT. -
MDATAsections must be unique within section groups. For example, do not useFOOas the name of anMDATAsection and a zone or field section in the same section group. -
Like field sections,
MDATAsections cannot overlap or nest. AnMDATAsection is implicitly closed by the first tag encountered. In this example:<AUTHOR>Dickens <B>Shelley</B> Keats</AUTHOR>The
<B>tag closes theAUTHORMDATAsection; as a result, this document has anAUTHORof ‘Dickens’, but not of ‘Shelley’ or ‘Keats’. -
To prevent race conditions, each call to
ADD_MDATAandREMOVE_MDATAlocks out other calls on that rowid for that index for all values and sections. However, becauseADD_MDATAandREMOVE_MDATAdo not commit, it is possible for an application to deadlock when calling them both. It is the application’s responsibility to prevent deadlocking.
See Also:
-
"ALTER INDEX"in Oracle Text Reference -
"ADD_MDATA_SECTION"in Oracle Text Reference -
The
"CONTAINS"query operators chapter of the Oracle Text Reference for information on theMDATAoperator -
The
"CTX_DDL"package chapter of Oracle Text Reference for information on adding and removingMDATAsections