contains()#

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

Filters values produced by a Producer to only include strings that contain a specified substring. Must be called in a rule or query context.

Parameters#

NameTypeDescription
stringProducerThe string to check.
substringProducer or Python str objectThe substring to check for.

Returns#

An Expression object.

Example#

Use contains() to filter strings that contain a specified substring:

#import relationalai as rai
from relationalai.std import strings


# =====
# SETUP
# =====

model = rai.Model("MyModel")
Person = model.Type("Person")

with model.rule():
    Person.add(id=1).set(name="Alice")
    Person.add(id=2).set(name="Bob")


# =======
# EXAMPLE
# =======

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

print(response.results)
import relationalai as rai
from relationalai.std import strings


# =====
# SETUP
# =====

model = rai.Model("MyModel2")
Person = model.Type("Person")

with model.rule():
    Person.add(id=1).set(name="Alice")
    Person.add(id=2).set(name="Bob")


# ========
# EXAMPLES
# ========

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

print(response.results)
#    id   name
# 0   1  Alice


# Get all people whose name does not contain "ice".
with model.query() as select:
    person = Person()
    with model.not_found():
        strings.contains(person.name, "ice")
    response = select(person.id, person.name)

print(response.results)
#    id name
# 0   2  Bob

See Also#