Help to get the right syntax for query statement - from v3 client to v4 client

Hi,
Please help to arrive the right weaviate client v4 sytnax for the below v3 client query statement.

V3 client query statement:

class_name = “Yoga”
response = (
client.query
.get(class_name, [“document”])
.with_near_text({‘concepts’ : [“Mahayana Buddhism”]})
.do()
)
print(response[‘data’][‘Get’][class_name][0][‘document’])

“Yoga” is the classification, and “document” is the property. I am trying to text match “Mahayana Buddhism”

It was working with v3 client but after v4 upgrade, I am unable to get the right query statement.

How do I get this working with v4 client?

Thanks.

Hi @pon_raj, I hope you having a nice day!

To perform near_text search with Python Client v4, please use:

yoga = client.collections.get(“Yoga”)
response = yoga.query.near_text(
query=“Mahayana Buddhism”,
return_metadata=MetadataQuery(distance=True)
)

I hope this helps, have a nice week!

Thank you for the help with v4 syntax. It works but response has objects no matter what I query. For an example, if I query non-existing text such as “abcaslkhas”, it returns objects.
Do I need to add any additional attributes to the query so that it returns objects ony if the query string exists?
Thanks.

Happy Friday @pon_raj !

So the Near Text operator is to find objects with the nearest vector to an input text.

Indeed you could insert some unrelated text and still retrieve an object because it vectorize the query input and it tries to find ‘ANN’ approximate nearest neighbor for vector-search queries.

The following operator can explain the distance in such search and shall return a far distance value:

  _additional {
    distance
  }   

See here:

However from what I understood you are trying to achieve, then I would highly recommend you → Hybrid Search which is the key to your case because Hybrid search results can favor the keyword component, the vector component or mix of both.

  • Use alpha operator of 1 is a pure vector search or;
  • An alpha of 0 is a pure keyword search or;
  • Play with the value to lean more towards keyword or vector component!

I hope this helps

Thank you for the helpful advice.