• Aucun résultat trouvé

Content Indexing

Dans le document SQL Server (Page 140-144)

In addition to creating primary and secondary XML indexes on the xmldata type column, you can cre-ate and use full-text indexes on the column. While the primary and secondary XML indexes index the values and nodes, a full-text index indexes the entire XML instance, ignoring the values, nodes, and other XML syntax.

However, unlike primary and secondary XML indexes, only one full-text index per table is allowed. Not per column, but per table. A full-text index is applied to a column, not a table.

Because both types of indexes (primary/secondary and full-text) can be applied to both a table and a column, they both can be used together to query an XML instance. In the case of a full-text index, the index is applied first, and then an XQuery expression is applied to sift deeper.

The requirements for creating a full-text index are very similar to that of the primary index in that a unique primary key column must already be defined on the table for which the full-text index is created.

The basic syntax for a full-text index is as follows:

CREATE FULLTEXT INDEX

ON TableName ( xml_ColumnName ) KEY INDEX IndexName

TableNamerefers to the table in which the full-text index is being created. ColumnNameis the name of the column on which the full-text index will be applied. IndexNameis the name of the unique primary key index.

Before a full-text index can be created, a full-text catalog must exist in the database, as all full-text indexes are stored in the catalog. A database can contain one or more catalogs.

The following example first creates a full-text catalog in which to store the full-text index, and then cre-ates a full-text index on the Instructions column of the Production.ProductModel table:

CREATE FULLTEXT CATALOG FTC AS DEFAULT GO

CREATE FULLTEXT INDEX

ON Production.ProductModel(Instructions) KEY INDEX PK_ProductModel_ProductModelID ON FTC

GO

Figure 6-7 displays the results of the CREATE FULLTEXT CATALOGstatement. The full-text catalog FTC has been created in the AdventureWorks database.

Figure 6-7

Double-click on the FTC full text catalog, or right-click and select Properties to display the FTC Properties page. On the left side of the Properties page, select Tables/Views. This page (see Figure 6-8) shows that a full-text index was created on the Production.ProductModel table using the unique index PK_ProductModel_ProductModelIDon the Instructions column. Any table that has a full-text index on it is automatically displayed in the list on the right, as it shows only those tables that have a full-text index.

Figure 6-8

In the following section, you put the full-text index to use.

CONTAINS()

You use the CONTAINS()keyword to search character strings looking for word or phrase matches. It also conducts what is called a proximity search, looking for a word that is near another word.

For example, the following statement uses the CONTAINS()keyword to search the Instructions column of the Production.ProductModel table looking for the phrase “Inspection Specification”:

SELECT ProductModelID, Instructions FROM production.productmodel

WHERE CONTAINS(Instructions, ‘“Inspection Specification”’) The results of the query are shown in Figure 6-9.

Figure 6-9

To get the best performance out of full-text searches, in certain scenarios it is possi-ble to combine a full-text search with an XML index. The first step is to filter the XML values using the SQL full-text search, and then query the filtered values.

In this example, the results returned four rows because the query found four instances of the phrase

“Inspection Specification”within the Instructions column. The CONTAINSpredicate in this exam-ple searched each row of the Instructions column looking inside each XML document, and returned only those rows that contain the “Inspection Specification”phrase.

The results of the query can be defined even further with an additional ANDclause, such as the follow-ing, which would return a single row:

SELECT ProductModelID, Instructions FROM production.productmodel

WHERE CONTAINS(Instructions, ‘“Inspection Specification”’) AND ProductModelID = 47

In the following example, the CONTAINSpredicate looks for multiple phrases by including an additional ORcondition:

SELECT ProductModelID, Instructions FROM production.productmodel

WHERE CONTAINS(Instructions, ‘“Inspection Specification” OR “Securely tighten the spindle”’)

The query returns results where the CONTAINSpredicate finds instructions containing the phrase

“Inspection Specification”or “Securely tighten the spindle”, this time returning the same four rows as previously shown in Figure 6-9 plus an additional row, ProductModelID53.

The next example uses the NEARkeyword looking for the word Inspectnear the word Specification. The NEARoperator is used to indicate that a word or phrase on the left side of the NEARoperator is in close proximity to the word or phrase on the right side of the operator:

SELECT ProductModelID, Instructions FROM production.productmodel

WHERE CONTAINS(Instructions, ‘Inspect NEAR Specification’)

The results from this query return seven rows, as shown in Figure 6-10, indicating that the query found seven instances of the word Inspectnear the word Specification.

Figure 6-10

As stated earlier, further filtering can be accomplished by combining the full-text index (using the CONTAINS()keyword) with an XQuery expression to filter the results even more. The following exam-ple again queries the Instructions column of the Production.ProductModel table to look for a specific value returned from the CONTAINS()keyword:

SELECT ProductModelID, CatalogDescription.query(‘

declare namespace pd=”http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription”;

<Prod>

{ /pd:ProductDescription/@ProductModelID } { /pd:ProductDescription/pd:Summary }

</Prod>

‘) as Result

FROM Production.ProductModel

WHERE CatalogDescription.value(‘declare namespace

pd=”http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription”;

contains( (/pd:ProductDescription/pd:Summary//*/text())[1],

“smooth-shifting”)’,’bit’) = 1 The results of this query are shown in Figure 6-11.

Figure 6-11

In this example, a full-text index is combined with an XQuery expression to find and filter the results. The SELECTpart of the query returns the ProductModelID and the entire Summarynode based on the results of the WHEREclause. The WHEREclause is where the filtering takes place using the CONTAINSpredicate.

The CONTAINSpredicate uses an XQuery to look through the Summarynode in the CatalogDescription column looking for the text “smooth-shifting”. If the query finds what it is looking for, it returns the information requested by the SELECTportion of the query.

As you create and use the indexes, you will also find that there will probably be a need to modify those indexes to better fit your application. The topic of modifying indexes is discussed next.

Dans le document SQL Server (Page 140-144)