Query for suggestions with index



Configure the index for suggestions

  • In order to be able to ask for suggested terms when querying an index field,
    that field must first be configured for suggestions in the index definition.

  • See the following sample index:
    (This index will be used in the examples ahead).

class Products_ByName(AbstractIndexCreationTask):
    # The IndexEntry class defines the index-fields
    class IndexEntry:
        def __init__(self, product_name: str = None):
            self.product_name = product_name

    def __init__(self):
        super().__init__()
        # The 'map' function defines the content of the index-fields
        self.map = "from product in docs.Products select new {product_name = product.Name}"
        self._suggestion("product_name")
        self._index("product_name", FieldIndexing.SEARCH)

Increased indexing time:

  • When configuring an index for suggestions, then during the indexing process,
    in addition to the regular breakdown of the data into terms (tokenization),
    RavenDB will scramble the terms to simulate common errors.

  • This can impact indexing speed but the cost of querying suggestions is Not impacted.

The index terms

Based on the Northwind sample data,
these are the terms generated for the above index Products/ByName:

Figure 1. Index terms

Terms generated for index Products/ByName

  1. The index-field name - as defined in the index definition.
    In this example the field name is ProductName.

  2. The terms that were generated for this index-field from the documents in the Products collection.

    • The image shows a partial view out of the 163 terms in this list.
    • The terms were generated by RavenDB's default search analyzer since full-text search was set on this field.

Suggest terms - for single term

Based on the Northwind sample data,
the following query on the index Products/ByName from above has no resulting documents,
since the term chokolade does Not exist in the index terms for index-field ProductName.

# This query on index 'Products/ByName' has NO resulting documents
products = list(
    session.query_index_type(Products_ByName, Products_ByName.IndexEntry)
    .search("product_name", "chokolade")
    .of_type(Product)
)

If you suspect that the term chokolade in the query criteria is written incorrectly,
you can ask RavenDB to suggest similar terms from the index, as follows:

# Query the index for suggested terms for single term:
# ====================================================

suggestions = (
    session
    # Query the index
    .query_index_type(Products_ByName, Products_ByName.IndexEntry)
    # Call 'suggest_using'
    .suggest_using(
        lambda builder: builder
        # Request to get terms from index-field 'ProductName' that are similar to 'chokolade'
        .by_field("product_name", "chokolade")
    ).execute()
)
# Define the suggestion request for single term
suggestion_request = SuggestionWithTerm("product_name")
# Looking for terms from index-field 'product_name' that are similar to 'chokolade'
suggestion_request.term = "chokolade"

# Query the index for suggestions
suggestions = (
    session.query_index_type(Products_ByName, Products_ByName.IndexEntry)
    # Call 'suggest_using' - pass the suggestion request
    .suggest_using(suggestion_request).execute()
)
// Query for terms from index-field 'ProductName' that are similar to 'chokolade'
from index "Products/ByName" 
select suggest(ProductName, "chokolade")

# The resulting suggested terms:
# ==============================

print("Suggested terms in index-field 'product_name' that are similar to 'chokolade':")
for suggested_term in suggestions["product_name"].suggestions:
    print(f"\t{suggested_term}")

# Suggested terms in index-field 'product_name' that are similar to 'chokolade':
#     schokolade
#     chocolade
#     chocolate

Suggest terms - for multiple terms

# Query the index for suggested terms for multiple terms:
# =======================================================

suggestions = (
    session
    # Query the index
    .query_index_type(Products_ByName, Products_ByName.IndexEntry)
    # Call 'suggest_using'
    .suggest_using(
        lambda builder: builder
        # Request to get terms from index-field 'product_name' that are similar to 'chokolade' OR 'syrop'
        .by_field("product_name", ["chokolade", "syrop"])
    ).execute()
)
# Define the suggestion request for multiple terms
suggestion_request = SuggestionWithTerms("product_name")
# Looking for terms from index-field 'product_name' that are similar to 'chokolade' OR 'syrop'
suggestion_request.terms = ["chokolade", "syrop"]

# Query the index for suggestions
suggestions = (
    session.query_index_type(Products_ByName, Products_ByName.IndexEntry)
    # Call 'suggest_using' - pass the suggestion request
    .suggest_using(suggestion_request).execute()
)
// Query for terms from index-field 'ProductName' that are similar to 'chokolade' OR 'syrop'
from index "Products/ByName" select suggest(ProductName, $p0)
{ "p0" : ["chokolade", "syrop"] }

# The resulting suggested terms:
# ==============================

# Suggested terms in index-field 'product_name' that are similar to 'chokolade' OR to 'syrop':
#     schokolade
#     chocolade
#     chocolate
#     sirop
#     syrup

