Pre-filtering Vector Searches

  • how-to
    +
    You can specify filters as part of a vector search statement which will restrict the documents searched during the query.

    About Pre-filtering

    Using pre-filtering as part of your vector search offers two key advantages:

    1. Enhanced precision and relevance: Narrow your search results based on specific criteria, such as organization, date/time ranges, or geospatial locations.

    2. Performance optimization: Reduce the search space before executing queries to improve query execution time and reduce computational overhead.

    Prerequisites

    Procedure

    To run a pre-filtered vector search with the REST API:

    1. In your command-line tool, enter a curl command with the XPOST verb.

    2. Set your header content to include "Content-Type: application/json".

    3. Add your username, password, and the Search Service endpoint on port 8094.

    4. Add the index name you want to query to the endpoint.

    curl -XPOST -H "Content-Type: application/json" \
      -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/bucket/vector-sample/scope/color/index/{INDEX_NAME}/query \
    -d \

    Example

    In the following example, you will extend a search query to find matches in color-index. A pre-filter on the query will narrow the documents in the index searched to those with a color field value that closely matches navy.

    curl -XPOST -H "Content-Type: application/json" \
      -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/bucket/vector-sample/scope/color/index/color-index/query \
    -d '{
          "fields": ["*"],
          "query": {
            "min": 70,
            "max": 80,
            "inclusive_min": false,
            "inclusive_max": true,
            "field": "brightness"
          },
          "knn": [
            {
              "k": 10,
              "field": "colorvect_l2",
              "vector": [ 176, 0, 176 ],
              "filter": {
                "field:  "color",
                "match": "navy"
              }
            }
          ]
        }'