Skip to main content

Filters

Overviewโ€‹

This page shows you how to add conditional filters to your search queries with the where operator.

Prerequisites

If you haven't yet, we recommend going through the Quickstart tutorial first to get the most out of this section.

A single-condition filterโ€‹

To add a filter, you must provide at least one where condition to your query.

The following example specifies that the round property must equal "Double Jeopardy!". Note that the valueText parameter is used since the property datatype is text.

Filter arguments list

See this page for the list of available filter arguments.

response = (
client.query
.get("JeopardyQuestion", ["question", "answer", "round"])
.with_where({
"path": ["round"],
"operator": "Equal",
"valueText": "Double Jeopardy!"
})
.with_limit(3)
.do()
)

print(json.dumps(response, indent=2))
Example response

It should produce a response like the one below:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "garage",
"question": "This French word originally meant \"a place where one docks\" a boat, not a car",
"round": "Double Jeopardy!"
},
{
"answer": "Mexico",
"question": "The Colorado River provides much of the border between this country's Baja California Norte & Sonora",
"round": "Double Jeopardy!"
},
{
"answer": "Amy Carter",
"question": "On September 1, 1996 this former first daughter married Jim Wentzel at the Pond House near Plains",
"round": "Double Jeopardy!"
}
]
}
}
}

With a search parameterโ€‹

Conditional filters can be combined with a search parameter such as nearXXX, hybrid or bm25.

The following example adds a points filter to a nearText query, where the points property must be greater than 200. Note that the valueInt is used as the property datatype is int.

response = (
client.query
.get("JeopardyQuestion", ["question", "answer", "round", "points"])
.with_where({
"path": ["points"],
"operator": "GreaterThan",
"valueInt": 200
})
.with_near_text({
"concepts": ["fashion icons"]
})
.with_limit(3)
.do()
)

print(json.dumps(response, indent=2))
Example response

It should produce a response like the one below:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "fashion designers",
"points": 400,
"question": "Ted Lapidus, Guy Laroche, Christian Lacroix",
"round": "Jeopardy!"
},
{
"answer": "Dapper Flapper",
"points": 400,
"question": "A stylish young woman of the 1920s",
"round": "Double Jeopardy!"
},
{
"answer": "Women's Wear Daily",
"points": 800,
"question": "This daily chronicler of the fashion industry launched \"W\", a bi-weekly, in 1972",
"round": "Jeopardy!"
}
]
}
}
}

By partial matches (text)โ€‹

With text data type properties, you can use the Like operator to filter by partial matches.

The following example filters for objects including the text "inter" in any part of a token in the answer property.

* vs ?

* matches zero or more characters, whereas ? matches exactly one unknown character.

response = (
client.query
.get("JeopardyQuestion", ["question", "answer", "round"])
.with_where({
"path": ["answer"],
"operator": "Like",
"valueText": "*inter*"
})
.with_limit(3)
.do()
)

print(json.dumps(response, indent=2))
Example response

It should produce a response like the one below:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "interglacial",
"question": "This term refers to the warm periods within ice ages; we're in one of those periods now",
"round": "Jeopardy!"
},
{
"answer": "the Interior",
"question": "In 1849, Thomas Ewing, \"The Logician of the West\", became the USA's first Secy. of this Cabinet Dept.",
"round": "Jeopardy!"
},
{
"answer": "Interlaken, Switzerland",
"question": "You can view the Jungfrau Peak from the main street of this town between the Brienz & Thun Lakes",
"round": "Final Jeopardy!"
}
]
}
}
}

Multiple-condition filtersโ€‹

To add a multiple-condition filter, you must set the operator to And or Or, and set two or more conditions under the corresponding operands parameter.

The following example specifies and And condition, so that both:

  • the round property must equal "Double Jeopardy!", and
  • the points property must be less than 600.
response = (
client.query
.get("JeopardyQuestion", ["question", "answer", "round", "points"])
.with_where({
"operator": "And",
"operands": [
{
"path": ["round"],
"operator": "Equal",
"valueText": "Double Jeopardy!",
},
{
"path": ["points"],
"operator": "LessThan",
"valueInt": 600,
},
]
})
.with_limit(3)
.do()
)

print(json.dumps(response, indent=2))
Example response

It should produce a response like the one below:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "Mexico",
"points": 200,
"question": "The Colorado River provides much of the border between this country's Baja California Norte & Sonora",
"round": "Double Jeopardy!"
},
{
"answer": "Amy Carter",
"points": 200,
"question": "On September 1, 1996 this former first daughter married Jim Wentzel at the Pond House near Plains",
"round": "Double Jeopardy!"
},
{
"answer": "Greek",
"points": 400,
"question": "Athenians speak the Attic dialect of this language",
"round": "Double Jeopardy!"
}
]
}
}
}

Nested multiple conditionsโ€‹

Conditional filters can be nested in Weaviate. To do so, set the operator of an outer operands value to And or Or. Then, you can provide two or more conditions to the inner operands.

The following example specifies that:

  • the answer property must contain a substring "nest", And
  • the points property must be greater than 700, Or, the points property must be less than 300.
response = (
client.query
.get("JeopardyQuestion", ["question", "answer", "round", "points"])
.with_where({
"operator": "And",
"operands": [
{
"path": ["answer"],
"operator": "Like",
"valueText": "*nest*",
},
{
"operator": "Or",
"operands": [
{
"path": ["points"],
"operator": "GreaterThan",
"valueInt": 700,
},
{
"path": ["points"],
"operator": "LessThan",
"valueInt": 300,
},
]
}
]
})
.with_limit(3)
.do()
)

print(json.dumps(response, indent=2))
Example response

It should produce a response like the one below:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "rhinestones",
"points": 100,
"question": "Imitation diamonds, they were originally gems obtained from a certain German river",
"round": "Jeopardy!"
},
{
"answer": "Clytemnestra",
"points": 1000,
"question": "In \"Absalom! Absalom!\", it's the \"mythological\" name of Thomas Stupen's daughter, known as Clytie for short",
"round": "Double Jeopardy!"
},
{
"answer": "Ernest Hemingway",
"points": 200,
"question": "His 1926 novel \"The Sun Also Rises\" has been published in England as \"Fiesta\"",
"round": "Jeopardy!"
}
]
}
}
}

More Resourcesโ€‹

If you can't find the answer to your question here, please look at the:

  1. Frequently Asked Questions. Or,
  2. Knowledge base of old issues. Or,
  3. For questions: Stackoverflow. Or,
  4. For more involved discussion: Weaviate Community Forum. Or,
  5. We also have a Slack channel.