Package: weaviate-filter
Weaviate FilterTheweaviate-filterpackage provides a convenient way to build GraphQL filters forWeaviate. The main class,GraphQLFilter, allows you to create complex filters by adding conditions and operands, and then retrieve the final filter object.InstallationTo install theweaviate-filterpackage, you can use pip:pip install weaviate-filterUsageTo use theweaviate-filterpackage, you first need to import theGraphQLFilterclass and create an instance of it:fromweaviate_filterimportGraphQLFilterfilter=GraphQLFilter()Adding a ConditionYou can add a condition to the filter by using theadd_conditionmethod:filter.add_condition(path="name",operator="Like",value_type="valueString",value="John")path: The path of the property (e.g., "name").operator: The operator to use for the condition (e.g., "Like").value_type: The type of the value (e.g., "valueString").value: The value for the condition (e.g., "John").Adding OperandsYou can add operands to the filter by using theadd_operandsmethod:filter.add_operands(operator="And",operand1,operand2)operator: The operator to use for the operands (e.g., "And").operands: The operands to add.Adding List Conditions as OperandsYou can add a list of conditions as operands to the filter by using theadd_list_conditions_as_operandsmethod:filter.add_list_conditions_as_operands(path="name",operator="Like",value_type="valueString",values_list=["John","Doe"])path: The path of the property (e.g., "name").operator: The operator to use for the conditions (e.g., "Like").value_type: The type of the values (e.g., "valueString").values_list: The list of values for the conditions (e.g., ["John", "Doe"]).condition_operator(optional): The operator to use for the operands (e.g., "Or"). Defaults to "Or".Adding Conditions for Multiple PathsYou can add conditions for multiple paths as operands to the filter by using theadd_conditions_for_multiple_pathsmethod:filter.add_conditions_for_multiple_paths(paths=["name","age"],operator="Equal",value_type="valueString",value="John")paths: The paths of the properties (e.g., ["name", "age"]).operator: The operator to use for the conditions (e.g., "Equal").value_type: The type of the value (e.g., "valueString").value: The value for the conditions (e.g., "John").condition_operator(optional): The operator to use for the operands (e.g., "Or"). Defaults to "Or".Getting the FilterOnce you have added all the conditions and operands, you can get the final filter object by using theget_filtermethod:final_filter=filter.get_filter(operator="And")operator(optional): The operator for the filter (e.g., "And"). Defaults to "And".Theget_filtermethod will return a dictionary with the final filter object.ExampleHere is a complete example of using theweaviate-filterpackage:fromweaviate_filterimportGraphQLFilter# Create a filterfilter=GraphQLFilter()# Add a conditionfilter.add_condition(path="name",operator="Like",value_type="valueString",value="John")# Add operandsoperand1={"path":"age","operator":"GreaterThan","valueInt":30}operand2={"path":"age","operator":"LessThan","valueInt":50}filter.add_operands(operator="And",operand1,operand2)# Add list conditions as operandsfilter.add_list_conditions_as_operands(path="name",operator="Like",value_type="valueString",values_list=["John","Doe"])# Add conditions for multiple pathsfilter.add_conditions_for_multiple_paths(paths=["name","age"],operator="Equal",value_type="valueString",value="John")# Get the final filter objectfinal_filter=filter.get_filter(operator="And")print(final_filter)Output:{'operator': 'And', 'operands': [{'operator': 'And', 'operands': [{'path': 'age', 'operator': 'GreaterThan', 'valueInt': 30}, {'path': 'age', 'operator': 'LessThan', 'valueInt': 50}]}, {'operator': 'Or', 'operands': [{'path': 'name', 'operator': 'Like', 'valueString': 'John'}, {'path': 'name', 'operator': 'Like', 'valueString': 'Doe'}]}, {'operator': 'Or', 'operands': [{'path': ['name'], 'operator': 'Equal', 'valueString': 'John'}, {'path': ['age'], 'operator': 'Equal', 'valueString': 'John'}]}]}Real Use Case DemoImport Required Modules:importweaviatefromsentence_transformersimportSentenceTransformerfromweaviate_filterimportGraphQLFilterImport the required modules. This includes the Weaviate client, the Sentence Transformers library for encoding the query, and the GraphQLFilter class for constructing the query filter.Initialize SentenceTransformer Model:model=SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')Initialize the SentenceTransformer model. This model is used to convert the text query into a vector.Setup Weaviate Client:WEAVIATE_URL=""WEAVIATE_API_KEY=""client=weaviate.Client(url=WEAVIATE_URL,# Replace w/ your endpointauth_client_secret=weaviate.AuthApiKey(api_key=WEAVIATE_API_KEY),# Replace w/ your Weaviate instance API key)Set up the Weaviate client. You need to replaceWEAVIATE_URLandWEAVIATE_API_KEYwith your own Weaviate instance's URL and API key.Define Filters:genders=["M","F"]countries=["Germany"]labels=["dlf","building"]Define the filters you want to apply. In this example, we are filtering for entries with gender "M" or "F", birth_country "Germany", and labels "dlf" or "building" with a value greater than or equal to 1.Create Filter:filter=GraphQLFilter()filter.add_list_conditions_as_operands(["gender"],"Equal","valueText",genders,"Or")filter.add_list_conditions_as_operands(["birth_country"],"Equal","valueText",countries,"Or")filter.add_conditions_for_multiple_paths(labels,"GreaterThanEqual","valueNumber",1,"Or")where_filter=filter.get_filter(operator="Or")Create the filter using theGraphQLFilterclass. Theadd_list_conditions_as_operandsmethod is used to add a list of conditions as operands with a specified operator ("And" or "Or"). Theadd_conditions_for_multiple_pathsmethod is used to add conditions for multiple paths with a specified operator. Finally, theget_filtermethod is used to get the final filter with a specified operator.Define and Encode Query:query="We were hungry"query_vector=model.encode(query)Define the query and encode it into a vector using the SentenceTransformer model.Create nearVector Dictionary:nearVector={"vector":query_vector}Create thenearVectordictionary with the encoded query vector.Define Field Options:field_options=["gender","window_text","birth_country","dlf","building"]Define the fields you want to retrieve in the query.Perform Query:result=client.query.get("Window",field_options).with_near_vector(nearVector).with_limit(2).with_additional(['certainty']).with_where(where_filter).do()Perform the query using the Weaviate client. Thewith_near_vectormethod is used to add thenearVectorto the query. Thewith_limitmethod is used to limit the number of results. Thewith_additionalmethod is used to add additional fields to the query. Thewith_wheremethod is used to add thewherefilter to the query.Print Results:forrinresult["data"]["Get"]["Window"]:print(r)Print the results of the query.