How to filter by properties and nearText in v4?

Hi I am using the latest v4 weaviate client and wish to query a collection using nearText and filter by properties at the same time. How can I achieve this? Thanks

hi @DeltaBoukensha !!

Welcome to our community :hugs:

Check here how you can filter by properties:

For instance:

from weaviate.classes.query import Filter

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
    filters=Filter.by_property("round").equal("Double Jeopardy!"),
    limit=3
)

for o in response.objects:
    print(o.properties)

Let me know if this helps!

Thank!

Hi thanks for reply. How do I use filters in combination with nearText queries?

In your example you filter only by the property round.

If there would be another property called “description” how could I for example filter by description nearText “positive customer review” AND property round AND datetime perhaps?

hi @DeltaBoukensha !!

You can filter with multiple conditions and nested filters.

For example:

from weaviate.classes.query import Filter

filters = Filter.by_property("answer").like("*bird*") &
            (Filter.by_property("points").greater_than(700) | Filter.by_property("points").less_than(300))

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
    filters=filters,
    limit=3
)

for o in response.objects:
    print(o.properties)

Let me know if this helps!

Thanks!

Like function seems to be a regexp. Is there a way to combine and use nearText search instead? Or otherways to combine vector search?

If it is not possible please just say so instead of suggesting alternatives. I would prefer you just confirm that it is not possible to use nearText search in combination with other filters.

I tried this but I get a type error since I’m using typescript. Please help me, what could missing from the filters?

  const adventures = await client.collections.get("adventures").query.nearText(description, {
    filters: [
      client.collections.get("adventures").filter.byProperty('startDate').greaterThan(new Date()),
    ],
    targetVector: "vDescription",
    limit: 1000,
  });
  return adventures;
    No overload matches this call.
  Overload 1 of 3, '(query: string | string[], opts?: BaseNearTextOptions<undefined>): Promise<WeaviateReturn<undefined>>', gave the following error.
    Type 'FilterValue<Date>[]' is missing the following properties from type 'FilterValue': operator, value
  Overload 2 of 3, '(query: string | string[], opts: GroupByNearTextOptions<undefined>): Promise<GroupByReturn<undefined>>', gave the following error.
    Type 'FilterValue<Date>[]' is missing the following properties from type 'FilterValue': operator, value
  Overload 3 of 3, '(query: string | string[], opts?: NearTextOptions<undefined>): QueryReturn<undefined>', gave the following error.
    Type 'FilterValue<Date>[]' is missing the following properties from type 'FilterValue': operator, valuets(2769)
types.d.ts(61, 3): The expected type comes from property 'filters' which is declared here on type 'BaseNearTextOptions<undefined>'
types.d.ts(61, 3): The expected type comes from property 'filters' which is declared here on type 'GroupByNearTextOptions<undefined>'
types.d.ts(61, 3): The expected type comes from property 'filters' which is declared here on type 'NearTextOptions<undefined>'
(property) filters?: weaviate.FilterValue
The filters to be applied to the query. Use weaviate.filter.* to create filters

Hi @DeltaBoukensha !

AFAIK, like will only support * and ?

You can use the same filter with near_text or hybrid instead of fetch_objects

I see you are using JS, so it should be like this:

import weaviate, { Filters } from 'weaviate-client';

const jeopardy = client.collections.get('JeopardyQuestion');

const result = await jeopardy.query.nearText({
  filters: Filters.and(
    jeopardy.filter.byProperty('answer').like('*bird*'),

    Filters.or(
      jeopardy.filter.byProperty('points').greaterThan(700),
      jeopardy.filter.byProperty('points').lessThan(300)
    )
  ),
  limit: 3
})

Let me know if this helps.

1 Like

Thank you so much. This helps :heart:

1 Like