Suggest terms - for multiple fields

# Query the index for suggested terms in multiple fields:
# =======================================================

suggestions = (
    session
    # Query the index
    .query_index_type(Companies_ByNameAndByContactName, Companies_ByNameAndByContactName.IndexEntry)
    # Call 'suggest_using' to get suggestions for terms that are
    # similar to 'chese' in first index-field (e.g. 'company_name')
    .suggest_using(lambda builder: builder.by_field("company_name", "chese"))
    # Call 'and_suggest_using' to get suggestions for terms that are
    # similar to 'frank' in an additional index-field (e.g. 'company_name')
    .and_suggest_using(lambda builder: builder.by_field("contact_name", "frank")).execute()
)
# Define suggestion requests for multiple fields:

request1 = SuggestionWithTerm("company_name")
# Looking for terms from index-field 'company_name' that are similar to 'chese'
request1.term = "chese"

request2 = SuggestionWithTerm("contact_name")
# Looking for terms from nested index-field 'contact_name' that are similar to 'frank'
request2.term = "frank"

# Query the index for suggestions
suggestions = (
    session.query_index_type(
        Companies_ByNameAndByContactName, Companies_ByNameAndByContactName.IndexEntry
    )
    # Call 'suggest_using' - pass the suggestion request for the first index-field
    .suggest_using(request1)
    # Call 'and_suggest_using' - pass the suggestion request for the second index-field
    .and_suggest_using(request2).execute()
)
class Companies_ByNameAndByContactName(AbstractIndexCreationTask):
    class IndexEntry:
        def __init__(self, company_name: str = None, contact_name: str = None):
            self.company_name = company_name
            self.contact_name = contact_name

    def __init__(self):
        super().__init__()
        self.map = "from company in docs.Companies select new {company_name = company.Name, contact_name = company.Contact.Name}"

        # Configure the index-fields for suggestions
        self._suggestion("company_name")
        self._suggestion("contact_name")

        # Optionally: set 'search' on the index-fields
        # This will split the fields' content into multiple terms allowing for a full-text search
        self._index("company_name", FieldIndexing.SEARCH)
        self._index("contact_name", FieldIndexing.SEARCH)
// Query for suggested terms 
// from index-field 'CompanyName' AND from index-field 'ContactName'
from index "Companies/ByNameAndByContactName"
select suggest(CompanyName, "chese"), suggest(ContactName, "frank")

# The resulting suggested terms:
# ==============================

# Suggested terms in index-field 'company_name' that is similar to 'chese':
#     cheese
#     chinese

# Suggested terms in index-field 'contact_name' that are similar to 'frank':
#     fran
#     franken

Suggest terms - customize options and display name

# Query the index for suggested terms - customize options and display name:
# =========================================================================

suggestions = (
    session
    # Query the index
    .query_index_type(Products_ByName, Products_ByName.IndexEntry)
    # Call 'suggest_using'
    .suggest_using(
        lambda builder: builder.by_field("product_name", "chokolade")
        # Customize suggestions options
        .with_options(
            SuggestionOptions(
                accuracy=0.3,
                page_size=5,
                distance=StringDistanceTypes.N_GRAM,
                sort_mode=SuggestionSortMode.POPULARITY,
            )
        )
        # Customize display name for results
        .with_display_name("SomeCustomName")
    ).execute()
)
# Define the suggestion request
suggestion_request = SuggestionWithTerm("product_name")
# Looking for terms from index-field 'ProductName' that are similar to 'chokolade'
suggestion_request.term = "chokolade"
# Customize options
suggestion_request.options = SuggestionOptions(
    accuracy=0.3,
    page_size=5,
    distance=StringDistanceTypes.N_GRAM,
    sort_mode=SuggestionSortMode.POPULARITY,
)
# Customize display name
suggestion_request.display_field = "SomeCustomName"

# Query the index for suggestions
suggestions = (
    session.query_index_type(Products_ByName, Products_ByName.IndexEntry)
    # Call 'suggest_using' - pass the suggestion request
    .suggest_using(suggestion_request).execute()
)
// Query for suggested terms - customize options and display name
from index "Products/ByName"
select suggest(
    ProductName,
    "chokolade",
    '{ "Accuracy" : 0.3, "PageSize" : 5, "Distance" : "NGram", "SortMode" : "Popularity" }'
) as "SomeCustomName"

# The resulting suggested terms:
# ==============================

print("Suggested terms:")
# Results are available under the custom name entry
for suggested_term in suggestions["SomeCustomName"].suggestions:
    print(f"\t{suggested_term}")

# Suggested terms:
#     chocolade
#     schokolade
#     chocolate
#     chowder
#     marmalade