contains()#

relationalai.std.strings
contains(string: str | Producer, substring: str | Producer) -> Expression

Checks if a string contains a substring. Must be called in a rule or query context.

Parameters#

NameTypeDescription
stringProducerThe string to check.
substringstr or ProducerThe substring to check for.

Returns#

An Expression object.

Example#

import relationalai as rai
from relationalai.std.strings import contains

# Create a model named "people" with a Person type.
model = rai.Model("people")
Person = model.Type("Person")

# Add some people to the model.
with model.rule():
    alice = Person.add(name="Alice")
    bob = Person.add(name="Bob")

# Get all people whose name contains "Ali".
with model.query() as select:
    person = Person()
    contains(person.name, "Ali")
    response = select(person.name)

print(response.results)
# Output:
#     name
# 0  Alice

# The `substring` argument can also be a Producer.
with model.query() as select:
    sub = Person(name="Bob").name
    person = Person()
    contains(person.name, sub)
    response = select(person.name)

print(response.results)
# Output:
#    name
# 0   Bob

See Also#