Git Product home page Git Product logo

Comments (5)

joein avatar joein commented on September 26, 2024

Hi @hopkins385

Yes, you are right, it is a typo in docs, thanks for pointing it out! We will update it soon

If you will have any other problems with filters feel free to reach us out or take a look at tests e.g. nested filter example

from qdrant-client.

joein avatar joein commented on September 26, 2024

Also it should be filter=models.Filter(must=[models.FieldCondition...., and not just filter=(must=...)

from qdrant-client.

hopkins385 avatar hopkins385 commented on September 26, 2024

cool. happy I was able to help.
Maybe someone can explain shortly which approach would be more suitable for my usecase.
Scenario: In a collection are multiple "documents" (langchain) clustered by custom metadata field "media_id".
Problem: I want to filter the documents to say maybe just 2 out of 5 by passing an array of media_ids.

Which approach would be better?
Sidenote: For me its not clear if the database is first looking for "similar" vectors and after that the result is filtered,
or if first the filter is applied and after that "similar" vectors are located

Approach 1:

def get_qdrant_documents(query: str, collection_name: str, media_ids: List[str] | None) -> List[Document]:
  
# ...

    filtr = models.Filter(
        must=[]
    )

    if media_ids is not None:
        filtr.must.append(
            models.FieldCondition(
                key="metadata.media_id",
                match=models.MatchAny(any=media_ids)
            )
        )

    return qdrant.similarity_search(
        query=query,
        filter=filtr,
    )

Approach 2:

def get_qdrant_documents(query: str, collection_name: str, media_ids: List[str] | None) -> List[Document]:

    filtr = models.Filter(
        must=[
            models.NestedCondition(
                nested=models.Nested(
                    key="metadata",
                    filter=models.Filter(
                        must=[]
                    )
                )
            )
        ]
    )

    if media_ids is not None:
        for media_id in media_ids:
            filtr.must[0].nested.filter.must.append(
                models.FieldCondition(
                    key="media_id",
                    match=models.MatchValue(value=media_id)
                )
            )

    return qdrant.similarity_search(
        query=query,
        filter=filtr,
    )

from qdrant-client.

davidmyriel avatar davidmyriel commented on September 26, 2024

Thank you for spotting, updating docs.

from qdrant-client.

joein avatar joein commented on September 26, 2024

Hello, @hopkins385

I guess the second filter is not what you want since, should has to be used instead of must.
I think in your case both approaches are not really different.
Explanation for the difference between filters like metadata.media_id and those build with models.Nested can be found here docs

About sidenote:
It is a bit more complex than just pre-filter or post-filter. Qdrant has an advanced query planner which helps to perform queries efficiently. We applied certain modifications to hnsw algorithm itself as well.
This blogpost can help you better understand Qdrant internals.

from qdrant-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.