Hi! I’d like to implement a query with an inverter (“not” filter) to one of the multiple conditional filters in a collection.query.near_text
search.
Here are the details with a made-up example: in collection “Test”, I would like to run a near text query for movies that EITHER 1) have DirectorA
as its movie_director
OR 2) do NOT have Horror
in its tags.
In case it’s helpful with getting started, here is the code that would exactly so except for having “do have Horror
in its tags” instead, adapted from what @DudaNogueira graciously shared last time for my previous question on a similar topic.
import weaviate
from weaviate import classes as wvc
client.collections.delete("Test")
collection = client.collections.create(
name="Test",
vectorizer_config=[
wvc.config.Configure.NamedVectors.text2vec_openai(name="default")
],
inverted_index_config=wvc.config.Configure.inverted_index(
index_null_state=True,
index_property_length=True
),
properties=[
wvc.config.Property(name="movie_description", data_type=wvc.config.DataType.TEXT),
wvc.config.Property(name="movie_tags", data_type=wvc.config.DataType.TEXT_ARRAY),
wvc.config.Property(name="movie_director", data_type=wvc.config.DataType.TEXT),
]
)
collection = client.collections.get("Test")
collection.data.insert_many([
{ "movie_description": "Move desc 1. No tag", "movie_director": "DirectorB"},
{ "movie_description": "Move desc 2. One Tag", "movie_tags": ["Drama"], "movie_director": "DirectorA"},
{ "movie_description": "Move desc 3. Two Tags", "movie_tags": ["Crime", "Horror"], "movie_director": "DirectorB"},
{ "movie_description": "Move desc 4. OverLap tags", "movie_tags": ["Horror", "Action"], "movie_director": "DirectorA"},
])
filters = (wvc.query.Filter.by_property("movie_tags").contains_any(["Horror"]) |
wvc.query.Filter.by_property("movie_director").equal("DirectorA")
)
query = collection.query.near_text(
query="some movie",
filters= filters
)
for o in query.objects:
print(o.properties)
I am aware that the “Not” operator is not implemented, although based on this thread it seems like it might be included soon in a new release? Meanwhile, I wonder what is the best way as of now to implement the “do not have” filter? I assume there might be a way to do so with client.graphql_raw_query
and GraphQL, but I don’t have any experience with the language, so any guidance (sample code for a not filter, documentations, etc.) is highly appreciated.
Thank you!