Full-Text Query

From Ilianko
Revision as of 15:07, 26 October 2026 by Anko (talk | contribs) (Recreated DP-420 term page: Full-Text Query (Integration & Analytics))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

← Back to DP-420 Terminology Reference

Category: Integration & Analytics

Summary: Native keyword search via FULLTEXTCONTAINS/FULLTEXTSCORE, backed by a full-text index.

Overview

A full-text query is Azure Cosmos DB for NoSQL's native capability to search text properties directly using functions such as FULLTEXTCONTAINS and FULLTEXTSCORE (BM25 relevance ranking), backed by a full-text index defined in the container's indexing policy — without requiring a separate search service. It can also be combined with vector search results using reciprocal rank fusion (RRF) for hybrid search, offering a lighter-weight alternative to integrating a dedicated Azure AI Search Integration when only basic keyword relevance search is needed.

using Microsoft.Azure.Cosmos;

string endpoint = "";
string key = "";

using CosmosClient client = new(endpoint, key);

Database database = await client.CreateDatabaseIfNotExistsAsync(
    id: "catalog-db"
);

// Define the container
ContainerProperties containerProperties = new()
{
    Id = "products",

    // Partition key
    PartitionKeyPath = "/category",

    // Optional: automatically expire items after 30 days
    DefaultTimeToLive = 60 * 60 * 24 * 30,

    // Declare which properties contain searchable text
    FullTextPolicy = new FullTextPolicy
    {
        DefaultLanguage = "en-US",

        FullTextPaths =
        {
            new FullTextPath
            {
                Path = "/name",
                Language = "en-US"
            },
            new FullTextPath
            {
                Path = "/description",
                Language = "en-US"
            }
        }
    },

    // Configure all indexes for the container
    IndexingPolicy = new IndexingPolicy
    {
        IndexingMode = IndexingMode.Consistent,
        Automatic = true,

        IncludedPaths =
        {
            new IncludedPath
            {
                Path = "/*"
            }
        },

        // Do not use the regular range index for this property
        ExcludedPaths =
        {
            new ExcludedPath
            {
                Path = "/largeMetadata/*"
            }
        },

        // Composite index:
        //
        // SELECT *
        // FROM c
        // WHERE c.category = "laptops"
        // ORDER BY c.price DESC
        CompositeIndexes =
        {
            new Collection
            {
                new CompositePath
                {
                    Path = "/category",
                    Order = CompositePathSortOrder.Ascending
                },
                new CompositePath
                {
                    Path = "/price",
                    Order = CompositePathSortOrder.Descending
                }
            }
        },

        // Specialized full-text indexes
        FullTextIndexes =
        {
            new FullTextIndexPath
            {
                Path = "/name"
            },
            new FullTextIndexPath
            {
                Path = "/description"
            }
        }
    }
};

// Create the container with 400 provisioned RU/s
ContainerResponse response =
    await database.CreateContainerIfNotExistsAsync(
        containerProperties: containerProperties,
        throughput: 400
    );

Container container = response.Container;

Console.WriteLine(
    $"Container '{container.Id}' is ready. " +
    $"Status: {response.StatusCode}"
);
// Create a container with only full-text indexing
ContainerProperties containerProperties = new()
{
    Id = "text-only",
    PartitionKeyPath = "/id",

    // Only define full-text indexes
    IndexingPolicy = new IndexingPolicy
    {
        IndexingMode = IndexingMode.Consistent,
        Automatic = true,

        // Only full-text indexes
        FullTextIndexes =
        {
            new FullTextIndexPath
            {
                Path = "/title"
            },
            new FullTextIndexPath
            {
                Path = "/abstract"
            }
        },

        // No range or composite indexes
        IncludedPaths = { },
        ExcludedPaths = { }
    }
};
  
    var queryable = container.GetItemLinqQueryable(
        queryText: "SELECT * FROM c WHERE FULLTEXTCONTAINS(c.description, @search)",
        parameters: new[]
        {
            new SqlParameter("@search", "high-performance laptop")
        }
    );

    foreach (Product product in queryable)
    {
        Console.WriteLine($"Name: {product.Name}");
    }
  

More Info & Links

DP-420 Style Practice Questions

Q1. What enables full-text query capability in Azure Cosmos DB for NoSQL?

  • A. A full-text index defined in the container's indexing policy, queried with functions like FULLTEXTCONTAINS/FULLTEXTSCORE
  • B. A separate Azure AI Search resource that must always be provisioned
  • C. The default range index applied to every property
  • D. The change feed processor

Q2. When might you choose Azure Cosmos DB's native full-text query over integrating Azure AI Search?

  • A. When you need lightweight keyword relevance ranking without provisioning and maintaining a separate search service
  • B. When you require a dedicated analytical store
  • C. When you need multi-region write support
  • D. When you need to enforce a composite index

Answer Key

Answers are listed at the very end so you can attempt the questions first.

  • Q1 (Full-Text Query): A. A full-text index defined in the container's indexing policy, queried with functions like FULLTEXTCONTAINS/FULLTEXTSCORE — Full-text query is a native Cosmos DB capability driven by a full-text index and query functions like FULLTEXTCONTAINS and FULLTEXTSCORE.
  • Q2 (Full-Text Query): A. When you need lightweight keyword relevance ranking without provisioning and maintaining a separate search service — Native full-text query avoids the cost/complexity of a separate Azure AI Search service for basic BM25-style keyword relevance search.

DP-420: Designing and Implementing Cloud-Native Applications Using Microsoft Azure Cosmos DB — term detail page